feat(SPG-328) BE Login Web Admin

pull/2/head
ashar 2024-06-05 13:54:31 +07:00
parent 1aae9d0a1b
commit df6adf0e0f
3 changed files with 25 additions and 1 deletions

View File

@ -1,9 +1,11 @@
import { UserRole } from 'src/modules/user-related/user/constants'; import { UserRole } from 'src/modules/user-related/user/constants';
import { STATUS } from './base.constants';
export const default_admin = { export const default_admin = {
id: 'c59f811e-873c-4472-bd58-21c111902114', id: 'c59f811e-873c-4472-bd58-21c111902114',
name: 'superadmin', name: 'superadmin',
username: 'superadmin', username: 'superadmin',
password: 'Eigen123!', password: 'Eigen123!',
status: STATUS.ACTIVE,
role: UserRole.SUPERADMIN, role: UserRole.SUPERADMIN,
}; };

View File

@ -0,0 +1,5 @@
import { IEventAuth } from 'src/core/strings/constants/interface.constants';
export class UserLoginEvent {
constructor(public readonly data: IEventAuth) {}
}

View File

@ -7,9 +7,11 @@ import {
import { validatePassword } from 'src/core/helpers/password/bcrypt.helpers'; import { validatePassword } from 'src/core/helpers/password/bcrypt.helpers';
import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager'; import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager';
import { SessionService } from 'src/core/sessions'; 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 { EventTopics } from 'src/core/strings/constants/interface.constants';
import { UserModel } from 'src/modules/user-related/user/data/models/user.model'; 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 { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';
import { UserLoginEvent } from '../entities/login.event';
@Injectable() @Injectable()
export class LoginManager extends BaseCustomManager<UserEntity> { export class LoginManager extends BaseCustomManager<UserEntity> {
@ -27,19 +29,23 @@ export class LoginManager extends BaseCustomManager<UserEntity> {
} }
async process(): Promise<void> { async process(): Promise<void> {
// get user active by username
this.userLogin = await this.dataService.getOneByOptions({ this.userLogin = await this.dataService.getOneByOptions({
where: { where: {
username: this.data.username, username: this.data.username,
status: STATUS.ACTIVE,
}, },
}); });
if (!this.userLogin) this.throwError(); if (!this.userLogin) this.throwError();
// validasi password
const valid = await validatePassword( const valid = await validatePassword(
this.data.password, this.data.password,
this.userLogin?.password, this.userLogin?.password,
); );
if (!valid) this.throwError(); if (!valid) this.throwError();
// * Disini untuk isi token
const tokenData = { const tokenData = {
id: this.userLogin.id, id: this.userLogin.id,
name: this.userLogin.name, name: this.userLogin.name,
@ -51,6 +57,7 @@ export class LoginManager extends BaseCustomManager<UserEntity> {
this.token = this.session.createAccessToken(tokenData); this.token = this.session.createAccessToken(tokenData);
const refreshToken = this.session.createAccessToken(tokenData); const refreshToken = this.session.createAccessToken(tokenData);
// Update refresh token
await this.dataService.update( await this.dataService.update(
this.queryRunner, this.queryRunner,
this.entityTarget, this.entityTarget,
@ -83,9 +90,19 @@ export class LoginManager extends BaseCustomManager<UserEntity> {
} }
get eventTopics(): EventTopics[] { 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() { throwError() {
throw new UnauthorizedException({ throw new UnauthorizedException({
statusCode: HttpStatus.UNAUTHORIZED, statusCode: HttpStatus.UNAUTHORIZED,