From 0c32985ce2edc32b085c90d303329c80b2ac5317 Mon Sep 17 00:00:00 2001
From: parsa aghaei
Date: Sun, 16 Aug 2026 15:11:05 +0330
Subject: [PATCH] fix(auth): centralize JWT secret with registerAsync to ensure
consistent signing and verification
---
backend/src/auth/auth.constants.ts | 14 ++++++++++++++
backend/src/auth/auth.module.ts | 14 ++++++--------
backend/src/auth/jwt.strategy.ts | 8 ++------
3 files changed, 22 insertions(+), 14 deletions(-)
create mode 100644 backend/src/auth/auth.constants.ts
diff --git a/backend/src/auth/auth.constants.ts b/backend/src/auth/auth.constants.ts
new file mode 100644
index 00000000..f2865302
--- /dev/null
+++ b/backend/src/auth/auth.constants.ts
@@ -0,0 +1,14 @@
+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;
diff --git a/backend/src/auth/auth.module.ts b/backend/src/auth/auth.module.ts
index 98a698ed..594ff487 100644
--- a/backend/src/auth/auth.module.ts
+++ b/backend/src/auth/auth.module.ts
@@ -5,19 +5,17 @@ import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { JwtStrategy } from './jwt.strategy';
import { UsersModule } from '../users/users.module';
-
-const JWT_SECRET =
- process.env.JWT_ACCESS_SECRET ||
- process.env.JWT_SECRET ||
- 'canina_jwt_fallback_secret_32_characters_minimum_len_2026';
+import { getJwtSecret } from './auth.constants';
@Module({
imports: [
UsersModule,
PassportModule,
- JwtModule.register({
- secret: JWT_SECRET,
- signOptions: { expiresIn: '7d' },
+ JwtModule.registerAsync({
+ useFactory: () => ({
+ secret: getJwtSecret(),
+ signOptions: { expiresIn: '7d' },
+ }),
}),
],
controllers: [AuthController],
diff --git a/backend/src/auth/jwt.strategy.ts b/backend/src/auth/jwt.strategy.ts
index 2c9da8e0..bd827add 100644
--- a/backend/src/auth/jwt.strategy.ts
+++ b/backend/src/auth/jwt.strategy.ts
@@ -2,6 +2,7 @@ import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UsersService } from '../users/users.service';
+import { getJwtSecret } from './auth.constants';
export interface JwtPayload {
sub: string;
@@ -13,15 +14,10 @@ export interface JwtPayload {
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
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({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
- secretOrKey: secret,
+ secretOrKey: getJwtSecret(),
});
}