πŸ“ BE TIL Day 21 0411

JBΒ·2022λ…„ 4μ›” 11일
0

CodeCamp BE 02

λͺ©λ‘ 보기
20/30

⬇️ Main Note
https://docs.google.com/document/d/1x5mrwS0IoRmVkAR_N6FGwgVxZypR8qctSiPNYQJkcNE/edit


🌿 bcrypt

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,
    });
  }
}

☁️ JWT


If there exists correspoding user during login progress, backend sends accessToken to frontend. getAccessToken


🌿 Guarding Structure Flow


profile
두비두λ°₯λ°₯

0개의 λŒ“κΈ€