import { BadRequestException, Injectable } from "@nestjs/common";
import { UsersService } from "./users.service";
import { randomBytes, scrypt as _scrypt } from "crypto";
import { promisify } from "util";
// promisify를 통해 비동기로 돌리려는 함수를 promise로 감싸주지 않고 사용할 수 있다.
// scrypt를 이용하여 해쉬 처리를 한다.
const scrypt = promisify(_scrypt)
@Injectable()
export class AuthService {
constructor(private usersService : UsersService){}
async signup (email : string, password : string) {
/*
1. 이메일이 사용중인지 확인
2. 비밀번호를 해쉬로 변환
3. 유저 생성
4. 유저를 리턴
*/
const users = await this.usersService.find(email)
if(users.length) {
throw new BadRequestException('이메일이 이미 존재합니다.')
}
// 암호를 해쉬화, Salt 생성
const salt = randomBytes(8).toString('hex')
// Salt와 암호를 혼합하여 해쉬 처리
const hash = (await scrypt(password, salt, 32)) as Buffer
// 해쉬처리된 값들을 혼합하여, DB에 저장
const result = salt + '.' + hash.toString('hex')
}
async signin(email : string, password : string) {
const [user] = await this.usersService.find(email);
if(!user) {
throw new NotFoundException('사용자가 없습니다.')
}
const [salt, storeHash] = user.password.split('.');
const hash = (await scrypt(password, salt, 32)) as Buffer
if(storeHash !== hash.toString('hex')){
throw new BadRequestException('비밀번호가 맞지 않습니다.')
}
return user;
}
}