-
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.
Merge pull request #21 from mash-up-kr/refactor/user-respository
Refactor: User모듈 레포지토리 레이어 분리
- Loading branch information
Showing
6 changed files
with
75 additions
and
38 deletions.
There are no files selected for viewing
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,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
|
||
@Module({ | ||
imports: [ | ||
JwtModule.register({ | ||
global: true, | ||
}), | ||
], | ||
providers: [AuthService], | ||
exports: [AuthService], | ||
}) | ||
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,20 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { JwtPayload } from '@src/common/types/type'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private readonly jwt: JwtService, | ||
private readonly config: ConfigService, | ||
) {} | ||
|
||
async issueAccessToken(payload: JwtPayload): Promise<string> { | ||
const token = await this.jwt.signAsync(payload, { | ||
secret: this.config.get<string>('JWT_SECRET'), | ||
expiresIn: this.config.get<string>('JWT_EXPIRE_TIME'), | ||
}); | ||
return 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
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,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { User } from '@src/infrastructure'; | ||
import { Model } from 'mongoose'; | ||
|
||
@Injectable() | ||
export class UsersRepository { | ||
constructor( | ||
@InjectModel(User.name) private readonly userModel: Model<User>, | ||
) {} | ||
|
||
async findOrCreate(deviceToken: string) { | ||
const user = await this.userModel | ||
.findOne({ | ||
deviceToken: deviceToken, | ||
}) | ||
.lean(); | ||
if (user) { | ||
return user; | ||
} | ||
const newUser = await this.userModel.create({ | ||
deviceToken: deviceToken, | ||
}); | ||
return newUser; | ||
} | ||
} |
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,49 +1,25 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { CreateUserDto } from './dto'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { JwtPayload } from 'src/common/types/type'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { User } from '@src/infrastructure'; | ||
import { UsersRepository } from './users.repository'; | ||
import { AuthService } from '../auth/auth.service'; | ||
|
||
@Injectable() | ||
export class UsersService { | ||
constructor( | ||
@InjectModel(User.name) private readonly userModel: Model<User>, | ||
private readonly jwt: JwtService, | ||
private readonly config: ConfigService, | ||
private readonly userRepository: UsersRepository, | ||
private readonly authService: AuthService, | ||
) {} | ||
|
||
async createUser(dto: CreateUserDto): Promise<string> { | ||
const user = await this.userModel | ||
.findOneAndUpdate( | ||
{ | ||
deviceToken: dto.deviceToken, | ||
}, | ||
{ | ||
$set: { | ||
deviceToken: dto.deviceToken, | ||
}, | ||
}, | ||
{ | ||
new: true, | ||
upsert: true, | ||
}, | ||
) | ||
.lean(); | ||
// 새로운 user의 ID | ||
const user = await this.userRepository.findOrCreate(dto.deviceToken); | ||
// JWT Token Payload | ||
const tokenPayload: JwtPayload = { | ||
id: user._id.toString(), | ||
}; | ||
const token = await this.issueAccessToken(tokenPayload); | ||
return token; | ||
} | ||
|
||
private async issueAccessToken(payload: JwtPayload) { | ||
const token = await this.jwt.signAsync(payload, { | ||
secret: this.config.get<string>('JWT_SECRET'), | ||
expiresIn: this.config.get<string>('JWT_EXPIRE_TIME'), | ||
}); | ||
// JWT Token 발급 | ||
const token = await this.authService.issueAccessToken(tokenPayload); | ||
return token; | ||
} | ||
} |