async function recoverPassword(email) {
  const client = await db.getClient();
  try {
    // Generate a password recovery token
    const token = crypto.randomBytes(64).toString('hex');

    // Check if a user with the specified email exists in the database
    const findUserByEmail = {
      text: 'SELECT id FROM users WHERE email = $1 AND is_active = TRUE AND deleted_at IS NULL',
      values: [email]
    };
    const users = await client.query(findUserByEmail);

    // User not found, throw an error
    if (users.rowCount === 0) throw new ResourceNotFoundError();

    // User exists, assign the password reset token to the user
    const updatePasswordResetToken = {
      text:
        'UPDATE users SET password_reset_token = $1 WHERE email = $2 AND is_active = TRUE AND deleted_at IS NULL',
      values: [token, email]
    };
    await client.query(updatePasswordResetToken);

    // Send verification email
    const msg = {
      link: `${authUrl}/password/reset/${token}`
    };
    mailer.send(email, 'Reset your password', 'recoverPassword.njk', msg);

    // Return an object containing the success message and the password reset token
    return {
      message: 'A password reset link was sent to your email.',
      token
    };
  } catch (error) {
    // Bubble up the error to the global error handler
    throw error;
  } finally {
    // Release the database client
    client.release();
  }
}
/**
 * Send a password recovery link to the specified email.
 * @static
 * @async
 * @param {string} email - The email address of the user.
 * @throws {Error}
 * @returns {Promise} Promise object represents the string message and password recovery token
 */
Send a password recovery link to the specified email.
@static
@async
@param {string} email - The email address of the user.
@throws {Error}
@returns {Promise} Promise object represents the string message and password recovery token
Send a password recovery link to the specified email . @static @async @param { string } email - The email address of the user . @throws { Error } @returns { Promise } Promise object represents the string message and password recovery token
async function recoverPassword(email) {
  const client = await db.getClient();
  try {
    const token = crypto.randomBytes(64).toString('hex');
    const findUserByEmail = {
      text: 'SELECT id FROM users WHERE email = $1 AND is_active = TRUE AND deleted_at IS NULL',
      values: [email]
    };
    const users = await client.query(findUserByEmail);
    if (users.rowCount === 0) throw new ResourceNotFoundError();
    const updatePasswordResetToken = {
      text:
        'UPDATE users SET password_reset_token = $1 WHERE email = $2 AND is_active = TRUE AND deleted_at IS NULL',
      values: [token, email]
    };
    await client.query(updatePasswordResetToken);
    const msg = {
      link: `${authUrl}/password/reset/${token}`
    };
    mailer.send(email, 'Reset your password', 'recoverPassword.njk', msg);
    return {
      message: 'A password reset link was sent to your email.',
      token
    };
  } catch (error) {
    throw error;
  } finally {
    client.release();
  }
}
async function recoverPassword ( email ) { const client = await db . getClient ( ) ; try { const token = crypto . randomBytes ( 64 ) . toString ( 'hex' ) ; const findUserByEmail = { text : 'SELECT id FROM users WHERE email = $1 AND is_active = TRUE AND deleted_at IS NULL' , values : [ email ] } ; const users = await client . query ( findUserByEmail ) ; if ( users . rowCount === 0 ) throw new ResourceNotFoundError ( ) ; const updatePasswordResetToken = { text : 'UPDATE users SET password_reset_token = $1 WHERE email = $2 AND is_active = TRUE AND deleted_at IS NULL' , values : [ token , email ] } ; await client . query ( updatePasswordResetToken ) ; const msg = { link : ` ${ authUrl } ${ token } ` } ; mailer . send ( email , 'Reset your password' , 'recoverPassword.njk' , msg ) ; return { message : 'A password reset link was sent to your email.' , token } ; } catch ( error ) { throw error ; } finally { client . release ( ) ; } }
Send a password recovery link to the specified email.
Send a password recovery link to the specified email .
// Generate a password recovery token // Check if a user with the specified email exists in the database // User not found, throw an error // User exists, assign the password reset token to the user // Send verification email // Return an object containing the success message and the password reset token // Bubble up the error to the global error handler // Release the database client