108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import { Injectable, BadRequestException } from '@nestjs/common';
|
|
import { RequestOtpDto } from '../dto/request-otp.dto';
|
|
import { SmsService } from '../../notification/services/sms.service';
|
|
import { UserService } from '../../user/providers/user.service';
|
|
import { randomInt } from 'crypto';
|
|
import { TokensService } from './tokens.service';
|
|
import { AuthMessage } from 'src/common/enums/message.enum';
|
|
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
|
import { AdminLoginTransformer } from '../transformers/admin-login.transformer';
|
|
import { UserLoginTransformer } from '../transformers/user-login.transformer';
|
|
import { normalizePhone } from '../../util/phone.util';
|
|
import { OtpService } from './otp.service';
|
|
|
|
@Injectable()
|
|
export class AuthService {
|
|
readonly ADMIN_PERMISSIONS_KEY = 'admin-perms';
|
|
|
|
constructor(
|
|
private readonly smsService: SmsService,
|
|
private readonly userService: UserService,
|
|
private readonly tokensService: TokensService,
|
|
private readonly adminRepository: AdminRepository,
|
|
private readonly otpService: OtpService,
|
|
) {
|
|
|
|
}
|
|
|
|
async requestOtp(dto: RequestOtpDto, isAdmin: boolean) {
|
|
const { phone } = dto;
|
|
const normalizedPhone = normalizePhone(phone);
|
|
|
|
if (isAdmin) {
|
|
await this.valdateAdmin(normalizedPhone)
|
|
}
|
|
|
|
const code = this.generateOtpCode();
|
|
|
|
await this.otpService.set(normalizedPhone, code)
|
|
|
|
// await this.smsService.sendotp(normalizedPhone, code);
|
|
|
|
return { code };
|
|
}
|
|
|
|
async verifyOtp(phone: string, code: string) {
|
|
const normalizedPhone = normalizePhone(phone);
|
|
|
|
const cachedCode = await this.otpService.get(normalizedPhone);
|
|
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
|
|
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
|
|
|
|
await this.otpService.delete(normalizedPhone)
|
|
|
|
const user = await this.userService.getOrCreate({
|
|
phone: normalizedPhone,
|
|
gender: true,
|
|
maxCredit: 0
|
|
});
|
|
|
|
|
|
const tokens = await this.tokensService.generateAccessAndRefreshToken(user.id, false);
|
|
|
|
const userResponse = UserLoginTransformer.transform(user);
|
|
|
|
return { tokens, user: userResponse };
|
|
}
|
|
|
|
async verifyOtpAdmin(phone: string, code: string) {
|
|
const normalizedPhone = normalizePhone(phone);
|
|
|
|
const cachedCode = await this.otpService.get(normalizedPhone);
|
|
|
|
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
|
|
|
|
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
|
|
await this.otpService.delete(normalizedPhone);
|
|
|
|
const admin = await this.adminRepository.findOne({ phone: normalizedPhone }, { populate: ['role', 'role.permissions'] });
|
|
|
|
if (!admin) {
|
|
throw new BadRequestException(AuthMessage.ADMIN_NOT_FOUND);
|
|
}
|
|
|
|
const tokens = await this.tokensService.generateAccessAndRefreshToken(admin.id, true);
|
|
|
|
const adminResponse = await AdminLoginTransformer.transform(admin);
|
|
|
|
return { tokens, admin: adminResponse };
|
|
}
|
|
|
|
private generateOtpCode(): string {
|
|
const code = randomInt(10000, 100000);
|
|
return code.toString();
|
|
}
|
|
|
|
refreshToken(oldRefreshToken: string) {
|
|
return this.tokensService.refreshToken(oldRefreshToken);
|
|
}
|
|
|
|
private async valdateAdmin(phone: string) {
|
|
const admin = await this.adminRepository.findOne({ phone })
|
|
if (!admin) {
|
|
throw new BadRequestException('Admin Not Found')
|
|
}
|
|
}
|
|
|
|
}
|