bug
This commit is contained in:
@@ -38,13 +38,22 @@ export class AdminAuthGuard implements CanActivate {
|
||||
|
||||
const token = this.extractTokenFromHeader(request);
|
||||
if (!token) {
|
||||
this.logger.warn('No token provided');
|
||||
this.logger.warn('No token provided', {
|
||||
hasAuthHeader: !!request.headers.authorization,
|
||||
authHeader: request.headers.authorization ? 'present' : 'missing',
|
||||
headers: Object.keys(request.headers),
|
||||
});
|
||||
throw new UnauthorizedException('No token provided');
|
||||
}
|
||||
|
||||
try {
|
||||
// Verify token - JWT module is global, so it uses the configured secret automatically
|
||||
const payload = await this.jwtService.verifyAsync<IAdminTokenPayload>(token);
|
||||
// Get the JWT secret from config
|
||||
const secret = this.configService.getOrThrow<string>('JWT_SECRET');
|
||||
|
||||
// Verify token with the secret
|
||||
const payload = await this.jwtService.verifyAsync<IAdminTokenPayload>(token, {
|
||||
secret,
|
||||
});
|
||||
|
||||
if (!payload.adminId || !payload.restId) {
|
||||
this.logger.error('Invalid token payload structure', payload);
|
||||
@@ -85,16 +94,27 @@ export class AdminAuthGuard implements CanActivate {
|
||||
if (err instanceof ForbiddenException || err instanceof UnauthorizedException) {
|
||||
throw err;
|
||||
}
|
||||
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
|
||||
const errorStack = err instanceof Error ? err.stack : undefined;
|
||||
this.logger.error('Token verification error in AdminAuthGuard', {
|
||||
error: err.message,
|
||||
stack: err.stack,
|
||||
error: errorMessage,
|
||||
stack: errorStack,
|
||||
});
|
||||
throw new UnauthorizedException('Invalid or expired token');
|
||||
}
|
||||
}
|
||||
|
||||
private extractTokenFromHeader(request: Request): string | undefined {
|
||||
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
||||
return type === 'Bearer' ? token : undefined;
|
||||
const authHeader = request.headers.authorization || request.headers['authorization'];
|
||||
if (!authHeader) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const [type, token] = authHeader.split(' ');
|
||||
if (type?.toLowerCase() !== 'bearer' || !token) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user