server/repository/data/UserRepo.js
/**
   * Signup logic handler
   * @param {String} email email of user
   * @param {String} password password of user
   * @returns {Object} data to be sent in api response
   */
Signup logic handler
@param {String} email email of user
@param {String} password password of user
@returns {Object} data to be sent in api response
Signup logic handler @param { String } email email of user @param { String } password password of user @returns { Object } data to be sent in api response
async signup(email, password, firstName, lastName) {
    let existingUser;
    try {
      existingUser = await this.UserModel.findOne({ email });
    } catch (err) {
      err.message = "Error occurred during signup of user";
      throw err;
    }
    if (existingUser) {
      throw new Error("User already exists. Try to login");
    }
    const user = new this.UserModel({
      email,
      password,
      firstName,
      lastName,
    });
    let savedUser;
    try {
      const salt = await bcrypt.genSalt(10);
      const hash = await bcrypt.hash(user.password, salt);
      user.password = hash;
      savedUser = await user.save();
    } catch (err) {
      err.message = "Error occurred during saving of user to database";
      throw err;
    }
    const token = jwt.sign({ id: savedUser.id }, process.env.JWT_SECRET, {
      expiresIn: "7d",
    });
    const data = {
      token: `Bearer ${token}`,
      user: {
        id: savedUser.id,
        firstName: savedUser.firstName,
        lastName: savedUser.lastName,
        role: savedUser.role,
        email: savedUser.email,
      },
    };
    return data;
  }
async signup ( email , password , firstName , lastName ) { let existingUser ; try { existingUser = await this . UserModel . findOne ( { email } ) ; } catch ( err ) { err . message = "Error occurred during signup of user" ; throw err ; } if ( existingUser ) { throw new Error ( "User already exists. Try to login" ) ; } const user = new this . UserModel ( { email , password , firstName , lastName , } ) ; let savedUser ; try { const salt = await bcrypt . genSalt ( 10 ) ; const hash = await bcrypt . hash ( user . password , salt ) ; user . password = hash ; savedUser = await user . save ( ) ; } catch ( err ) { err . message = "Error occurred during saving of user to database" ; throw err ; } const token = jwt . sign ( { id : savedUser . id } , process . env . JWT_SECRET , { expiresIn : "7d" , } ) ; const data = { token : ` ${ token } ` , user : { id : savedUser . id , firstName : savedUser . firstName , lastName : savedUser . lastName , role : savedUser . role , email : savedUser . email , } , } ; return data ; }
Signup logic handler
 @param {String} email email of user
 @param {String} password password of user
 @returns {Object} data to be sent in api response
Signup logic handler @param { String } email email of user @param { String } password password of user @returns { Object } data to be sent in api response