Skip to main content
Every time a user successfully authenticates, SocketFi creates a session — a short-lived proof of identity that your application uses to identify the user and authorize requests to your backend. Sessions are separate from the wallet itself: a session can expire or be cleared without affecting the wallet, its balances, or any on-chain state. Understanding sessions is essential for building a secure, responsive application on top of SocketFi.

What Is a Session?

A session represents a recently verified user. It contains two pieces of information:
The socketfiAccessToken is a signed JSON Web Token (JWT). It encodes the user’s identity and wallet association, and it is cryptographically signed by SocketFi so your backend can verify it without making an additional network call to SocketFi’s servers.

Using the Access Token

Attach the access token to every request your application makes to your own backend. Use the standard Authorization: Bearer header:
Your backend receives this header, verifies the token with @socketfi/server, and extracts the user identity and wallet address. See Backend Verification for the complete server-side flow.
Never use the socketfiAccessToken as a primary key or store it as a user identifier. Use session.userProfile.id as your stable foreign key. The access token changes every time the user re-authenticates.

Session Storage

Where you store the session depends on your platform.
For web applications, localStorage works well for persisting the token across page reloads. Pair it with React state for in-memory access during the session.
localStorage is accessible to JavaScript on the same origin. If your application has a Content Security Policy and is not susceptible to XSS, this is an acceptable trade-off for usability. For higher-security applications, consider storing the token in an httpOnly cookie set by your own backend.

React Context Example

For most React applications, a Context provider is the cleanest way to share session state across your component tree.
Use it in your components:

Session Expiration

Sessions are intentionally short-lived. When a socketfiAccessToken expires, your backend will return an HTTP 401 response to requests that include it. Design your application to handle this without disrupting the user experience:
Proactively decode the JWT on the client and check the exp claim before making requests. This lets you trigger re-authentication before an API call fails, resulting in a smoother user experience.

Sessions vs. Wallets

It is important to understand that sessions and wallets are completely independent.
Signing out does not delete, freeze, or affect the wallet in any way.

Common Session Errors


Security Best Practices

  • Always verify tokens server-side. Frontend session state tells you who the user claims to be. The verifyAuth() call tells you who they actually are.
  • Use HTTPS exclusively. Session tokens transmitted over plain HTTP can be intercepted. All production traffic must be encrypted.
  • Store minimal data. Persist only the token and the user profile. Do not store wallet addresses, balance data, or other wallet state in session storage.
  • Handle expiration gracefully. Build automatic re-authentication into your data-fetching layer so users are not unexpectedly locked out.