bracket-backend/src/common/modules/auth/login.service.ts
unurled 418a9e2c7a
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 1m8s
fix eslint errors
2025-02-07 20:24:16 +01:00

42 lines
1.4 KiB
TypeScript

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<UserEntity> {
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<boolean> {
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,
},
);
}
}