diff --git a/src/common/guards/auth.guard.ts b/src/common/guards/auth.guard.ts index 0260dce..10b60a9 100644 --- a/src/common/guards/auth.guard.ts +++ b/src/common/guards/auth.guard.ts @@ -5,42 +5,37 @@ import { Request } from 'express'; import { IS_PUBLIC_KEY } from "../decorators/public.decorator"; import { ConfigService } from "@nestjs/config"; -interface AuthenticatedRequest extends Request { - user?: any; -} - @Injectable() export class AuthGuard implements CanActivate { - constructor( - private readonly jwtService: JwtService, - private readonly reflector: Reflector, - private readonly configService: ConfigService, - ) { } + constructor( + private readonly jwtService: JwtService, + private readonly reflector: Reflector, + private readonly configService: ConfigService, + ) {} + async canActivate(context: ExecutionContext): Promise { + const isPublic = this.reflector.getAllAndOverride(IS_PUBLIC_KEY, [ + context.getHandler(), + context.getClass(), + ]); + if (isPublic) return true; - async canActivate(context: ExecutionContext): Promise { - const isPublic = this.reflector.getAllAndOverride(IS_PUBLIC_KEY, [ - context.getHandler(), - context.getClass(), - ]); - if (isPublic) return true; + const request = context.switchToHttp().getRequest(); + const authHeader = request.headers['authorization'] as string | undefined; - const request = context.switchToHttp().getRequest(); - const authHeader = request.headers['authorization'] as string | undefined; - - if (!authHeader || !authHeader.startsWith('Bearer ')) { - throw new UnauthorizedException('Token manquant ou invalide'); - } - - const token = authHeader.split(' ')[1]; - try { - const payload = await this.jwtService.verifyAsync(token, - { secret: this.configService.get('jwt.secret') }, - ); - request.user = payload; - return true; - } catch (error) { - throw new UnauthorizedException('Token invalide ou expire'); - } + if (!authHeader || !authHeader.startsWith('Bearer ')) { + throw new UnauthorizedException('Token manquant ou invalide'); } -} \ No newline at end of file + + const token = authHeader.split(' ')[1]; + try { + const payload = await this.jwtService.verifyAsync(token, { + secret: this.configService.get('jwt.accessSecret'), // ✅ corrige ici + }); + request.user = payload; + return true; + } catch (error) { + throw new UnauthorizedException('Token invalide ou expiré'); + } + } +}