OTP
@loginid/websdk3 • Docs
@loginid/websdk3 / Internal Modules / OTP
Class: OTP
Extends LoginIDBase to support OTP methods.
Extends
Extended by
Constructors
new OTP()
new OTP(
config
):OTP
Initializes a new instance of OTP with the provided configuration.
Parameters
• config: LoginIDConfig
Configuration object for LoginID.
Returns
Overrides
Properties
session
readonly
session:SessionManager
Instance of SessionManager, providing access to the session management methods.
Inherited from
Methods
requestAndSendOtp()
requestAndSendOtp(
username
,method
,options
):Promise
<void
>
This method requests an OTP from the backend to be sent via the selected method. The method of delivery should be based on
the user's choice from the list of available options. This can be found in the result of authenticateWithPasskey
method as fallbackOptions
.
Parameters
• username: string
Username to request and send the OTP to.
• method: Message
= 'email'
Method to send the code, either 'email' or 'sms'. Default is 'email'.
• options: RequestAndSendOtpOptions
= {}
Additional options for sending the OTP.
Returns
Promise
<void
>
A promise that resolves when the operation completes successfully.
Example
import { LoginIDWebSDK } from "@loginid/websdk3";
// Obtain credentials from LoginID
const BASE_URL = process.env.BASE_URL;
// Initialize the SDK with your configuration
const config = {
baseUrl: BASE_URL,
};
// Use the SDK components for signup and signin
const lid = new LoginIDWebSDK(config);
const username = "billy@loginid.io";
async function sendUserOTPHandler() {
try {
// Send OTP to a user via email
await lid.requestAndSendOtp(username, "email");
console.log("OTP sent successfully.");
} catch (error) {
console.error("Error sending code:", error);
}
}
const sendOtpButton = document.getElementById("button");
sendOtpButton.addEventListener("click", sendUserOTPHandler);
validateOtp()
validateOtp(
username
,otp
,options
):Promise
<AuthResult
>
This method verifies the OTP and returns an authorization token, which can be used with the passkeyCreate()
method to create a new passkey. The authorization token has a short validity period and should be used immediately.
Parameters
• username: string
Username to validate with.
• otp: string
OTP to validate.
• options: ValidateOtpOptions
= {}
Additional authentication options.
Returns
Promise
<AuthResult
>
Result of the authentication operation.
Example
import { LoginIDWebSDK } from "@loginid/websdk3";
// Obtain credentials from LoginID
const BASE_URL = process.env.BASE_URL;
// Initialize the SDK with your configuration
const config = {
baseUrl: BASE_URL,
};
// Use the SDK components for signup and signin
const lid = new LoginIDWebSDK(config);
// Button click handler to generate a code with passkey
async function handleRequestOTPButtonClick() {
const username = "billy@loginid.io";
try {
// Request OTP with passkey
const result = await lid.requestOtp(username);
// Extract the OTP from the response
const otp = result.code;
// Authenticate with the OTP
// You can authenticate on another device with this OTP
const authenticateResult = await lid.validateOtp(username, otp);
// Handle the authentication result
console.log("Authentication Result:", authenticateResult);
} catch (error) {
// Handle errors
console.error("Error during authentication:", error);
}
}
// Attach the click handler to a button
const requestOtpButton = document.getElementById("requestOtpButton");
requestOtpButton.addEventListener("click", handleRequestOTPButtonClick);