import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { PrismaService } from '../prisma/prisma.service'; import { JwtPayload } from '../common/auth.types'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(config: ConfigService, private prisma: PrismaService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: config.get('jwt.accessSecret')!, }); } /** Un employé désactivé ou un magasin suspendu perd l'accès immédiatement. */ async validate(payload: JwtPayload): Promise { if (payload.kind === 'staff') { const staff = await this.prisma.staffUser.findUnique({ where: { id: payload.sub }, select: { active: true, store: { select: { status: true } } }, }); if (!staff?.active || staff.store.status === 'SUSPENDED' || staff.store.status === 'CLOSED') { throw new UnauthorizedException(); } } return payload; } }