AnhLedger commited on
Commit
5ec5d67
·
1 Parent(s): 4d9429b

fix: Get JWT_KEY from .env

Browse files
backend/src/modules/authentication/authentication.guard.ts CHANGED
@@ -5,15 +5,17 @@ import {
5
  UnauthorizedException,
6
  } from '@nestjs/common';
7
  import { JwtService } from '@nestjs/jwt';
8
- import { jwtConstants } from './constants.js';
9
  import { Request } from 'express';
10
  import { Reflector } from '@nestjs/core';
11
  import { IS_PUBLIC_KEY } from './authentication.decorator.js';
 
 
12
  @Injectable()
13
  export class AuthenticationGuard implements CanActivate {
14
  constructor(
15
  private jwtService: JwtService,
16
  private reflector: Reflector,
 
17
  ) {}
18
 
19
  async canActivate(context: ExecutionContext): Promise<boolean> {
@@ -33,7 +35,7 @@ export class AuthenticationGuard implements CanActivate {
33
  }
34
  try {
35
  const payload = await this.jwtService.verifyAsync(token, {
36
- secret: jwtConstants.secret,
37
  });
38
  // 💡 We're assigning the payload to the request object here
39
  // so that we can access it in our route handlers
 
5
  UnauthorizedException,
6
  } from '@nestjs/common';
7
  import { JwtService } from '@nestjs/jwt';
 
8
  import { Request } from 'express';
9
  import { Reflector } from '@nestjs/core';
10
  import { IS_PUBLIC_KEY } from './authentication.decorator.js';
11
+ import { ConfigService } from '@nestjs/config';
12
+ import { buffer } from 'stream/consumers';
13
  @Injectable()
14
  export class AuthenticationGuard implements CanActivate {
15
  constructor(
16
  private jwtService: JwtService,
17
  private reflector: Reflector,
18
+ private configService: ConfigService
19
  ) {}
20
 
21
  async canActivate(context: ExecutionContext): Promise<boolean> {
 
35
  }
36
  try {
37
  const payload = await this.jwtService.verifyAsync(token, {
38
+ secret: this.configService.get<String>('JWT_KEY') as string,
39
  });
40
  // 💡 We're assigning the payload to the request object here
41
  // so that we can access it in our route handlers
backend/src/modules/authentication/authentication.module.ts CHANGED
@@ -3,16 +3,19 @@ import { AuthenticationService } from './authentication.service.js';
3
  import { UserModule } from '../user/user.module.js';
4
  import { JwtModule } from '@nestjs/jwt';
5
  import { AuthenticationController } from './authentication.controller.js';
6
- import { jwtConstants } from './constants.js';
7
  import { AuthenticationGuard } from './authentication.guard.js';
8
  import { APP_GUARD } from '@nestjs/core';
 
9
  @Module({
10
  imports: [
11
  UserModule,
12
- JwtModule.register({
13
- global: true,
14
- secret: jwtConstants.secret,
15
- signOptions: { expiresIn: '90s' },
 
 
 
16
  }),
17
  ],
18
  providers: [
@@ -25,9 +28,4 @@ import { APP_GUARD } from '@nestjs/core';
25
  controllers: [AuthenticationController],
26
  exports: [AuthenticationService],
27
  })
28
- export class AuthenticationModule {
29
- constructor() {
30
- console.log('JWT Secret:', process.env.DB_USER);
31
- console.log('JWT Secret:', process.env.JWT_KEY);
32
- }
33
- }
 
3
  import { UserModule } from '../user/user.module.js';
4
  import { JwtModule } from '@nestjs/jwt';
5
  import { AuthenticationController } from './authentication.controller.js';
 
6
  import { AuthenticationGuard } from './authentication.guard.js';
7
  import { APP_GUARD } from '@nestjs/core';
8
+ import { ConfigModule, ConfigService } from '@nestjs/config';
9
  @Module({
10
  imports: [
11
  UserModule,
12
+ JwtModule.registerAsync({
13
+ imports: [ConfigModule], // Nhập ConfigModule để sử dụng ConfigService
14
+ useFactory: async (configService: ConfigService) => ({
15
+ secret: configService.get<string>('JWT_KEY'), // Lấy giá trị từ biến môi trường
16
+ signOptions: { expiresIn: '60s' }, // Thời gian hết hạn token
17
+ }),
18
+ inject: [ConfigService], // Inject ConfigService vào factory
19
  }),
20
  ],
21
  providers: [
 
28
  controllers: [AuthenticationController],
29
  exports: [AuthenticationService],
30
  })
31
+ export class AuthenticationModule {}