-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from DevKor-github/refactoring/노정훈
Refactoring/노정훈
- Loading branch information
Showing
12 changed files
with
385 additions
and
332 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class AccessGuard extends AuthGuard('access') {} |
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,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class RefreshGuard extends AuthGuard('refresh') {} |
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,28 +1,5 @@ | ||
import { Injectable, ExecutionContext } from '@nestjs/common'; | ||
import { AuthService } from '../auth.service'; | ||
import { UserService } from 'src/user/user.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { lastValueFrom, Observable } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class SignupGuard extends AuthGuard('signup') { | ||
constructor( | ||
private readonly authService: AuthService, | ||
private readonly userService: UserService, | ||
private readonly reflector: Reflector, | ||
) { | ||
super(reflector); | ||
} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const result = super.canActivate(context); | ||
if (typeof result === 'boolean' || result instanceof Promise) { | ||
return result; | ||
} else if (result instanceof Observable) { | ||
return await lastValueFrom(result); | ||
} else { | ||
throw new Error('Unexpected canActivate return value'); | ||
} | ||
} | ||
} | ||
export class SignupGuard extends AuthGuard('signup') {} |
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,3 @@ | ||
export * from './jwt-access.strategy'; | ||
export * from './jwt-refresh.strategy'; | ||
export * from './jwt-signup.strategy'; |
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,25 +1,18 @@ | ||
import { Injectable, HttpException } from '@nestjs/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Strategy, ExtractJwt } from 'passport-jwt'; | ||
import { JwtPayload } from 'src/interfaces/auth'; | ||
import { UserService } from 'src/user/user.service'; | ||
|
||
@Injectable() | ||
export class JwtAccessStrategy extends PassportStrategy(Strategy, 'access') { | ||
constructor(private readonly userService: UserService) { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: process.env.ACCESS_TOKEN_SECRET_KEY, | ||
}); | ||
} | ||
|
||
async validate(payload: JwtPayload) { | ||
const user = await this.userService.findUserByVal('id', payload.id); | ||
|
||
if (!user || user.id != payload.id) { | ||
throw new HttpException('Invalid access token', 401); | ||
} | ||
|
||
return user; | ||
return payload; | ||
} | ||
} |
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
6 changes: 3 additions & 3 deletions
6
src/decorators/user.decorator.ts → src/decorators/accessUser.decorator.ts
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,10 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { JwtPayload } from 'src/interfaces/auth'; | ||
|
||
export const User = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext) => { | ||
(_: unknown, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
|
||
console.log(data); | ||
return request.user; | ||
return request.user as JwtPayload; | ||
}, | ||
); |
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 { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { SignupPayload } from 'src/interfaces/auth'; | ||
|
||
export const SignupUser = createParamDecorator( | ||
(_: never, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
|
||
return request.user as SignupPayload; | ||
}, | ||
); |
Oops, something went wrong.