Repository navigation
user v2 마이그레이션 #118
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
Merged
user v2 마이그레이션 #118
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,33 @@ | ||
| import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
| import { Reflector } from '@nestjs/core'; | ||
| import { AuthGuard } from '@nestjs/passport'; | ||
| import { PublicRouteToken } from '@src/common'; | ||
| import { JWT_V2_STRATEGY_TOKEN } from '@src/modules/users/guards/strategy/strategy.token'; | ||
|
|
||
| @Injectable() | ||
| export class JwtV2Guard extends AuthGuard(JWT_V2_STRATEGY_TOKEN) { | ||
| constructor(private readonly reflector: Reflector) { | ||
| super(); | ||
| } | ||
|
|
||
| async canActivate(context: ExecutionContext): Promise<boolean> { | ||
| // Handler 혹은 Class중 하나만 정의되어있다면 boolean으로 둘다 정의되어있다면 array([boolean,boolean])로 옵니다 | ||
| const publicMetadata = this.reflector.getAllAndMerge(PublicRouteToken, [ | ||
| context.getClass(), | ||
| context.getHandler(), | ||
| ]); | ||
|
|
||
| // Handler와 Class모두 메타데이터가 정의되어있는 경우 | ||
| if ( | ||
| (Array.isArray(publicMetadata) && publicMetadata.length) || | ||
| typeof publicMetadata === 'boolean' | ||
| ) { | ||
| try { | ||
| return (await super.canActivate(context)) as boolean; | ||
| } catch (err) { | ||
| return true; | ||
| } | ||
| } | ||
| return (await super.canActivate(context)) as boolean; | ||
| } | ||
| } |
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,38 @@ | ||
| import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
| import { ConfigService } from '@nestjs/config'; | ||
| import { PassportStrategy } from '@nestjs/passport'; | ||
| import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
| import { DataSource } from 'typeorm'; | ||
| import { JwtPayload, ReqUserPayload } from '@src/common/types/type'; | ||
| import { User } from '@src/infrastructure/database/entities/user.entity'; | ||
| import { JWT_V2_STRATEGY_TOKEN } from './strategy.token'; | ||
|
|
||
| @Injectable() | ||
| export class JwtV2Strategy extends PassportStrategy( | ||
| Strategy, | ||
| JWT_V2_STRATEGY_TOKEN, | ||
| ) { | ||
| constructor( | ||
| private readonly dataSource: DataSource, | ||
| private readonly config: ConfigService, | ||
| ) { | ||
| super({ | ||
| secretOrKey: config.get<string>('JWT_SECRET'), | ||
| jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
| }); | ||
| } | ||
|
|
||
| async validate(payload: JwtPayload): Promise<ReqUserPayload> { | ||
| const user = await this.dataSource.manager.findOne(User, { | ||
| where: { | ||
| id: payload.id, | ||
| }, | ||
| }); | ||
| if (!user) { | ||
| throw new UnauthorizedException('인증에 실패하였습니다.'); | ||
| } | ||
| return { | ||
| id: payload.id, | ||
| }; | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export const JWT_STRATEGY_TOKEN = 'jwt'; | ||
| export const JWT_V2_STRATEGY_TOKEN = 'jwt-v2'; |
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 |
|---|---|---|
| @@ -1,22 +1,48 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { MongooseModule } from '@nestjs/mongoose'; | ||
| import { Folder, FolderSchema, User, UserSchema } from '@src/infrastructure'; | ||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||
| import { | ||
| Folder as FolderMongoEntity, | ||
| FolderSchema, | ||
| User as UserMongoEntity, | ||
| UserSchema, | ||
| } from '@src/infrastructure'; | ||
| import { Folder } from '@src/infrastructure/database/entities/folder.entity'; | ||
| import { User } from '@src/infrastructure/database/entities/user.entity'; | ||
| import { AuthModule } from '../auth/auth.module'; | ||
| import { FoldersPGRepository } from '../folders/folders.pg.repository'; | ||
| import { FolderRepository } from '../folders/folders.repository'; | ||
| import { JwtStrategy } from './guards/strategy'; | ||
| import { JwtV2Strategy } from './guards/strategy/jwt.v2.strategy'; | ||
| import { UsersController } from './users.controller'; | ||
| import { UsersPGRepository } from './users.pg.repository'; | ||
| import { UsersRepository } from './users.repository'; | ||
| import { UsersService } from './users.service'; | ||
| import { UsersV2Controller } from './users.v2.controller'; | ||
| import { UsersV2Service } from './users.v2.service'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| AuthModule, | ||
| TypeOrmModule.forFeature([Folder, User]), | ||
| /** @deprecated */ | ||
| MongooseModule.forFeature([ | ||
| { name: User.name, schema: UserSchema }, | ||
| { name: Folder.name, schema: FolderSchema }, | ||
| { name: UserMongoEntity.name, schema: UserSchema }, | ||
| { name: FolderMongoEntity.name, schema: FolderSchema }, | ||
| ]), | ||
| ], | ||
| controllers: [UsersController], | ||
| providers: [UsersService, UsersRepository, FolderRepository, JwtStrategy], | ||
| controllers: [UsersController, UsersV2Controller], | ||
| providers: [ | ||
| UsersV2Service, | ||
| UsersPGRepository, | ||
| FoldersPGRepository, | ||
| JwtV2Strategy, | ||
|
|
||
| /** @deprecated */ | ||
| JwtStrategy, | ||
| UsersService, | ||
| UsersRepository, | ||
| FolderRepository, | ||
| ], | ||
| }) | ||
| export class UsersModule {} |
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,19 @@ | ||
| import { Body, Controller, Post } from '@nestjs/common'; | ||
| import { CreateUserDocs, UserControllerDocs } from './docs'; | ||
| import { CreateUserDto } from './dto'; | ||
| import { CreateUserResponse } from './response'; | ||
| import { UsersV2Service } from './users.v2.service'; | ||
|
|
||
| @UserControllerDocs | ||
| @Controller({ version: '2', path: 'users' }) | ||
| export class UsersV2Controller { | ||
| constructor(private readonly userService: UsersV2Service) {} | ||
|
|
||
| @Post() | ||
| @CreateUserDocs | ||
| async createUser(@Body() dto: CreateUserDto): Promise<CreateUserResponse> { | ||
| const { userId, token } = await this.userService.createUser(dto); | ||
| const response = new CreateUserResponse(userId, token); | ||
| return response; | ||
| } | ||
| } |
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,44 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { JwtPayload } from 'src/common/types/type'; | ||
| import { DEFAULT_FOLDER_NAME } from '@src/common/constant'; | ||
| import { FolderType } from '@src/infrastructure/database/types/folder-type.enum'; | ||
| import { AuthService } from '../auth/auth.service'; | ||
| import { FoldersPGRepository } from '../folders/folders.pg.repository'; | ||
| import { CreateUserDto } from './dto'; | ||
| import { UsersPGRepository } from './users.pg.repository'; | ||
|
|
||
| @Injectable() | ||
| export class UsersV2Service { | ||
| constructor( | ||
| private readonly userRepository: UsersPGRepository, | ||
| private readonly folderRepository: FoldersPGRepository, | ||
| private readonly authService: AuthService, | ||
| ) {} | ||
|
|
||
| async createUser( | ||
| dto: CreateUserDto, | ||
| ): Promise<{ userId: string; token: string }> { | ||
| let user = await this.userRepository.findUserByDeviceToken(dto.deviceToken); | ||
| if (!user) { | ||
| // 새로운 user의 ID | ||
| user = await this.userRepository.findOrCreate(dto.deviceToken); | ||
| await this.folderRepository.createOne( | ||
| user.id, | ||
| DEFAULT_FOLDER_NAME, | ||
| FolderType.DEFAULT, | ||
| ); | ||
| } | ||
|
|
||
| // JWT Token Payload | ||
| const tokenPayload: JwtPayload = { | ||
| id: user.id, | ||
| }; | ||
| // JWT Token 발급 | ||
| const token = await this.authService.issueAccessToken(tokenPayload); | ||
|
|
||
| return { | ||
| userId: user.id, | ||
| token: token, | ||
| }; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
근데 이렇게 하면 지금 앱에서 api 호출 못할것 같은데!! 맞나
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
적용 완료!