141 lines
3.7 KiB
TypeScript
141 lines
3.7 KiB
TypeScript
import {
|
|
HttpStatus,
|
|
Inject,
|
|
Injectable,
|
|
Logger,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common';
|
|
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<UserEntity> {
|
|
@Inject()
|
|
protected session: SessionService;
|
|
protected token;
|
|
protected userLogin;
|
|
|
|
async validateProcess(): Promise<void> {
|
|
return;
|
|
}
|
|
|
|
async beforeProcess(): Promise<void> {
|
|
return;
|
|
}
|
|
|
|
async process(): Promise<void> {
|
|
// get user active by username
|
|
Logger.debug('getOneByOptions', 'LoginManager');
|
|
this.userLogin = await this.dataService.getOneByOptions({
|
|
where: {
|
|
username: this.data.username,
|
|
status: STATUS.ACTIVE,
|
|
},
|
|
relations: [
|
|
'user_privilege',
|
|
'user_privilege.user_privilege_configurations',
|
|
],
|
|
});
|
|
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,
|
|
username: this.userLogin.username,
|
|
role: this.userLogin.role,
|
|
user_privilege_id: this.userLogin.user_privilege_id,
|
|
};
|
|
|
|
Logger.debug('Sign Token', 'LoginManager');
|
|
this.token = this.session.createAccessToken(tokenData);
|
|
|
|
Logger.debug('refreshToken', 'LoginManager');
|
|
const refreshToken = this.session.createAccessToken(tokenData);
|
|
|
|
Logger.debug('Update Refresh Token', 'LoginManager');
|
|
// Update refresh token
|
|
await this.dataService.update(
|
|
this.queryRunner,
|
|
this.entityTarget,
|
|
{ id: this.userLogin.id },
|
|
{
|
|
refresh_token: refreshToken,
|
|
},
|
|
);
|
|
|
|
Logger.debug('Process Login Done', 'LoginManager');
|
|
return;
|
|
}
|
|
|
|
async afterProcess(): Promise<void> {
|
|
return;
|
|
}
|
|
|
|
getResult() {
|
|
return {
|
|
id: this.userLogin.id,
|
|
name: this.userLogin.name,
|
|
username: this.userLogin.username,
|
|
role: this.userLogin.role,
|
|
user_privilege_id: this.userLogin.user_privilege_id,
|
|
token: this.token,
|
|
user_privilege:
|
|
this.userLogin.user_privilege?.user_privilege_configurations
|
|
?.filter((item) => item.module != 'POS')
|
|
?.map((item) => {
|
|
return {
|
|
id: item.id,
|
|
menu: item.menu,
|
|
menu_label: item.menu_label,
|
|
view: item.view,
|
|
create: item.create,
|
|
edit: item.edit,
|
|
delete: item.delete,
|
|
cancel: item.cancel,
|
|
confirm: item.confirm,
|
|
};
|
|
}),
|
|
};
|
|
}
|
|
|
|
get entityTarget(): any {
|
|
return UserModel;
|
|
}
|
|
|
|
get eventTopics(): EventTopics[] {
|
|
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,
|
|
message: `Failed! You have entered an invalid username or password`,
|
|
error: 'Unauthorized',
|
|
});
|
|
}
|
|
}
|