nestjs_auth...

This commit is contained in:
최준흠 2022-09-12 14:28:20 +09:00
parent 2ed6e8ed21
commit 8a170ec303

View File

@ -1,5 +1,5 @@
/* eslint-disable prettier/prettier */
import { Injectable } from '@nestjs/common'
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { User } from '@prisma/client'
import { UserDTO } from 'src/user/dtos/user.dto'
@ -16,58 +16,67 @@ export class AuthService {
//password 암호화
async getEcryptedPassword(password: string): Promise<string> {
// const encryptedPassword = await bcrypt.hash(
// password,
// jwtConstants.password_saltorRounds
// )
const encryptedPassword = password
return encryptedPassword
// return await bcrypt
// .hash(password, jwtConstants.password_saltorRounds)
// .catch((e) => {
// new Error('암호화 실패' + e)
// })
return password
}
//app.controller.ts에서 @UseGuards(AuthGuard('local'))용
async validateUser(email: string, password: string): Promise<any | null> {
const user = await this.userService.fetchOne({ email: email })
async validateUser(email: string, password: string): Promise<any> {
const user = await this.userService
.fetchOne({ email: email })
.catch((error) => {
new UnauthorizedException('사용자를 찾을 수 없습니다.' + error)
})
const encryptedPassword = await this.getEcryptedPassword(password)
if (user && user.password === encryptedPassword) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, ...result } = user
return result
} else {
new UnauthorizedException('암호가 맞지 않습니다.')
}
return null
}
async login(user: User) {
async login(
user: User
): Promise<{ access_token: string; refresh_token: string }> {
return await this.getTokens(user)
}
async register(data: UserDTO): Promise<any | null> {
const tokens = await this.getTokens(data)
data.refresh_token = tokens['refresh_token']
const encryptedPassword = await this.getEcryptedPassword(data.password)
data.password = encryptedPassword
const user = await this.userService.add(data)
if (!user) return null
return user
async register(data: UserDTO): Promise<User> {
data.refresh_token = (await this.getTokens(data)).refresh_token
data.password = await this.getEcryptedPassword(data.password)
return await this.userService.add(data)
}
async updateRefreshToken(user: User): Promise<any> {
async refreshTokens(
user: User
): Promise<{ access_token: string; refresh_token: string }> {
const tokens = await this.getTokens(user)
await this.userService.update({
where: { id: user.id },
data: { refresh_token: tokens['refresh_token'] }
data: { refresh_token: tokens.refresh_token }
})
return tokens
}
async getTokens(data: UserDTO): Promise<any> {
const payload = {
email: data.email,
name: data.name
}
const [accessToken, refreshToken] = await Promise.all([
this.jwtService.sign(payload, {
secret: jwtConstants.access_secret,
expiresIn: jwtConstants.access_expiresIn
}),
async getTokens(
data: UserDTO
): Promise<{ access_token: string; refresh_token: string }> {
return await Promise.all([
this.jwtService.sign(
{
email: data.email,
name: data.name
},
{
secret: jwtConstants.access_secret,
expiresIn: jwtConstants.access_expiresIn
}
),
this.jwtService.sign(
{},
{
@ -75,9 +84,8 @@ export class AuthService {
expiresIn: jwtConstants.refresh_expiresIn
}
)
])
const tokens = { access_token: accessToken, refresh_token: refreshToken }
console.log(tokens)
return tokens
]).then((response) => {
return { access_token: response[0], refresh_token: response[1] }
})
}
}