Skip to main content
Authentication is the gateway to every SocketFi capability. Before a user can view their wallet, request a transaction, or interact with any on-chain contract, they must first authenticate. SocketFi handles this through a hosted, passwordless flow built on passkeys — the same FIDO2 standard used by Apple, Google, and Microsoft. A single authenticate() call covers both new user registration and returning user sign-in; the SDK detects which flow to run automatically. This guide covers the full authentication lifecycle: passkey registration vs. sign-in, session storage, backend token verification, protected routes, session expiration, re-authentication, and logout.

How authentication works

When you call socketfi.authenticate(), the SDK opens the SocketFi hosted authentication flow. For a new user, the flow creates a passkey on their device and provisions a smart wallet on Stellar. For a returning user, the flow prompts for their existing passkey and creates a fresh session. In both cases, your app receives a Session object containing an access token and wallet information.
Authentication proves identity — it does not authorize individual transactions. Every state-changing contract call goes through a separate approval step when you call requestTransaction().

Calling authenticate()

The call signature is the same for both registration and sign-in:
The returned Session object looks like this:

Session storage and management

After authentication succeeds, persist the session so users stay logged in across page reloads or app restarts.
Store the session in localStorage and rehydrate it on mount:
src/auth/provider.tsx

Backend token verification

Never rely on client-side session state to grant access to backend resources. Always verify socketfiAccessToken server-side using the @socketfi/server package.
The verifyAuth() function validates the token signature and returns the verified user and wallet:

Express middleware example

src/server/middleware/requireAuth.ts
Apply it to protected routes:
src/server/routes/wallet.ts

Protected routes

React (web)

Wrap sensitive routes in a guard component that redirects unauthenticated users to your login page:
src/components/ProtectedRoute.tsx

React Native

Use a navigator that conditionally renders the authenticated or unauthenticated stack:
src/navigation/RootNavigator.tsx

Handling session expiration and re-authentication

Sessions expire after a period of inactivity. When an API call returns 401, catch it and trigger re-authentication:
src/api/client.ts

Logout

Logout removes the local session state. It does not delete the user’s wallet, revoke the passkey, or affect any on-chain assets.
After logout, redirect the user to your login screen and reset any component state that depends on the session.

Error handling for auth failures

Wrap every authenticate() call in try/catch. The most common errors are:
You can also configure a global error handler on the client instance to capture all SDK errors in one place:
src/socketfi/client.ts

Production checklist

  • ✅ Verify socketfiAccessToken server-side with @socketfi/server before granting API access
  • ✅ Store tokens in encrypted storage (expo-secure-store on mobile, sessionStorage for stricter web security)
  • ✅ Handle session expiration and re-authenticate transparently
  • ✅ Protect all sensitive routes with an auth guard
  • ✅ Show loading indicators during the passkey prompt and session restore
  • ✅ Monitor authentication failures in your observability tool