authguard corrected
This commit is contained in:
parent
b93f935564
commit
2ff4711bf6
@ -5,42 +5,37 @@ import { Request } from 'express';
|
|||||||
import { IS_PUBLIC_KEY } from "../decorators/public.decorator";
|
import { IS_PUBLIC_KEY } from "../decorators/public.decorator";
|
||||||
import { ConfigService } from "@nestjs/config";
|
import { ConfigService } from "@nestjs/config";
|
||||||
|
|
||||||
interface AuthenticatedRequest extends Request {
|
|
||||||
user?: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuard implements CanActivate {
|
export class AuthGuard implements CanActivate {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly jwtService: JwtService,
|
private readonly jwtService: JwtService,
|
||||||
private readonly reflector: Reflector,
|
private readonly reflector: Reflector,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||||
|
context.getHandler(),
|
||||||
|
context.getClass(),
|
||||||
|
]);
|
||||||
|
if (isPublic) return true;
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
const request = context.switchToHttp().getRequest<Request>();
|
||||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
const authHeader = request.headers['authorization'] as string | undefined;
|
||||||
context.getHandler(),
|
|
||||||
context.getClass(),
|
|
||||||
]);
|
|
||||||
if (isPublic) return true;
|
|
||||||
|
|
||||||
const request = context.switchToHttp().getRequest<Request>();
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||||
const authHeader = request.headers['authorization'] as string | undefined;
|
throw new UnauthorizedException('Token manquant ou invalide');
|
||||||
|
|
||||||
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<string>('jwt.secret') },
|
|
||||||
);
|
|
||||||
request.user = payload;
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
throw new UnauthorizedException('Token invalide ou expire');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
const token = authHeader.split(' ')[1];
|
||||||
|
try {
|
||||||
|
const payload = await this.jwtService.verifyAsync(token, {
|
||||||
|
secret: this.configService.get<string>('jwt.accessSecret'), // ✅ corrige ici
|
||||||
|
});
|
||||||
|
request.user = payload;
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw new UnauthorizedException('Token invalide ou expiré');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user