Whatsapp Authentication
Authenticate users via WhatsApp using Okto's NextJS SDK with sendOTP, resendOTP, and loginUsingWhatsApp methods for secure, token-based session management.
The sendOTP()
method initiates the WhatsApp authentication flow by sending a one-time password (OTP) to the specified user's WhatsApp number.
The loginUsingWhatsApp()
method verifies the provided OTP against the user's WhatsApp number. Upon successful verification, the user's session is established.
Example
import { useState } from 'react';
import { useOkto } from '@okto_web3/react-sdk';
function WhatsAppAuthentication() {
const oktoClient = useOkto();
const [phoneNo, setPhoneNo] = useState('');
const [otp, setOtp] = useState('');
const [token, setToken] = useState('');
async function handleSendOTP() {
try {
const response = await oktoClient.sendOTP(phoneNo, "whatsapp");
console.log('OTP Sent:', response);
setToken(response.token);
} catch (error) {
console.error('Error sending OTP:', error);
}
}
async function handleResendOTP() {
try {
const response = await oktoClient.resendOTP(phoneNo, token, "whatsapp");
console.log('OTP Resent:', response);
setToken(response.token);
} catch (error) {
console.error('Error resending OTP:', error);
}
}
async function handleVerifyOTP() {
try {
const session = await oktoClient.loginUsingWhatsApp(phoneNo, otp, token, (session: any) => {
localStorage.setItem("okto_session", JSON.stringify(session));
});
console.log('Login Successful:', session);
} catch (error) {
console.error('Error verifying OTP:', error);
}
}
return (
<div>
<input type="tel" placeholder="Enter WhatsApp number" value={phoneNo} onChange={(e) => setPhoneNo(e.target.value)} />
<input type="text" placeholder="Enter OTP" value={otp} onChange={(e) => setOtp(e.target.value)} />
<button onClick={handleSendOTP}>Send OTP</button>
<button onClick={handleResendOTP}>Resend OTP</button>
<button onClick={handleVerifyOTP}>Verify OTP</button>
</div>
);
}
Note
For error handling:
- Use the error code to debug issues. Check out the SDK errors and warnings documentation
- For help, navigate to the troubleshooting guide to contact support
Method Overview
Method | Description |
---|---|
async OktoClient.sendOTP | Sends an OTP to the user's WhatsApp number |
async OktoClient.resendOTP | Resends an OTP using a previously generated token |
async OktoClient.loginUsingWhatsApp | Verifies the OTP and logs in the user |
Send OTP
async OktoClient.sendOTP(contact: string, method: "whatsapp")
sends the OTP to the
specified WhatsApp number.
Parameters
Parameter | Type | Description | Required |
---|---|---|---|
contact | string | User's WhatsApp number | Yes |
method | string | OTP delivery method (email or whatsapp) | Yes |
Response
Success Response
Field Name | Type | Description |
---|---|---|
result | Promise<WhatsAppSendOtpResponse> | Resolves with a success response if the OTP is sent successfully |
Resend OTP
async OktoClient.resendOTP(contact: string, token: string, method: "whatsapp")
resends the OTP using the previously issued token.
Parameters
Parameter | Type | Description | Required |
---|---|---|---|
contact | string | User's WhatsApp number | Yes |
token | string | The token received from the previous sendOTP request | Yes |
method | "whatsapp" | OTP delivery method (email or whatsapp) | Yes |
Response
Success Response
Field Name | Type | Description |
---|---|---|
result | Promise<WhatsAppResendOtpResponse> | Resolves with a success response if the OTP is resent successfully. |
Login Using WhatsApp
async OktoClient.loginUsingWhatsApp(contact: string, otp: string, token: string)
verifies the OTP against the provided WhatsApp number and logs in the user. If
successful, returns the user's Smart Wallet Address.
Parameters
Parameter | Type | Description | Required |
---|---|---|---|
contact | string | User's WhatsApp number | Yes |
otp | string | OTP sent to the user's WhatsApp number | Yes |
token | string | The token received from the previous sendOTP request | Yes |
Response
Success Response
Field Name | Type | Description |
---|---|---|
result | Promise<Address> | Returns the user's Smart Wallet Address on successful login |