Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#61] refreshToken 보내면 새로운 token 들 반환 #62

Merged
merged 12 commits into from
Apr 16, 2024
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GradeController } from './grade/grade.controller';
import { GradeService } from './grade/grade.service';
import { GradeModule } from './grade/grade.module';
import { RankModule } from './rank/rank.module';
import { LoginModule } from './login/login.module';
import * as process from 'process';

@Module({
Expand All @@ -29,6 +30,7 @@ import * as process from 'process';
GithubModule,
GradeModule,
RankModule,
LoginModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
12 changes: 12 additions & 0 deletions src/login/login.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Body, Controller, Post } from '@nestjs/common';
import { LoginService } from './login.service';

@Controller('/api')
export class LoginController {
constructor(private loginService: LoginService) {}

@Post('login/refresh')
public refreshTokens(@Body('refreshToken') refreshToken: string) {
return this.loginService.sendTokens(refreshToken);
}
}
13 changes: 13 additions & 0 deletions src/login/login.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { LoginController } from './login.controller';
import { LoginService } from './login.service';
import { JwtModule } from '@nestjs/jwt';
import { JwtConfig } from '../Config/jwt.config';
import { JwtStrategy } from '../auth/strategy/jwt.strategy';

@Module({
imports: [JwtModule.registerAsync({ useClass: JwtConfig })],
controllers: [LoginController],
providers: [LoginService, JwtStrategy],
})
export class LoginModule {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auth module에 합치는게 더 낫지 않을까 생각됩니다. 중복되는 코드도 많고 그래서요

56 changes: 56 additions & 0 deletions src/login/login.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { TokenExpiredError } from 'jsonwebtoken';

@Injectable()
export class LoginService {
constructor(
private jwtService: JwtService,
private configService: ConfigService,
) {}

public sendTokens(refreshToken: string) {
const payload = this.validateRefreshToken(refreshToken);

return {
accessToken: this.generateAccessToken(payload.userId),
refreshToken: this.generateRefreshToken(payload.userId),
};
}

public generateAccessToken(userId: string): string {
return this.jwtService.sign(
{
userId: userId,
},
{
secret: this.configService.get('JWT_ACCESS_SECRET'),
expiresIn: this.configService.get('JWT_ACCESS_EXPIRES_IN'),
},
);
}

public generateRefreshToken(userId: string): string {
return this.jwtService.sign(
{
userId: userId,
},
{
secret: this.configService.get('JWT_REFRESH_SECRET'),
expiresIn: this.configService.get('JWT_REFRESH_EXPIRES_IN'),
},
);
}
public validateRefreshToken(refreshToken: string) {
try {
return this.jwtService.verify(refreshToken, {
secret: this.configService.get('JWT_REFRESH_SECRET'),
});
} catch (e) {
if (e instanceof TokenExpiredError) {
throw new UnauthorizedException();
}
}
}
}
Loading