YesCoding

Go to English
search:

[TypeOrm & Nestjs] entity ์—์„œ BeforeInsert ๋กœ hash password ๊ตฌํ˜„ํ•˜๊ธฐ

thumbnail hashpassword

Nestjs์™€ TypeORM ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ hash ์ฒ˜๋ฆฌ์‹œ entity ์—์„œ @Beforeinsert, @Beforeupdate ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์›๋ž˜๋Š” auth.service.ts ์—์„œ hashPassword ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ผ๋Š”๋ฐ, user.service.ts ์—์„œ๋„ ํ•ด๋‹น hashPassword ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜ 1๊ฐœ ๋•Œ๋ฌธ์— user service ๊ฐ€ Auth service ์— ์˜์กดํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์‹ซ์–ด์„œ, ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋“ค์–ด์žˆ๋Š” user entity ์— insert ๋‚˜ update ๋ฅผ ํ•  ๋•Œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž๋™์œผ๋กœ Hash ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด์•˜๋‹ค.

๊ธฐ์กด ์ฝ”๋“œ

// src/domain/auth/auth.service.ts import * as bcrypt from 'bcrypt' private async hashPassword(password: string): Promise<string> { const salt = await bcrypt.genSalt(); const hashedPassword = await bcrypt.hash(password, salt); return hashedPassword; }

๋ชฉํ‘œ

users.entity.ts ์—์„œ @BeforeInsert decorator ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ธฐ์ „์— password ๋ฅผ hash ํ•œ๋‹ค.

์„ฑ๊ณตํ•˜๋ฉด auth.service.ts ์— ๋งŒ๋“ค์—ˆ๋˜ hashPassword ํ•จ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  user service ์™€ auth service ๊ฐ„ ์˜์กด๋„๋„ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋‹ค.

@Entity('users') @Unique(['email', 'provider']) export class Users { private ulid: ULID; constructor() { this.ulid = monotonicFactory(); } @PrimaryColumn({ name: 'user_id', comment: 'Universally Unique Lexicographically Sortable Identifier of user.', }) userId: string; @Column({ comment: 'user email' }) email: string; @Column({ type: 'varchar', comment: 'user password', nullable: true }) password: string; ... @CreateDateColumn({ name: 'created_at', type: 'timestamp with time zone', comment: 'user created date', }) createdAt: Date; @UpdateDateColumn({ name: 'updated_at', type: 'timestamp with time zone', comment: 'user updated date', }) updatedAt: Date; @DeleteDateColumn({ name: 'deleted_at', type: 'timestamp with time zone', comment: 'user deleted date', }) deletedAt: Date; ... @BeforeInsert() generateUlid() { this.userId = this.ulid(); } @BeforeInsert() async hashPassword(): Promise<void> { const salt = await bcrypt.genSalt(); this.password = await bcrypt.hash(this.password, salt); } }

๊ธฐ์กด ์œ ์ €์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋งŒ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•  ๋•Œ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•จ

@BeforeUpdate() async hashPassword(): Promise<void> { const salt = await bcrypt.genSalt(); this.password = await bcrypt.hash(this.password, salt); }

๐Ÿ’ก๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

@BeforeInsert, @BeforeUpdate ๋Š” TypeORM ์˜ ๋ฆฌ์Šค๋„ˆ์ด๋‹ค.

repository.save ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ๋ถˆ๋ ค์ง„๋‹ค.

this.password ๋Š” save ์ „ create ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ entity instance ์˜ password ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

const newUserEntityInstance = this.userRepository.create(newUser) this.userRepository.save(newUserEntityInstance)

Recommend Post
nestjs request dto and response dto
nestjs request dto and response dto
google social login ํ•œ ์œ ์ €์˜ ์ƒ๋…„์›”์ผ ๊ฐ€์ ธ์˜ค๊ธฐ
google social login ํ•œ ์œ ์ €์˜ ์ƒ๋…„์›”์ผ ๊ฐ€์ ธ์˜ค๊ธฐ
TypeORM soft delete on cascade
TypeORM soft delete on cascade
typeORM entity ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ hash ํ•˜๊ธฐ
typeORM entity ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ hash ํ•˜๊ธฐ
ยฉ Copyright 2022, YesCoding