Question:
Run bcrypt code automatically when Instantiate a class in typescript in Nodejs

Here are two quick ways to Run bcrypt code automatically when Instantiate a class in typescript in Node.js.


  1. Builder pattern

import { v4 as uuidv4 } from 'uuid';

import bcrypt from 'bcryptjs';


class User {

    public userId: string;

    public password: string = '';


    constructor(public userName: string, public plainPassword: string, public email: string) {

        this.userId = uuidv4();

        this.userName = userName;

        this.email = email.toLowerCase();

        this.plainPassword = plainPassword;

    }


    public async hashPassword(): Promise<void> {

        try {

            const salt = await bcrypt.genSalt(10);

            this.password = await bcrypt.hash(this.plainPassword, salt);

        } catch (error) {

            throw error;

        }

    }

}


class UserBuilder {

    private user?: User;


    public initialize(userName: string, plainPassword: string, email: string): UserBuilder {

        this.user = new User(userName, plainPassword, email);

        return this;

    }


    public async build(): Promise<User|null> {

        if (this.user) {

            await this.user.hashPassword();

            return this.user;

        }

        return null;

    }

}


async function clientCode() {

    const user = await new UserBuilder()

        .initialize('John Doe', 'password', 'name@email.com')

        .build();

        

    console.log(user);

}


clientCode();


  1. Factory pattern

import { v4 as uuidv4 } from 'uuid';

import bcrypt from 'bcryptjs';


class User {

    public userId: string;

    public password: string = '';


    private constructor(public userName: string, public plainPassword: string, public email: string) {

        this.userId = uuidv4();

        this.userName = userName;

        this.email = email.toLowerCase();

        this.plainPassword = plainPassword;

    }


    public async hashPassword(): Promise<void> {

        try {

            const salt = await bcrypt.genSalt(10);

            this.password = await bcrypt.hash(this.plainPassword, salt);

        } catch (error) {

            throw error;

        }

    }


    // factory method to create a User instance

    public static async createUser(userName: string, plainPassword: string, email: string): Promise<User> {

        const user = new User(userName, plainPassword, email);

        await user.hashPassword();

        return user;

    }

}


// Using the factory method

async function clientCode() {

    const user = await User.createUser('John Doe', 'password', 'name@email.com');

    console.log(user);

}


clientCode();


In essence, both patterns allow you to control the instantiation process, enhancing the readability and maintainability of your code. While these patterns may not offer a direct solution to the absence of async constructors in JavaScript/TypeScript, they effectively address the underlying problem.


Answered by:> rahpuser

Credit:> StackOverflow


Blog links:

>How to show encrypted user id in URL in Laravel?

>How to fix Laravel LiveWire listener?

>CRUD Operation on Angular

>Deploying a python App for iOS, Windows, and macOS

>Design a basic Mobile App With the Kivy Python Framework

>How React hooks Completely replace redux?

>How to access the state using Redux RTK in the React Native app?



Nisha Patel

Nisha Patel

Submit
0 Answers