This commit is contained in:
2025-11-24 18:39:57 +03:30
parent 9996ea3b09
commit 2de08b9e65
7 changed files with 44 additions and 33 deletions
+8 -5
View File
@@ -4,8 +4,8 @@ import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { ITokenPayload } from '../interfaces/IToken-payload';
export interface AdminAuthRequest extends Request {
adminId: string;
export interface UserAuthRequest extends Request {
userId: string;
restId: string;
}
@@ -21,10 +21,10 @@ export class AuthGuard implements CanActivate {
) {}
async canActivate(context: ExecutionContext) {
const request = context.switchToHttp().getRequest<AdminAuthRequest>();
const request = context.switchToHttp().getRequest<UserAuthRequest>();
try {
const secret = this.configService.get<string>('JWT_SECRET');
const secret = this.configService.getOrThrow<string>('JWT_SECRET');
const token = this.extractTokenFromHeader(request);
if (!token) {
throw new UnauthorizedException();
@@ -46,7 +46,10 @@ export class AuthGuard implements CanActivate {
return true;
} catch (err) {
console.log('error in AuthGuard', err);
if (err instanceof UnauthorizedException) {
throw err;
}
this.logger.error('error in AuthGuard', err);
throw new UnauthorizedException('Invalid or expired token');
}
}