Files
traintrape-moi/server/src/auth/auth.service.ts
2024-12-08 13:41:37 +01:00

26 lines
919 B
TypeScript

import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common'
import { PrismaService } from './../prisma/prisma.service'
import { JwtService } from '@nestjs/jwt'
import { AuthEntity } from './entity/auth.entity'
import * as bcrypt from 'bcrypt'
@Injectable()
export class AuthService {
constructor(private prisma: PrismaService, private jwtService: JwtService) {}
async login(name: string, password: string): Promise<AuthEntity> {
const player = await this.prisma.player.findUnique({ where: { name: name } })
if (!player) {
throw new NotFoundException(`Aucun⋅e joueur⋅se avec pour nom ${name}`)
}
const isPasswordValid = await bcrypt.compare(password, player.password)
if (!isPasswordValid) {
throw new UnauthorizedException('Mot de passe incorrect')
}
return {
accessToken: this.jwtService.sign({ playerId: player.id }),
}
}
}