2025-02-01 21:48:13 +00:00
|
|
|
import { AggregateRoot, Result, UniqueID } from "@common/domain";
|
|
|
|
|
import { UserAuthenticatedEvent } from "../events";
|
2025-02-03 13:12:36 +00:00
|
|
|
import { EmailAddress, PasswordHash, Username } from "../value-objects";
|
2025-02-01 21:48:13 +00:00
|
|
|
|
|
|
|
|
export interface IAuthenticatedUserProps {
|
|
|
|
|
username: Username;
|
|
|
|
|
email: EmailAddress;
|
2025-02-03 13:12:36 +00:00
|
|
|
passwordHash: PasswordHash;
|
2025-02-01 21:48:13 +00:00
|
|
|
roles: string[];
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 21:54:51 +00:00
|
|
|
export interface IAuthenticatedUser {
|
|
|
|
|
username: Username;
|
|
|
|
|
email: EmailAddress;
|
|
|
|
|
|
|
|
|
|
accessToken: string;
|
|
|
|
|
refreshToken: string;
|
|
|
|
|
|
|
|
|
|
isUser: boolean;
|
|
|
|
|
isAdmin: boolean;
|
|
|
|
|
|
|
|
|
|
comparePassword(password: PasswordHash | string): Promise<boolean>;
|
|
|
|
|
getRoles(): string[];
|
|
|
|
|
toPersistenceData(): any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class AuthenticatedUser
|
|
|
|
|
extends AggregateRoot<IAuthenticatedUserProps>
|
|
|
|
|
implements IAuthenticatedUser
|
|
|
|
|
{
|
|
|
|
|
public accessToken: string = "";
|
|
|
|
|
public refreshToken: string = "";
|
|
|
|
|
|
2025-02-03 13:12:36 +00:00
|
|
|
static create(props: IAuthenticatedUserProps, id: UniqueID): Result<AuthenticatedUser, Error> {
|
|
|
|
|
const user = new AuthenticatedUser(props, id);
|
2025-02-01 21:48:13 +00:00
|
|
|
|
|
|
|
|
// 🔹 Disparar evento de dominio "UserAuthenticatedEvent"
|
2025-02-03 13:12:36 +00:00
|
|
|
const { email } = props;
|
2025-02-01 21:48:13 +00:00
|
|
|
user.addDomainEvent(new UserAuthenticatedEvent(id, email.toString()));
|
|
|
|
|
|
|
|
|
|
return Result.ok(user);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 19:50:16 +00:00
|
|
|
private _hasRole(role: string): boolean {
|
|
|
|
|
return (this._props.roles || []).some((r) => r === role);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 21:54:51 +00:00
|
|
|
comparePassword(password: PasswordHash | string): Promise<boolean> {
|
|
|
|
|
if (typeof password === "string") {
|
|
|
|
|
return this._props.passwordHash.compare(password);
|
|
|
|
|
} else {
|
|
|
|
|
return this._props.passwordHash.compare(password.toString());
|
|
|
|
|
}
|
2025-02-03 18:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 21:54:51 +00:00
|
|
|
getRoles(): string[] {
|
2025-02-03 19:50:16 +00:00
|
|
|
return this._props.roles;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 21:54:51 +00:00
|
|
|
get username(): Username {
|
|
|
|
|
return this._props.username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get email(): EmailAddress {
|
|
|
|
|
return this._props.email;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 19:50:16 +00:00
|
|
|
get isUser(): boolean {
|
|
|
|
|
return this._hasRole("user");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isAdmin(): boolean {
|
|
|
|
|
return this._hasRole("admin");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-01 21:48:13 +00:00
|
|
|
/**
|
|
|
|
|
* 🔹 Devuelve una representación lista para persistencia
|
|
|
|
|
*/
|
2025-02-03 13:12:36 +00:00
|
|
|
toPersistenceData(): any {
|
2025-02-01 21:48:13 +00:00
|
|
|
return {
|
|
|
|
|
id: this._id.toString(),
|
|
|
|
|
username: this._props.username.toString(),
|
|
|
|
|
email: this._props.email.toString(),
|
2025-02-03 13:12:36 +00:00
|
|
|
password: this._props.passwordHash.toString(),
|
2025-02-03 18:03:23 +00:00
|
|
|
roles: this._props.roles.map((role) => role.toString()),
|
2025-02-03 21:54:51 +00:00
|
|
|
accessToken: this.accessToken,
|
|
|
|
|
refreshToken: this.refreshToken,
|
2025-02-01 21:48:13 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|