Skip to main content
Client-side authentication state is convenient, but it is never authoritative. A user’s browser can claim any identity — the only way to know you are talking to who you think you are is to verify the session token on your server before granting access to any protected resource. SocketFi makes this straightforward with the @socketfi/server package, which validates the cryptographic signature on every access token and returns the verified user identity and wallet address.
Always verify tokens server-side. Never trust a wallet address or user ID supplied directly by the client. Granting access based solely on client-provided identity claims is a critical security vulnerability. Every request to a protected endpoint must pass through verifyAuth() before your application logic runs.

Installation

Install the SocketFi Server SDK in your backend project:
The package is a pure Node.js library with no browser-side dependencies. It works in any Node.js server framework — Express, Fastify, Next.js API routes, Hono, and others.

Verifying a Token

Import verifyAuth and pass it the raw JWT string extracted from the Authorization: Bearer header:
verifyAuth is asynchronous. It validates the token’s cryptographic signature, checks all standard JWT claims (expiration, issuer, audience), and returns a typed result object.

Verification Results

verifyAuth always returns one of two shapes — a successful result or a failure result. Check result.valid before accessing any other fields.

Successful Verification

Failed Verification


Error Codes


Express Middleware

The cleanest way to protect your Express routes is an authentication middleware that runs verifyAuth before your route handlers:
Extend the express.Request type to include socketfi so TypeScript recognizes the property in downstream handlers:

Extracting User Identity and Wallet Address

Once verification succeeds, result.user.id and result.wallet.address are the authoritative identifiers for the requesting user. Use them to look up records, authorize operations, and attribute on-chain activity.
Store result.user.id as your foreign key in your own database rather than result.wallet.address. The user ID is stable across credential rotations. The wallet address is stable too, but querying by the SocketFi user ID is the more semantically correct way to link your records.

Full Verification Example

Here is a complete, self-contained example that covers token extraction, verification, error handling, and identity usage:

Development and Testing Utilities

The @socketfi/server package exposes one additional utility that is useful during local development and testing.

clearKeyCache()

verifyAuth() caches the public signing keys it fetches from SocketFi to avoid redundant network requests on every verification call. In production this cache is managed automatically and you never need to touch it. During local development or in test suites — for example, when rotating keys in a staging environment or writing unit tests that stub the verification flow — you can flush the cache manually:
clearKeyCache() is a development and testing utility. Production applications should never need to call it — the SDK manages key caching automatically and refreshes keys when necessary.

Security Checklist

Before shipping to production, confirm that your backend satisfies all of these requirements:
1

Call verifyAuth on every protected endpoint

No exceptions. Every API route that returns user data, reads wallet state, or performs any authenticated action must verify the token before proceeding.
2

Return 401 for all verification failures

Do not leak information about why a token failed. Return 401 Unauthorized for every result.valid === false case regardless of the error code. You can log the error code internally for debugging.
3

Use result.user.id as your identity source of truth

Never trust a user ID or wallet address submitted in a request body or query parameter. The only authoritative identity is what verifyAuth returns.
4

Log INVALID_SIGNATURE and INVALID_ISSUER errors

These codes indicate potential token forgery or a misconfigured client. Alert your security monitoring system when they appear.
5

Run your server over HTTPS

Tokens transmitted over plain HTTP can be intercepted. All production endpoints must use TLS.