From 95191f9b30e35fdc6e358bf0a9613237de572ec5 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 11 Nov 2025 15:57:33 +0330 Subject: [PATCH] auth --- src/modules/auth/auth.controller.ts | 18 ++++---- src/modules/auth/auth.module.ts | 5 +++ src/modules/auth/guards/auth.admin.guard.ts | 50 --------------------- src/modules/auth/guards/auth.guard.ts | 25 +++-------- 4 files changed, 21 insertions(+), 77 deletions(-) delete mode 100644 src/modules/auth/guards/auth.admin.guard.ts diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 5a831da..0d38651 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -28,7 +28,7 @@ export class AuthController { @ApiResponse({ status: 200, description: 'OTP verified successfully' }) @ApiResponse({ status: 400, description: 'Invalid OTP or expired' }) otpVerify(@Body() dto: VerifyOtpDto) { - return this.authService.verifyOtp(dto.phone, dto.otp); // assuming dto has `otp` property + return this.authService.verifyOtp(dto.phone, dto.slug, dto.otp); // assuming dto has `otp` property } @Post('admin/otp/request') @@ -40,14 +40,14 @@ export class AuthController { return this.authService.requestOtpAdmin(dto); } - @Post('admin/otp/verify') - @ApiOperation({ summary: 'Verify OTP code' }) - @ApiBody({ type: VerifyOtpDto, description: 'Mobile number and OTP code' }) - @ApiResponse({ status: 200, description: 'OTP verified successfully' }) - @ApiResponse({ status: 400, description: 'Invalid OTP or expired' }) - adminOtpVerify(@Body() dto: VerifyOtpDto) { - return this.authService.verifyOtpAdmin(dto.phone, dto.otp); // assuming dto has `otp` property - } + // @Post('admin/otp/verify') + // @ApiOperation({ summary: 'Verify OTP code' }) + // @ApiBody({ type: VerifyOtpDto, description: 'Mobile number and OTP code' }) + // @ApiResponse({ status: 200, description: 'OTP verified successfully' }) + // @ApiResponse({ status: 400, description: 'Invalid OTP or expired' }) + // adminOtpVerify(@Body() dto: VerifyOtpDto) { + // return this.authService.verifyOtpAdmin(dto.phone, dto.otp); // assuming dto has `otp` property + // } @RefreshTokenRateLimit() @ApiOperation({ summary: 'refresh the user access token / refresh token' }) diff --git a/src/modules/auth/auth.module.ts b/src/modules/auth/auth.module.ts index 92453f7..f3c21c1 100644 --- a/src/modules/auth/auth.module.ts +++ b/src/modules/auth/auth.module.ts @@ -7,6 +7,9 @@ import { JwtModule } from '@nestjs/jwt'; import { ConfigService } from '@nestjs/config'; import { AdminModule } from '../admin/admin.module'; import { TokensService } from './services/tokens.service'; +import { RestaurantsModule } from '../restaurants/restaurants.module'; +import { MikroOrmModule } from '@mikro-orm/nestjs'; +import { User } from '../users/entities/user.entity'; @Module({ imports: [ @@ -23,6 +26,8 @@ import { TokensService } from './services/tokens.service'; inject: [ConfigService], }), AdminModule, + RestaurantsModule, + MikroOrmModule.forFeature([User]), ], controllers: [AuthController], providers: [AuthService, TokensService], diff --git a/src/modules/auth/guards/auth.admin.guard.ts b/src/modules/auth/guards/auth.admin.guard.ts deleted file mode 100644 index 4d1e7d6..0000000 --- a/src/modules/auth/guards/auth.admin.guard.ts +++ /dev/null @@ -1,50 +0,0 @@ -// guards/jwt-auth.guard.ts -import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'; -import { Request } from 'express'; -import { JwtService } from '@nestjs/jwt'; -import { ConfigService } from '@nestjs/config'; -import { AuthEntityType, AuthRequest, JwtPayload } from './auth.guard'; - -@Injectable() -export class AdminAuthGuard implements CanActivate { - constructor( - private readonly jwtService: JwtService, - private readonly configService: ConfigService, - ) {} - - async canActivate(context: ExecutionContext) { - const request = context.switchToHttp().getRequest(); - - try { - const secret = this.configService.get('JWT_SECRET'); - const token = this.extractTokenFromHeader(request); - - if (!token) { - throw new UnauthorizedException(); - } - - try { - const payload = await this.jwtService.verifyAsync(token, { - secret, - }); - - if (payload.type !== AuthEntityType.ADMIN) { - throw new UnauthorizedException('Access denied. Admin rights required.'); - } - // 💡 We're assigning the payload to the request object here - // so that we can access it in our route handlers - request['user'] = payload; - } catch { - throw new UnauthorizedException(); - } - return true; - } catch (err) { - console.log('error in AuthGuard', err); - 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; - } -} diff --git a/src/modules/auth/guards/auth.guard.ts b/src/modules/auth/guards/auth.guard.ts index 4397f42..7b2dc40 100644 --- a/src/modules/auth/guards/auth.guard.ts +++ b/src/modules/auth/guards/auth.guard.ts @@ -1,22 +1,12 @@ -// guards/jwt-auth.guard.ts import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'; import { Request } from 'express'; import { JwtService } from '@nestjs/jwt'; import { ConfigService } from '@nestjs/config'; +import { ITokenPayload } from '../interfaces/IToken-payload'; -export enum AuthEntityType { - USER = 'user', - ADMIN = 'admin', -} export interface AuthRequest extends Request { - user: { - sub: string; // userId - slug: string; - }; -} -export interface JwtPayload { - sub: string; // The userId - type: AuthEntityType; + userId: string; + slug: string; } @Injectable() @@ -37,13 +27,12 @@ export class AuthGuard implements CanActivate { } try { - const payload = await this.jwtService.verifyAsync(token, { + const payload = await this.jwtService.verifyAsync(token, { secret, }); - if (payload.type !== AuthEntityType.USER) { - throw new UnauthorizedException('Access denied. User rights required.'); - } - request['user'] = payload; + + request['userId'] = payload.id; + request['slug'] = payload.restId; } catch { throw new UnauthorizedException(); }