Okto Docs
Whatsapp OTP

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:

Method Overview

MethodDescription
async OktoClient.sendOTPSends an OTP to the user's WhatsApp number
async OktoClient.resendOTPResends an OTP using a previously generated token
async OktoClient.loginUsingWhatsAppVerifies 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

ParameterTypeDescriptionRequired
contactstringUser's WhatsApp numberYes
methodstringOTP delivery method (email or whatsapp)Yes

Response

Success Response

Field NameTypeDescription
resultPromise<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

ParameterTypeDescriptionRequired
contactstringUser's WhatsApp numberYes
tokenstringThe token received from the previous sendOTP requestYes
method"whatsapp"OTP delivery method (email or whatsapp)Yes

Response

Success Response

Field NameTypeDescription
resultPromise<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

ParameterTypeDescriptionRequired
contactstringUser's WhatsApp numberYes
otpstringOTP sent to the user's WhatsApp numberYes
tokenstringThe token received from the previous sendOTP requestYes

Response

Success Response

Field NameTypeDescription
resultPromise<Address>Returns the user's Smart Wallet Address on successful login