Okto Docs
Email OTP

Email Authentication

Authenticate users via email using Okto's ReactJS SDK with sendOTP, resendOTP, and loginUsingEmail methods for secure, token-based session management.

The sendOTP() method initiates the email authentication flow by sending a one-time password (OTP) to the specified user's email address.

The loginUsingEmail() method verifies the provided OTP against the user's email. Upon successful verification, the user's session is established.

Example

import { useState } from 'react';
import { useOkto } from '@okto_web3/react-sdk';
 
function EmailAuthentication() {
    const oktoClient = useOkto(); 
 
    const [email, setEmail] = useState('');
    const [otp, setOtp] = useState('');
    const [token, setToken] = useState('');
 
    async function handleSendOTP() {
        try {
            const response = await oktoClient.sendOTP(email, "email"); 
            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(email, token, "email"); 
            console.log('OTP Resent:', response);
            setToken(response.token);
        } catch (error) {
            console.error('Error resending OTP:', error);
        }
    }
 
    async function handleVerifyOTP() {
        try {
            const session = await oktoClient.loginUsingEmail(email, 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="email" placeholder="Enter email" value={email} onChange={(e) => setEmail(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 email
async OktoClient.resendOTPResends an OTP using a previously generated token
async OktoClient.loginUsingEmailVerifies the OTP and logs in the user

Send OTP

async OktoClient.sendOTP(contact: string, method: "email") sends the OTP to the specified email address.

Parameters

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

Response

Success Response

Field NameTypeDescription
resultPromise<EmailSendOtpResponse>Resolves with a success response if the OTP is sent successfully

Resend OTP

async OktoClient.resendOTP(contact: string, token: string, method: "email") resends the OTP using the previously issued token.

Parameters

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

Response

Success Response

Field NameTypeDescription
resultPromise<EmailResendOtpResponse>Resolves with a success response if the OTP is resent successfully.

Login Using Email

async OktoClient.loginUsingEmail(contact: string, otp: string, token: string) verifies the OTP against the provided email and logs in the user. If successful, returns the user’s Smart Wallet Address.

Parameters

ParameterTypeDescriptionRequired
contactstringUser's email addressYes
otpstringOTP sent to the user email addressYes
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