Session Management

Login Using OAuth

Learn how to authenticate users using OAuth with the Okto SDK.

The loginUsingOAuth() method authenticates users using OAuth providers. It generates a session key pair, creates an authenticate payload, and sends it to the Gateway service. Upon successful authentication, it updates the user session.

Example

import { OktoClient } from "@okto_web3/core-js-sdk";
import { GoogleLogin } from '@react-oauth/google';
 
function LoginComponent() {
    // Initialize OktoClient
    const oktoClient = new OktoClient({
        environment: process.env.ENVIRONMENT,
        clientPrivateKey: process.env.CLIENT_PRIVATE_KEY,
        clientSWA: process.env.CLIENT_SWA,
    });
 
    // Using Google OAuth
    async function handleGoogleLogin(credentialResponse: any) {
        try {
            const user = await oktoClient.loginUsingOAuth({
                idToken: credentialResponse.credential,
                provider: "google",
            });
            console.log("Login successful:", user);
        } catch (error) {
            console.error("Login failed:", error);
        }
    }
 
    return (
        <div>
            <GoogleLogin onSuccess={handleGoogleLogin} />
        </div>
    );
}

Note

For error handling:

Method Overview

MethodDescription
async OktoClient.loginUsingOAuthAuthenticate user using OAuth

Login Using OAuth

async OktoClient.loginUsingOAuth(data: AuthData) authenticates users using OAuth providers and establishes their session.

Parameters

ParameterTypeDescriptionRequired
dataAuthDataAuthentication data containing the OAuth provider detailsYes

Where AuthData is one of:

TypeFieldsDescription
Google OAuth{ idToken: string; provider: 'google' }For authentication using Google OAuth
Okto Auth{ authToken: string; provider: 'okto' }For authentication using Okto token

Response

Success Response

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

On this page