import { Injectable, UnauthorizedException } from '@nestjs/common'; import { UserEntity } from './models/entities/user.entity'; import { CipherService } from '../helper/cipher.service'; import { PrismaService } from '../helper/prisma.service'; import { JwtScope } from './models/enums/jwt-scope'; import { JwtService } from '@nestjs/jwt'; import { UsersService } from '../../../modules/users/users.service'; @Injectable() export class LoginService { constructor( private readonly prismaService: PrismaService, private readonly cipherService: CipherService, private readonly jwtService: JwtService, private readonly usersService: UsersService, ) {} async validateUser(email: string, password: string): Promise { const user: UserEntity = await this.usersService.getUserByEmail(email); if (!this.cipherService.comparePassword(password, user.password)) throw new UnauthorizedException('Invalid password'); return user; } async isUserVerified(userId: string): Promise { const user: UserEntity = await this.usersService.getUserById(userId); return user.verified; } generateToken(userId: string, userTokenId: string, scope: JwtScope): string { return this.jwtService.sign( { scope, }, { subject: userId, expiresIn: scope !== JwtScope.USAGE ? '5m' : '7d', jwtid: userTokenId, }, ); } }