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