Skip to main content

Sign In With Passkey

This guide shows how to authenticate with a passkey, using LoginID.

tip

We recommend always offering passkey autofill as an option for your users, as it provides the best user experience.

Prerequisites

Setup SDK

npm i @loginid/websdk3

Import and initialize an instance:

import { LoginIDWebSDK } from "@loginid/websdk3";

const lid = new LoginIDWebSDK({
baseUrl: process.env.LOGINID_BASE_URL,
});

Authenticate With Passkey

Call the authenticateWithPasskey method, passing the email or username. The user will authenticate with a passkey, and the response will include a LoginID token.

note

The LoginID token is stored locally, allowing it to be used across different components or pages without needing to re-authenticate.

import React, { useState } from "react";
import { useAuth } from "../../contexts/AuthContext";

import { LoginIDWebSDK } from "@loginid/websdk3";

const lid = new LoginIDWebSDK({
baseUrl: process.env.REACT_APP_LOGINID_BASE_URL,
});

const Login: React.FC = () => {
const [username, setUsername] = useState<string>("");
const [error, setError] = useState<string>("");
const { setAuthUser } = useAuth();

const handlePasskeyLogin = async (e: React.FormEvent) => {
e.preventDefault();

try {
// User authenticates with passkey
const authResult = await lid.authenticateWithPasskey(username);

if (authResult.isAuthenticated) {
// You can return LoginID token to your backend for verification (authResult.token)

setAuthUser(user);
} else {
// Default to your fallback CIAM authentication method
}
} catch (e) {
if (e instanceof Error) {
setError(e.message);
}
}
};
};

If you wish to use webauthn autofill, you can see Passkey Autofill guide

Once you have received the result LoginID token, you can send it to your backend, and verify it. For detailed technical instructions, refer to this section on verifying LoginID tokens.