User => Player
This commit is contained in:
@ -5,7 +5,7 @@ import { PrismaModule } from 'src/prisma/prisma.module'
|
||||
import { PassportModule } from '@nestjs/passport'
|
||||
import { JwtModule } from '@nestjs/jwt'
|
||||
import { env } from 'process'
|
||||
import { UsersModule } from 'src/users/users.module'
|
||||
import { PlayersModule } from 'src/players/players.module'
|
||||
import { JwtStrategy } from './jwt.strategy'
|
||||
|
||||
export const JWT_SECRET = env.JWT_SECRET
|
||||
@ -18,7 +18,7 @@ export const JWT_SECRET = env.JWT_SECRET
|
||||
secret: JWT_SECRET,
|
||||
signOptions: { expiresIn: '12h' },
|
||||
}),
|
||||
UsersModule,
|
||||
PlayersModule,
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, JwtStrategy],
|
||||
|
@ -9,17 +9,17 @@ export class AuthService {
|
||||
constructor(private prisma: PrismaService, private jwtService: JwtService) {}
|
||||
|
||||
async login(name: string, password: string): Promise<AuthEntity> {
|
||||
const user = await this.prisma.user.findUnique({ where: { name: name } })
|
||||
if (!user) {
|
||||
throw new NotFoundException(`Aucun⋅e utilisateur⋅rice avec pour nom ${name}`)
|
||||
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, user.password)
|
||||
const isPasswordValid = await bcrypt.compare(password, player.password)
|
||||
if (!isPasswordValid) {
|
||||
throw new UnauthorizedException('Mot de passe incorrect')
|
||||
}
|
||||
|
||||
return {
|
||||
accessToken: this.jwtService.sign({ userId: user.id }),
|
||||
accessToken: this.jwtService.sign({ playerId: player.id }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { AuthGuard } from '@nestjs/passport'
|
||||
import { User } from '@prisma/client'
|
||||
import { Player } from '@prisma/client'
|
||||
import { Request } from 'express'
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
||||
|
||||
export type AuthenticatedRequest = Request & { user: User }
|
||||
export type AuthenticatedRequest = Request & { user: Player }
|
||||
|
@ -2,23 +2,24 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'
|
||||
import { PassportStrategy } from '@nestjs/passport'
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt'
|
||||
import { JWT_SECRET } from './auth.module'
|
||||
import { UsersService } from 'src/users/users.service'
|
||||
import { User } from '@prisma/client'
|
||||
import { PlayersService } from 'src/players/players.service'
|
||||
import { Player } from '@prisma/client'
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
constructor(private usersService: UsersService) {
|
||||
constructor(private playersService: PlayersService) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: JWT_SECRET,
|
||||
})
|
||||
}
|
||||
|
||||
async validate(payload: { userId: number }): Promise<User> {
|
||||
const user = await this.usersService.findOne(payload.userId)
|
||||
if (!user) {
|
||||
async validate(payload: { playerId: number }): Promise<Player> {
|
||||
console.log(payload)
|
||||
const player = await this.playersService.findOne(payload.playerId)
|
||||
if (!player) {
|
||||
throw new UnauthorizedException()
|
||||
}
|
||||
return user
|
||||
return player
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user