β¬οΈ Main Note
https://docs.google.com/document/d/1x5mrwS0IoRmVkAR_N6FGwgVxZypR8qctSiPNYQJkcNE/edit
Hashing the user password and saving the user-password to database with encrypted.
// ============== Resolver ==============
import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { User } from './entities/user.entity';
import { UserService } from './user.service';
import * as bcrypt from 'bcrypt';
@Resolver()
export class UserResolver {
constructor(private readonly userService: UserService) {}
@Mutation(() => User)
async createUser(
@Args('email') email: string,
@Args('password') password: string,
@Args('name') name: string,
@Args('age') age: number,
) {
const hashedPassword = await bcrypt.hash(password, 10); // res : hashλ μ΄νμ λ°μ μ μλ μ΅μ’
κ°
console.log(hashedPassword);
return await this.userService.create({ email, hashedPassword, name, age }); // ({νλ‘ νΈμμ λ°μ λ΄μ©μ 보λ΄μ€μΌν¨})
}
}
// ============== Services ==============
import { ConflictException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
async create({ email, hashedPassword, name, age }) {
// email μ€λ³΅ 체ν¬
const user = await this.userRepository.findOne({ email });
// κ°μ
λ μ΄λ©μΌμ΄ μΆ©λμ΄ λ κ² - 409
if (user) throw new ConflictException('μ΄λ―Έ λ±λ‘λ μ΄λ©μΌ μ
λλ€'); // λ°°ν¬ν λ μ΄λ°κ±° λΉΌμΌν¨ (ν΄μ»€λ€μ΄ ν΄νΉν κ°λ₯μ±μ΄ μμ μ΄λ¬ν μ€λ₯ λ°μκ΅μ λ³΄κ³ )
console.log(hashedPassword);
return await this.userRepository.save({
email,
password: hashedPassword,
name,
age,
});
}
}
If there exists correspoding user during login progress, backend sends accessToken to frontend. getAccessToken