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:
- 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 email |
async OktoClient.resendOTP | Resends an OTP using a previously generated token |
async OktoClient.loginUsingEmail | Verifies 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
| Parameter | Type | Description | Required |
|---|---|---|---|
contact | string | User's email address | Yes |
method | string | OTP delivery method (email or whatsapp) | Yes |
Response
Success Response
| Field Name | Type | Description |
|---|---|---|
result | Promise<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
| Parameter | Type | Description | Required |
|---|---|---|---|
contact | string | User's email address | Yes |
token | string | The token received from the previous sendOTP request | Yes |
method | "email" | OTP delivery method (email or whatsapp) | Yes |
Response
Success Response
| Field Name | Type | Description |
|---|---|---|
result | Promise<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
| Parameter | Type | Description | Required |
|---|---|---|---|
contact | string | User's email address | Yes |
otp | string | OTP sent to the user email address | 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 |
Learn
Implement secure, passwordless login with Okto React SDK using email-based OTPs. Learn session handling, token verification, and automated OTP delivery.
Learn
Implement secure, passwordless login with Okto React SDK using WhatsApp-based OTPs. Learn session handling, token verification, and automated OTP delivery.