This commit is contained in:
2025-11-19 00:44:11 +03:30
parent ef87a1ae6d
commit bf5b50a3a2
2 changed files with 38 additions and 27 deletions
+27 -7
View File
@@ -38,13 +38,22 @@ export class AdminAuthGuard implements CanActivate {
const token = this.extractTokenFromHeader(request); const token = this.extractTokenFromHeader(request);
if (!token) { 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'); throw new UnauthorizedException('No token provided');
} }
try { try {
// Verify token - JWT module is global, so it uses the configured secret automatically // Get the JWT secret from config
const payload = await this.jwtService.verifyAsync<IAdminTokenPayload>(token); 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) { if (!payload.adminId || !payload.restId) {
this.logger.error('Invalid token payload structure', payload); this.logger.error('Invalid token payload structure', payload);
@@ -85,16 +94,27 @@ export class AdminAuthGuard implements CanActivate {
if (err instanceof ForbiddenException || err instanceof UnauthorizedException) { if (err instanceof ForbiddenException || err instanceof UnauthorizedException) {
throw err; 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', { this.logger.error('Token verification error in AdminAuthGuard', {
error: err.message, error: errorMessage,
stack: err.stack, stack: errorStack,
}); });
throw new UnauthorizedException('Invalid or expired token'); throw new UnauthorizedException('Invalid or expired token');
} }
} }
private extractTokenFromHeader(request: Request): string | undefined { private extractTokenFromHeader(request: Request): string | undefined {
const [type, token] = request.headers.authorization?.split(' ') ?? []; const authHeader = request.headers.authorization || request.headers['authorization'];
return type === 'Bearer' ? token : undefined; if (!authHeader) {
return undefined;
}
const [type, token] = authHeader.split(' ');
if (type?.toLowerCase() !== 'bearer' || !token) {
return undefined;
}
return token;
} }
} }
@@ -19,35 +19,30 @@ import { RestId } from 'src/common/decorators';
@UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
@ApiBearerAuth() @ApiBearerAuth()
@ApiTags('categories') @ApiTags('admin/categories')
@Controller() @Controller('admin/categories')
export class CategoryController { export class CategoryController {
constructor(private readonly categoryService: CategoryService) {} constructor(private readonly categoryService: CategoryService) {}
@ApiBearerAuth('/admin/categories')
@Post()
@ApiOperation({ summary: 'Create category' }) @ApiOperation({ summary: 'Create category' })
@ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto }) @ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
@ApiBody({ type: CreateCategoryDto }) @ApiBody({ type: CreateCategoryDto })
@Post()
create(@Body() dto: CreateCategoryDto) { create(@Body() dto: CreateCategoryDto) {
return this.categoryService.create(dto); return this.categoryService.create(dto);
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Get()
@ApiOperation({ summary: 'my restaurant categories' }) @ApiOperation({ summary: 'my restaurant categories' })
@ApiOkResponse({ description: 'List of categories' }) @ApiOkResponse({ description: 'List of categories' })
@ApiQuery({ name: 'restId', required: false, type: String }) @ApiQuery({ name: 'restId', required: false, type: String })
@ApiQuery({ name: 'isActive', required: false, type: Boolean }) @ApiQuery({ name: 'isActive', required: false, type: Boolean })
@Get()
findAll(@RestId() restId: string, @Query('isActive') isActive?: string) { findAll(@RestId() restId: string, @Query('isActive') isActive?: string) {
// convert isActive from query string to boolean when provided // convert isActive from query string to boolean when provided
const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined; const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined;
return this.categoryService.findAll({ restId, isActive: isActiveBool }); return this.categoryService.findAll({ restId, isActive: isActiveBool });
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get category' }) @ApiOperation({ summary: 'Get category' })
@ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto }) @ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
@@ -56,8 +51,6 @@ export class CategoryController {
return this.categoryService.findOne(restId, id); return this.categoryService.findOne(restId, id);
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Patch(':id') @Patch(':id')
@ApiOperation({ summary: 'Update category' }) @ApiOperation({ summary: 'Update category' })
@ApiParam({ name: 'id' }) @ApiParam({ name: 'id' })
@@ -67,8 +60,6 @@ export class CategoryController {
return this.categoryService.update(restId, id, dto); return this.categoryService.update(restId, id, dto);
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete category' }) @ApiOperation({ summary: 'Delete category' })
@ApiParam({ name: 'id' }) @ApiParam({ name: 'id' })
@@ -77,11 +68,11 @@ export class CategoryController {
return this.categoryService.remove(restId, id); return this.categoryService.remove(restId, id);
} }
@Get('restaurant/:restId') // @Get('restaurant/:restId')
@ApiOperation({ summary: 'Restaurant categories for user' }) // @ApiOperation({ summary: 'Restaurant categories for user' })
@ApiOkResponse({ description: 'List of categories' }) // @ApiOkResponse({ description: 'List of categories' })
@ApiParam({ name: 'restId', required: true, type: String }) // @ApiParam({ name: 'restId', required: true, type: String })
findAllForRestaurant(@Param('restId') restId: string) { // findAllForRestaurant(@Param('restId') restId: string) {
return this.categoryService.findRestaurantCategories(restId); // return this.categoryService.findRestaurantCategories(restId);
} // }
} }