refactor to adapters
This commit is contained in:
41
core/identity/domain/value-objects/PasswordHash.ts
Normal file
41
core/identity/domain/value-objects/PasswordHash.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
import type { IValueObject } from '@gridpilot/shared/domain';
|
||||
|
||||
export interface PasswordHashProps {
|
||||
value: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Value Object: PasswordHash
|
||||
*
|
||||
* Wraps a bcrypt-hashed password string and provides verification.
|
||||
*/
|
||||
export class PasswordHash implements IValueObject<PasswordHashProps> {
|
||||
public readonly props: PasswordHashProps;
|
||||
|
||||
private constructor(value: string) {
|
||||
this.props = { value };
|
||||
}
|
||||
|
||||
static async create(plain: string): Promise<PasswordHash> {
|
||||
const saltRounds = 12;
|
||||
const hash = await bcrypt.hash(plain, saltRounds);
|
||||
return new PasswordHash(hash);
|
||||
}
|
||||
|
||||
static fromHash(hash: string): PasswordHash {
|
||||
return new PasswordHash(hash);
|
||||
}
|
||||
|
||||
get value(): string {
|
||||
return this.props.value;
|
||||
}
|
||||
|
||||
async verify(plain: string): Promise<boolean> {
|
||||
return bcrypt.compare(plain, this.props.value);
|
||||
}
|
||||
|
||||
equals(other: IValueObject<PasswordHashProps>): boolean {
|
||||
return this.props.value === other.props.value;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import type { IValueObject } from '@gridpilot/shared/domain';
|
||||
|
||||
export interface UserIdProps {
|
||||
@@ -14,6 +15,10 @@ export class UserId implements IValueObject<UserIdProps> {
|
||||
this.props = { value };
|
||||
}
|
||||
|
||||
public static create(): UserId {
|
||||
return new UserId(uuidv4());
|
||||
}
|
||||
|
||||
public static fromString(value: string): UserId {
|
||||
return new UserId(value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user