Skip to main content
Version: v2

redeemOtp

This function is only available on the JavaScript SDK. Support for more SDKs will be coming soon.

The redeemOtp function enables an app using the Beyond Identity SDK to redeem an otp for a grant code. This function is used in conjunction with authenticateOtp.

Dependencies​

The redeemOtp function requires the Beyond Identity SDK.

yarn add @beyondidentity/bi-sdk-js

or

npm install @beyondidentity/bi-sdk-js

Prerequisites​

Before making a call to redeemOtp, you must complete the following prerequisite calls:

  1. Import the required types and functions from the SDK.

    import { Embedded } from '@beyondidentity/bi-sdk-js';
  2. Initialize the SDK.

    // --- Initialize with required arguments
    try {
    const embedded = await Embedded.initialize();
    console.log("Initialization successful", embedded);
    } catch (error) {
    console.error("Initialization failed:", error);
    }

    // --- Initialize with required and optional arguments
    const config = {
    allowedDomains: ["example.com", "another-example.com"],
    logger: function (logType, message) {
    console.log(`[${logType}] ${message}`);
    },
    };

    try {
    const embedded = await Embedded.initialize(config);
    console.log("Initialization successful", embedded);
    } catch (error) {
    console.error("Initialization failed:", error);
    }
  3. Use authenticateOtp to initiate authentication using an OTP

    await embedded.authenticateOtp(url, email);
  4. Use redeemOtp to redeem an otp for a grant code

    await embedded.redeemOtp(url, otp);

Parameters​

ParameterTypeDescription
urlstringRequired. The authentication URL of the current transaction. This url is generated from authenticateOtp.
otpstringRequired. The OTP to redeem.

Returns​

On success, the redeemOtp function returns a Promise that resolves to an AuthenticateResponse, which itself is a JSON object that contains the following keys:

  • redirectUrl: string containing the redirect URL that originates from the /authorize call's redirect_uri parameter. The OAuth2 authorization code and the state parameter of the /authorize call are attached with the "code" and "state" parameters to this URL.

  • message: optional string containing a displayable message defined by policy returned by the cloud on success.

  • passkeyBindingToken: string containing a one-time-token that may be redeemed for a CredentialBindingLink.

You can exchange the token for a link by calling the credential-binding-jobs endpoint.

const response = await fetch(
`https://auth-${region}.beyondidentity.com/v1/tenants/${tenantId}/realms/${realmId}/applications/${applicationId}/credential-binding-jobs`,
{
method: "POST",
headers: { Authorization: `Bearer ${passkeyBindingToken}` },
}
);

This response will contain a credential_binding_link, which can be used by isBindPasskeyUrl and bindPasskey to bind the credential.

On failure, the redeemOtp function returns a Promise that resolves to an OtpChallengeResponse, which itself is a JSON object that contains the following keys:

Note: This url should be used when calling redeemOtp or authenticateOtp on retries.

  • url: object containing a URL containing the state of the authentication.

Examples​

Example: Call redeemOtp​

let authenticateResponse = await embedded.redeemOtp(url, otp);