Skip to content

Commit 49b0cef

Browse files
feat: jwt 토큰 decode 후 access 허용
1 parent 8dfdc50 commit 49b0cef

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

apps/backend/src/auth/auth.module.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,30 @@ import { AuthService } from './auth.service';
55
import { AuthController } from './auth.controller';
66
import { NaverStrategy } from './strategies/naver.strategy';
77
import { KakaoStrategy } from './strategies/kakao.strategy';
8+
import { JwtModule } from '@nestjs/jwt';
9+
import { JwtAuthGuard } from './guards/jwt-auth.guard';
10+
import { ConfigModule, ConfigService } from '@nestjs/config';
11+
812
@Module({
9-
imports: [UserModule],
13+
imports: [
14+
UserModule,
15+
ConfigModule.forRoot({ isGlobal: true }),
16+
JwtModule.registerAsync({
17+
imports: [ConfigModule],
18+
inject: [ConfigService],
19+
useFactory: async (configService: ConfigService) => ({
20+
secret: configService.get<string>('JWT_SECRET'),
21+
signOptions: { expiresIn: '1h' },
22+
}),
23+
}),
24+
],
1025
controllers: [AuthController],
11-
providers: [AuthService, NaverStrategy, KakaoStrategy, UserRepository],
26+
providers: [
27+
AuthService,
28+
NaverStrategy,
29+
KakaoStrategy,
30+
UserRepository,
31+
JwtAuthGuard,
32+
],
1233
})
1334
export class AuthModule {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
2+
import { JwtService } from '@nestjs/jwt';
3+
import { LoginRequiredException } from '../../exception/login.exception';
4+
import { InvalidTokenException } from 'src/exception/invalid.exception';
5+
6+
@Injectable()
7+
export class JwtAuthGuard implements CanActivate {
8+
constructor(private readonly jwtService: JwtService) {}
9+
10+
async canActivate(context: ExecutionContext): Promise<boolean> {
11+
const request = context.switchToHttp().getRequest();
12+
const authorizationHeader = request.headers['authorization'];
13+
14+
if (!authorizationHeader) {
15+
console.log('Authorization header missing');
16+
throw new LoginRequiredException();
17+
}
18+
19+
const token = authorizationHeader.split(' ')[1];
20+
21+
try {
22+
const decodedToken = this.jwtService.verify(token, {
23+
secret: process.env.JWT_SECRET,
24+
});
25+
request.user = decodedToken;
26+
return true;
27+
} catch (error) {
28+
console.log('Invalid token');
29+
throw new InvalidTokenException();
30+
}
31+
}
32+
}

apps/backend/src/auth/guards/jwtauth.guards.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ForbiddenException } from '@nestjs/common';
2+
3+
export class InvalidTokenException extends ForbiddenException {
4+
constructor() {
5+
super(`유효하지 않은 JWT 토큰입니다.`);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ForbiddenException } from '@nestjs/common';
2+
3+
export class LoginRequiredException extends ForbiddenException {
4+
constructor() {
5+
super(`로그인이 필요한 서비스입니다.`);
6+
}
7+
}

apps/backend/src/exception/user.exception.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)