diff --git a/src/core/strings/constants/default-data.constants.ts b/src/core/strings/constants/default-data.constants.ts index 278cd8e..b3d80dd 100644 --- a/src/core/strings/constants/default-data.constants.ts +++ b/src/core/strings/constants/default-data.constants.ts @@ -1,9 +1,11 @@ import { UserRole } from 'src/modules/user-related/user/constants'; +import { STATUS } from './base.constants'; export const default_admin = { id: 'c59f811e-873c-4472-bd58-21c111902114', name: 'superadmin', username: 'superadmin', password: 'Eigen123!', + status: STATUS.ACTIVE, role: UserRole.SUPERADMIN, }; diff --git a/src/modules/configuration/auth/domain/entities/login.event.ts b/src/modules/configuration/auth/domain/entities/login.event.ts new file mode 100644 index 0000000..cac5fb8 --- /dev/null +++ b/src/modules/configuration/auth/domain/entities/login.event.ts @@ -0,0 +1,5 @@ +import { IEventAuth } from 'src/core/strings/constants/interface.constants'; + +export class UserLoginEvent { + constructor(public readonly data: IEventAuth) {} +} diff --git a/src/modules/configuration/auth/domain/managers/login.manager.ts b/src/modules/configuration/auth/domain/managers/login.manager.ts index fe15496..3dfbb1f 100644 --- a/src/modules/configuration/auth/domain/managers/login.manager.ts +++ b/src/modules/configuration/auth/domain/managers/login.manager.ts @@ -7,9 +7,11 @@ import { import { validatePassword } from 'src/core/helpers/password/bcrypt.helpers'; import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager'; import { SessionService } from 'src/core/sessions'; +import { STATUS } from 'src/core/strings/constants/base.constants'; import { EventTopics } from 'src/core/strings/constants/interface.constants'; import { UserModel } from 'src/modules/user-related/user/data/models/user.model'; import { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity'; +import { UserLoginEvent } from '../entities/login.event'; @Injectable() export class LoginManager extends BaseCustomManager { @@ -27,19 +29,23 @@ export class LoginManager extends BaseCustomManager { } async process(): Promise { + // get user active by username this.userLogin = await this.dataService.getOneByOptions({ where: { username: this.data.username, + status: STATUS.ACTIVE, }, }); if (!this.userLogin) this.throwError(); + // validasi password const valid = await validatePassword( this.data.password, this.userLogin?.password, ); if (!valid) this.throwError(); + // * Disini untuk isi token const tokenData = { id: this.userLogin.id, name: this.userLogin.name, @@ -51,6 +57,7 @@ export class LoginManager extends BaseCustomManager { this.token = this.session.createAccessToken(tokenData); const refreshToken = this.session.createAccessToken(tokenData); + // Update refresh token await this.dataService.update( this.queryRunner, this.entityTarget, @@ -83,9 +90,19 @@ export class LoginManager extends BaseCustomManager { } get eventTopics(): EventTopics[] { - return []; + return [ + { + topic: UserLoginEvent, + data: { + id: this.userLogin.id, + type: 'login', + timestamp: new Date().getTime(), + }, + }, + ]; } + // !throw errornya akan sama, untuk security throwError() { throw new UnauthorizedException({ statusCode: HttpStatus.UNAUTHORIZED,