-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auth controller 추가, 42 access token 발급 (#1)
- Loading branch information
1 parent
3884f22
commit 18f5445
Showing
17 changed files
with
285 additions
and
9 deletions.
There are no files selected for viewing
Submodule api-gateway-config
updated
from d1d4a5 to 630823
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { AuthModule } from './auth/auth.module'; | ||
import { LoggerMiddleware } from './common/middlewares/logger.middleware'; | ||
|
||
@Module({ | ||
imports: [], | ||
imports: [AuthModule], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
}) | ||
export class AppModule {} | ||
export class AppModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(LoggerMiddleware).forRoutes('*'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Injectable, Logger, UnauthorizedException } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
import { FortyTwoUserDto } from './dto/fortytwo-user.dto'; | ||
|
||
@Injectable() | ||
export class AuthFortyTwoService { | ||
private readonly logger = new Logger(AuthFortyTwoService.name); | ||
private readonly baseUrl = 'https://api.intra.42.fr'; | ||
|
||
async getAccessToken(code: string): Promise<string> { | ||
try { | ||
const response = await axios.post( | ||
`${this.baseUrl}/oauth/token`, | ||
{ | ||
grant_type: 'authorization_code', | ||
client_id: process.env.FORTYTWO_CLIENT_ID, | ||
client_secret: process.env.FORTYTWO_CLIEND_SECRET, | ||
code, | ||
redirect_uri: process.env.FORTYTWO_REDIRECT_URI, | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
|
||
return response.data.access_token; | ||
} catch (error) { | ||
this.logger.error(error); | ||
throw new UnauthorizedException('Invalid code'); | ||
} | ||
} | ||
|
||
async getUserData(accessToken: string): Promise<FortyTwoUserDto> { | ||
try { | ||
const response = await axios.get(`${this.baseUrl}/v2/me`, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
const userData: FortyTwoUserDto = { | ||
nickname: response.data.login, | ||
email: response.data.email, | ||
}; | ||
return userData; | ||
} catch (error) { | ||
this.logger.error(error); | ||
throw new UnauthorizedException('Invalid access token'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthFortyTwoService } from './auth-fortytwo.service'; | ||
import { FortyTwoUserDto } from './dto/fortytwo-user.dto'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor( | ||
private readonly authService: AuthService, | ||
private readonly authFortyTwoService: AuthFortyTwoService, | ||
) {} | ||
|
||
@Post('signin') | ||
async signin(@Body() code: string) { | ||
// code를 이용해 access token을 받아온다. | ||
const fortyTwoAccessToken = | ||
await this.authFortyTwoService.getAccessToken(code); | ||
// access token을 이용해 42API에서 유저 정보를 받아온다. | ||
const fortyTwoUserDto: FortyTwoUserDto = | ||
await this.authFortyTwoService.getUserData(fortyTwoAccessToken); | ||
|
||
// 유저 정보를 이용해 유저를 찾는다. | ||
const user = await this.authService.validateUser(fortyTwoUserDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthFortyTwoService } from './auth-fortytwo.service'; | ||
|
||
@Module({ | ||
controllers: [AuthController], | ||
providers: [AuthService, AuthFortyTwoService], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { FortyTwoUserDto } from './dto/fortytwo-user.dto'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
private readonly logger = new Logger(AuthService.name); | ||
|
||
// constructor(private readonly connection: Connection) {} | ||
|
||
async validateUser(userData: FortyTwoUserDto) { | ||
// const queryRunner = this.connection.createQueryRunner(); | ||
// 유저 정보를 이용해 유저를 찾는다. | ||
// const user = await this. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class CreateAuthDto {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export class FortyTwoUserDto { | ||
nickname: string; | ||
email: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { CreateAuthDto } from './create-auth.dto'; | ||
|
||
export class UpdateAuthDto extends PartialType(CreateAuthDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Auth {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
Injectable, | ||
NestInterceptor, | ||
ExecutionContext, | ||
CallHandler, | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class LoggingInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
console.log('Before...'); | ||
|
||
const now = Date.now(); | ||
return next | ||
.handle() | ||
.pipe(tap(() => console.log(`After... ${Date.now() - now}ms`))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
Injectable, | ||
NestInterceptor, | ||
ExecutionContext, | ||
CallHandler, | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
export interface Response<T> { | ||
data: T; | ||
} | ||
|
||
@Injectable() | ||
export class TransformInterceptor<T> | ||
implements NestInterceptor<T, Response<T>> | ||
{ | ||
intercept( | ||
context: ExecutionContext, | ||
next: CallHandler, | ||
): Observable<Response<T>> { | ||
return next.handle().pipe( | ||
map((data) => ({ | ||
success: true, | ||
data, | ||
})), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Injectable, Logger, NestMiddleware } from '@nestjs/common'; | ||
import { NextFunction } from 'express'; | ||
|
||
@Injectable() | ||
export class LoggerMiddleware implements NestMiddleware { | ||
private logger = new Logger('HTTP'); | ||
|
||
use(req: any, res: any, next: NextFunction) { | ||
res.on('finish', () => { | ||
this.logger.log( | ||
`${req.ip} ${req.method} ${res.statusCode}`, | ||
req.originalUrl, | ||
); | ||
}); | ||
|
||
next(); | ||
} | ||
} |