Skip to main content
Version: v2

redeemOtp

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';
  1. 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);
}
  1. Use authenticateOtp to initiate authentication using an OTP, and keep the url of the OtpChallengeResponse it returns.

  2. Use redeemOtp to redeem the OTP the user received (see example below).

Parameters​

ParameterTypeDescription
urlstringRequired. The url of the OtpChallengeResponse returned by authenticateOtp, or by a previous failed redeemOtp.
otpstringRequired. The OTP to redeem.

Returns​

The redeemOtp function returns either an AuthenticateResponse if the OTP was correct, or an OtpChallengeResponse if it was not:

The Promise resolves to an AuthenticateResponse on success, or an OtpChallengeResponse if the OTP was wrong.

On Kotlin, the result is delivered to the callback, or emitted by the returned Flow. On Swift, it is delivered to the callback, or returned by the async throws variant. On JavaScript, React Native and Flutter, the function returns a Promise or Future.

On success, the AuthenticateResponse 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.

If the OTP was wrong, the OtpChallengeResponse contains the following key:

  • url: a URL containing the state of the authentication. Use it when calling redeemOtp or authenticateOtp again.

Examples​

Example: Call redeemOtp​

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