From 8a170ec30300d9afa54db53cda185f0ebde0fc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Mon, 12 Sep 2022 14:28:20 +0900 Subject: [PATCH] nestjs_auth... --- src/auth/auth.service.ts | 78 ++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 6dc356e..c1b0876 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -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 { - // 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 { - const user = await this.userService.fetchOne({ email: email }) + async validateUser(email: string, password: string): Promise { + 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 { - 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 { + 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 { + 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 { - 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] } + }) } }