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:
@@ -10,11 +10,23 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
canActivate(context: ExecutionContext) {
|
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, [
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||||
context.getHandler(),
|
context.getHandler(),
|
||||||
context.getClass(),
|
context.getClass(),
|
||||||
]);
|
]);
|
||||||
|
console.log(`[JwtAuthGuard.canActivate] isPublic:`, isPublic);
|
||||||
|
|
||||||
if (isPublic) return true;
|
if (isPublic) return true;
|
||||||
return super.canActivate(context);
|
|
||||||
|
const result = super.canActivate(context);
|
||||||
|
console.log(`[JwtAuthGuard.canActivate] super.canActivate result:`, result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async validate(payload: { sub: number; username: string; role: string; isPremium: boolean }) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,8 +127,8 @@ export class ProductsController {
|
|||||||
|
|
||||||
@Roles('admin')
|
@Roles('admin')
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() body: CreateProductDto) {
|
create(@Body() body: CreateProductDto, @Request() req: any) {
|
||||||
return this.productsService.create(body);
|
console.log('[ProductsController.create] Request received');\n console.log('[ProductsController.create] User:', req.user);\n console.log('[ProductsController.create] Body:', body);\n return this.productsService.create(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('pending')
|
@Post('pending')
|
||||||
|
|||||||
Reference in New Issue
Block a user