Compare commits

..

No commits in common. "6ae35efb3ee9d069e0e62c24fa257bc49a38f0da" and "1a21c4c6424e28d20688b7a40b7402d2a55aff86" have entirely different histories.

4 changed files with 19 additions and 27 deletions

View File

@ -1,14 +0,0 @@
export const DEFAULT_JWT_SECRET =
'canina_jwt_fallback_access_secret_32_characters_minimum_len_2026';
export const DEFAULT_JWT_REFRESH_SECRET =
'canina_jwt_fallback_refresh_secret_32_characters_minimum_len_2026';
export const getJwtSecret = (): string =>
process.env.JWT_ACCESS_SECRET ||
process.env.JWT_SECRET ||
DEFAULT_JWT_SECRET;
export const getJwtRefreshSecret = (): string =>
process.env.JWT_REFRESH_SECRET ||
DEFAULT_JWT_REFRESH_SECRET;

View File

@ -5,18 +5,20 @@ import { AuthService } from './auth.service';
import { AuthController } from './auth.controller'; import { AuthController } from './auth.controller';
import { JwtStrategy } from './jwt.strategy'; import { JwtStrategy } from './jwt.strategy';
import { UsersModule } from '../users/users.module'; import { UsersModule } from '../users/users.module';
import { getJwtSecret } from './auth.constants';
const JWT_SECRET =
process.env.JWT_ACCESS_SECRET ||
process.env.JWT_SECRET ||
'canina_jwt_fallback_secret_32_characters_minimum_len_2026';
@Module({ @Module({
imports: [ imports: [
UsersModule, UsersModule,
PassportModule, PassportModule,
JwtModule.registerAsync({ JwtModule.register({
useFactory: () => ({ secret: JWT_SECRET,
secret: getJwtSecret(),
signOptions: { expiresIn: '7d' }, signOptions: { expiresIn: '7d' },
}), }),
}),
], ],
controllers: [AuthController], controllers: [AuthController],
providers: [AuthService, JwtStrategy], providers: [AuthService, JwtStrategy],

View File

@ -2,7 +2,6 @@ import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport'; import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { getJwtSecret } from './auth.constants';
export interface JwtPayload { export interface JwtPayload {
sub: string; sub: string;
@ -14,10 +13,15 @@ export interface JwtPayload {
@Injectable() @Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) { export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly usersService: UsersService) { constructor(private readonly usersService: UsersService) {
const secret =
process.env.JWT_ACCESS_SECRET ||
process.env.JWT_SECRET ||
'canina_jwt_fallback_secret_32_characters_minimum_len_2026';
super({ super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false, ignoreExpiration: false,
secretOrKey: getJwtSecret(), secretOrKey: secret,
}); });
} }

View File

@ -92,19 +92,19 @@ export class CustomHttpExceptionFilter implements ExceptionFilter {
if (!msg) return 'خطای نامشخص رخ داده است.'; if (!msg) return 'خطای نامشخص رخ داده است.';
const lower = msg.toLowerCase(); const lower = msg.toLowerCase();
if (lower === 'unauthorized' || msg === 'Unauthorized') { if (lower.includes('unauthorized') || status === 401) {
return 'شما دسترسی لازم برای این عملیات را ندارید. لطفاً ابتدا وارد حساب کاربری خود شوید.'; return 'شما دسترسی لازم برای این عملیات را ندارید. لطفاً ابتدا وارد حساب کاربری خود شوید.';
} }
if (lower === 'forbidden' || msg === 'Forbidden') { if (lower.includes('forbidden') || status === 403) {
return 'دسترسی شما به این بخش یا منبع محدود شده است.'; return 'دسترسی شما به این بخش یا منبع محدود شده است.';
} }
if (lower === 'not found' || msg === 'Not Found') { if (lower.includes('not found') || status === 404) {
return 'مورد یا صفحه درخواستی یافت نشد.'; return 'مورد یا صفحه درخواستی یافت نشد.';
} }
if (lower === 'bad request' || msg === 'Bad Request') { if (status === 400) {
return 'درخواست نامعتبر است. لطفاً پارامترهای ورودی را چک کنید.'; return 'درخواست نامعتبر است. لطفاً پارامترهای ورودی را چک کنید.';
} }
if (lower === 'too many requests' || msg === 'ThrottlerException: Too Many Requests') { if (status === 429) {
return 'تعداد درخواست‌های شما بیش از حد مجاز است. لطفاً کمی صبر کرده و مجدداً تلاش کنید.'; return 'تعداد درخواست‌های شما بیش از حد مجاز است. لطفاً کمی صبر کرده و مجدداً تلاش کنید.';
} }