Email Authentication
Authenticate users via email using Okto's React Native 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 React, { useState } from 'react';
import { View, TextInput, Button, StyleSheet, Alert } from 'react-native';
import { useOkto } from '@okto_web3/react-native-sdk'; 
 
export default function EmailAuthentication() {
  const oktoClient = useOkto(); 
 
  const [email, setEmail] = useState('');
  const [otp, setOtp] = useState('');
  const [token, setToken] = useState('');
 
  const handleSendOTP = async () => {
    try {
      const response = await oktoClient.sendOTP(email, 'email'); 
      console.log('OTP Sent:', response);
      setToken(response.token);
    } catch (error) {
      console.error('Send OTP Error:', error);
      Alert.alert('Error', 'Failed to send OTP.');
    }
  };
 
  const handleResendOTP = async () => {
    try {
      const response = await oktoClient.resendOTP(email, token, 'email'); 
      console.log('OTP Resent:', response);
      setToken(response.token);
    } catch (error) {
      console.error('Resend OTP Error:', error);
      Alert.alert('Error', 'Failed to resend OTP.');
    }
  };
 
  const handleVerifyOTP = async () => {
    try {
      const session = await oktoClient.loginUsingEmail(email, otp, token, (session: any) => { 
        console.log('Session Callback:', session); 
      }); 
      console.log('Login Successful:', session);
    } catch (error) {
      console.error('Verify OTP Error:', error);
      Alert.alert('Login Failed', 'OTP verification failed.');
    }
  };
 
  return (
    <View>
      <TextInput
        placeholder="Enter email"
        value={email}
        onChangeText={setEmail}
        keyboardType="email-address"
        autoCapitalize="none"
      />
      <TextInput
        placeholder="Enter OTP"
        value={otp}
        onChangeText={setOtp}
        keyboardType="numeric"
      />
 
      <View>
        <Button title="Send OTP" onPress={handleSendOTP} />
      </View>
      <View>
        <Button title="Resend OTP" onPress={handleResendOTP} />
      </View>
      <View>
        <Button title="Verify OTP" onPress={handleVerifyOTP} />
      </View>
    </View>
  );
}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 |