Hashing the password for safe storage using 'bcrypt '

PHOTO EMBED

Sat May 14 2022 01:07:41 GMT+0000 (Coordinated Universal Time)

Saved by @dingar321 #javascript

//First install required package:
npm i bcrypt

//import bcrypt
import * as bcrypt from 'bcrypt';

//hashing the password using the hash function
const recievedPassword = 'password';
const hashedPassword = await bcrypt.hash(recievedPassword, await bcrypt.genSalt());

//To compare/check a password, use the compare function:
const isMatch = await bcrypt.compare(recievedPassword, hashedPassword);
console.log('passwords: ', isMatch);
content_copyCOPY

NestJs TypeScript

https://docs.nestjs.com/security/encryption-and-hashing