The ESPRMAuth class provides comprehensive authentication functionality for users. It includes methods for account sign-up, login, password management, OTP-based authentication, and OAuth-based login flows. Key features include:

  • Sign-Up Process: Methods to send sign-up codes and confirm user registration.
  • Password Management: Support for password recovery, setting new passwords, and login with credentials.
  • OTP-Based Login: Methods to request and validate OTPs for secure access.
  • OAuth Authentication: Support for initiating and completing login using OAuth codes.
  • Session Management: Provides functionality to fetch details of the currently logged-in user.

This class simplifies the integration of authentication mechanisms in applications built on the ESP RainMaker ecosystem.

Constructors

Methods

  • Confirms a user’s account by verifying the username and confirmation code. Optionally, tags can be provided.

    Parameters

    • username: string

      The username of the user to confirm.

    • verificationCode: string

      The code sent to the user for verification.

    • Optionaltags: string[]

      Optional tags associated with the user.

    Returns Promise<ESPAPIResponse>

    A promise that resolves with an ESPAPIResponse object if the user is successfully signed up.

  • Initiates the forgot password flow for a user by sending their username to the API.

    Parameters

    • username: string

      The username of the user who wants to reset their password.

    Returns Promise<ESPAPIResponse>

    A promise that resolves with an ESPAPIResponse indicating success.

  • Retrieves the currently logged-in user.

    This method checks if the access token is available and valid. If the access token is expired, it attempts to refresh the session using the refresh token. If successful, it returns an instance of the ESPRMUser class containing the user's tokens. If no valid access token is found, it returns null.

    Returns Promise<null | ESPRMUser>

    A promise that resolves to an instance of ESPRMUser if the user is logged in, or null if not.

  • Logs in a user by their username and password, returning an ESPRMUser instance upon success.

    Parameters

    • username: string

      The username of the user logging in.

    • password: string

      The password of the user.

    Returns Promise<ESPRMUser>

    A promise that resolves to an instance of ESPRMUser containing the user's tokens.

    ESPValidationError if the password is missing.

  • Logs in a user using OAuth by first requesting an OAuth code and then exchanging it for tokens. This method combines the functionality of requesting OAuth code and logging in with the code.

    Parameters

    • identityProvider: string

      The identity provider for which to request an OAuth login code.

    Returns Promise<ESPRMUser>

    A promise that resolves to an instance of ESPRMUser containing user tokens.

  • Logs in a user using an OTP (One-Time Password) and a session token.

    Parameters

    • username: string

      The username of the user attempting to log in.

    • verificationCode: string

      The OTP received by the user.

    • sessionToken: string

      The session token associated with the OTP request.

    Returns Promise<ESPRMUser>

    A promise that resolves to an instance of ESPRMUser containing user tokens.

    ESPValidationError if the verification code or session token is missing.

  • Requests a login OTP (One-Time Password) for the specified username.

    Parameters

    • username: string

      The username for which the OTP is requested.

    Returns Promise<string>

    A promise that resolves to a sessionToken string indicating the OTP session.

  • Registers a new user by providing their username and password.

    Parameters

    • username: string

      The username for the new account.

    • password: string

      The password for the new account.

    Returns Promise<ESPAPIResponse>

    A promise that resolves to an ESPAPIResponse indicating the success in sending sign up code.

  • Sets a new password for a user by providing their username, a new password, and a verification code.

    Parameters

    • username: string

      The username of the user who is setting a new password.

    • newPassword: string

      The new password to be set for the user.

    • verificationCode: string

      The verification code used to authorize the password change.

    Returns Promise<ESPAPIResponse>

    A promise that resolves to an ESPAPIResponse indicating the success of the operation.