feat(auth): add detailed logging in JwtAuthGuard and JwtStrategy for better debugging

feat(products): enhance logging in create method to track requests and user details
This commit is contained in:
Nils-Johan Gynther
2026-04-19 17:08:14 +02:00
parent 6c4683242c
commit 3ad634f348
3 changed files with 19 additions and 4 deletions
+13 -1
View File
@@ -10,11 +10,23 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
}
canActivate(context: ExecutionContext) {
const request = context.switchToHttp().getRequest();
const authHeader = request.headers.authorization;
const path = request.path;
const method = request.method;
console.log(`[JwtAuthGuard.canActivate] ${method} ${path}`);
console.log(`[JwtAuthGuard.canActivate] Authorization header:`, authHeader ? 'YES' : 'NO');
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass(),
]);
console.log(`[JwtAuthGuard.canActivate] isPublic:`, isPublic);
if (isPublic) return true;
return super.canActivate(context);
const result = super.canActivate(context);
console.log(`[JwtAuthGuard.canActivate] super.canActivate result:`, result);
return result;
}
}
+4 -1
View File
@@ -13,6 +13,9 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
}
async validate(payload: { sub: number; username: string; role: string; isPremium: boolean }) {
return { userId: payload.sub, username: payload.username, role: payload.role ?? 'user', isPremium: payload.isPremium ?? false };
console.log('[JwtStrategy.validate] Payload received:', payload);
const result = { userId: payload.sub, username: payload.username, role: payload.role ?? 'user', isPremium: payload.isPremium ?? false };
console.log('[JwtStrategy.validate] Returning user:', result);
return result;
}
}