Recover Password

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

/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: (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 Reference

Function API

Function API

accounts.recover_password(options: object) => Promise;

Arguments

  • options object

    An object defining the parameters for the existing user account.

    • email_address string Required

      A string defining the email address for the existing user account.

On This Page