@joystick.js/node

Recover Password

Start the password recovery process for an existing user account from the server.

To start the password recovery process for an existing user account, the accounts.recover_password() method can be called from anywhere on your server.

Example Usage

api/users/setters.js

import joystick, { accounts } from '@joystick.js/node';
import Twilio from 'twilio';

const twilio = Twilio(
  joystick.settings.private.twilio.account_sid,
  joystick.settings.private.twilio.auth_token,
);

const setters = {
  recover_password_via_sms: {
    input: {
      email_address: {
        type: 'string',
        required: true,
      },
    },
    set: async (input = {}) => {
      const user = await process.databases.mongodb.collection('users').findOne({
        emailAddress: input?.email_address,
      });

      if (!user || !user?.sms_phone_number) {
        throw new Error('User does not have an SMS number defined.');
      }

      const reset_token = await accounts.recover_password({
        email_address: input?.email_address,
      });

      await twilio.messages.create({
        body: `Your password reset token is: ${reset_token}.`,
        to: '+11234567890',
        from: '+18905671234',
      });
    },
  },
};

export default setters;

Above, we create a hypothetical setter that triggers a password recovery attempt, relaying the reset token to a two-factor SMS number via the Twilio API.

API

Definition

accounts.recover_password(options: object) => Promise

Parameters

options object required
An object defining the parameters for the existing user account.
email_address string required
The email address for the existing user account.