-
Notifications
You must be signed in to change notification settings - Fork 0
๐จfeat : ์ ์ ๊ถํ ๋ฐ ์ธ์ฆ ๋ก์ง ์ถ๊ฐ #5
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
Open
raymondanythings
wants to merge
5
commits into
master
Choose a base branch
from
feature/#5-JWT-MODULE
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30848a2
๐งชtest : jwt module rsa ์๋
raymondanythings a39b141
๐งชtest : jwt module rsa ์๋
raymondanythings 92ccb91
๐จfeat : DB-ORM ์ถ๊ฐ, JWT์ธ์ฆ๋ฐฉ์ ๊ตฌํ
raymondanythings 3027466
Merge branch 'master' into feature/#5-JWT-MODULE
raymondanythings 7734732
๐จfeat : ์ ์ ๊ถํ ๋ฐ ์ธ์ฆ ๋ก์ง ์ถ๊ฐ
raymondanythings File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id Int @id @default(autoincrement()) | ||
nickname String @unique | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
role UserRole? | ||
} | ||
|
||
enum UserRole { | ||
Admin | ||
Client | ||
} |
This file contains hidden or 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 hidden or 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,7 @@ | ||
import { ExceptionFilter } from '@nestjs/common' | ||
|
||
export class RoleException implements ExceptionFilter { | ||
async catch() { | ||
return { ok: false, error: '๊ถํ์ด ์์ต๋๋ค' } | ||
cheonkyu0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or 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 { GqlExecutionContext } from '@nestjs/graphql' | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common' | ||
|
||
export const AuthUser = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext) => { | ||
const gqlContext = GqlExecutionContext.create(ctx).getContext() | ||
const user = gqlContext['user'] | ||
return user | ||
}, | ||
) |
This file contains hidden or 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,34 @@ | ||
import { UsersService } from 'src/users/users.service' | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common' | ||
import { Reflector } from '@nestjs/core' | ||
import { GqlExecutionContext } from '@nestjs/graphql' | ||
import { JwtService } from 'src/jwt/jwt.service' | ||
import { RoleException } from './auth-exception.filter' | ||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
private readonly relector: Reflector, | ||
cheonkyu0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private readonly jwtService: JwtService, | ||
private readonly usersService: UsersService, | ||
) {} | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const roles = this.relector.get('roles', context.getHandler()) | ||
if (!roles) { | ||
return true | ||
} | ||
const gqlContext = GqlExecutionContext.create(context).getContext() | ||
const token = gqlContext.token | ||
if (token) { | ||
const decode = this.jwtService.verify(token) | ||
if (typeof decode === 'object' && decode.hasOwnProperty('id')) { | ||
const { user } = await this.usersService.findById(decode.id) | ||
if (user) { | ||
gqlContext['user'] = user | ||
return roles.includes('Any') || roles.includes(user.role) | ||
} | ||
} | ||
} | ||
|
||
throw new RoleException() | ||
} | ||
} |
This file contains hidden or 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 { UsersModule } from './../users/users.module' | ||
import { Module } from '@nestjs/common' | ||
import { APP_GUARD, APP_FILTER } from '@nestjs/core' | ||
import { AuthGuard } from './auth.guard' | ||
import { RoleException } from './auth-exception.filter' | ||
|
||
@Module({ | ||
imports: [UsersModule], | ||
providers: [ | ||
{ | ||
provide: APP_GUARD, | ||
useClass: AuthGuard, | ||
}, | ||
{ | ||
provide: APP_FILTER, | ||
useClass: RoleException, | ||
}, | ||
], | ||
}) | ||
export class AuthModule {} |
This file contains hidden or 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,6 @@ | ||
import { SetMetadata } from '@nestjs/common' | ||
import { UserRole } from '@prisma/client' | ||
|
||
export type AllowedRoles = keyof typeof UserRole | 'Any' | ||
|
||
export const Role = (roles: AllowedRoles[]) => SetMetadata('roles', roles) |
This file contains hidden or 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 const JWT_PROVIDER = 'jwt_provider' |
This file contains hidden or 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,6 @@ | ||
import { Socket } from 'socket.io' | ||
|
||
export type SocketMiddleware = ( | ||
socket: Socket, | ||
next: (err?: Error) => void, | ||
) => void |
This file contains hidden or 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,27 @@ | ||
import { Field, ObjectType } from '@nestjs/graphql' | ||
import { IsNotEmpty, IsNumber, IsDate } from 'class-validator' | ||
|
||
@ObjectType() | ||
export class CoreDTO { | ||
@Field(() => Number) | ||
@IsNotEmpty() | ||
@IsNumber() | ||
id: number | ||
|
||
@Field(() => Date) | ||
@IsDate() | ||
createdAt: Date | ||
|
||
@Field(() => Date) | ||
@IsDate() | ||
updatedAt: Date | ||
} | ||
|
||
@ObjectType() | ||
export class CoreOutput { | ||
@Field(() => String, { nullable: true }) | ||
error?: string | ||
|
||
@Field(() => Boolean) | ||
ok: boolean | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.