Tokens & sessions

How Spica issues and verifies JWTs for identities and users, how refresh tokens extend sessions, and how to revoke them.

Access tokens

A successful login returns a JWT signed with the API's passport-secret:

JSON
{"scheme": "USER", "token": "eyJhbGciOiJIUzI1NiIs..."}

Send it as Authorization: <scheme> <token>. The payload of a user token looks like this:

JSON
{
  "_id": "6b0b1adea5611972c29c2631",
  "username": "reader1",
  "policies": ["6b0b1aeba5611972c29c2635"],
  "lastLogin": "2026-11-28T18:18:06.100Z",
  "iat": 1795889886,
  "exp": 1796062686,
  "aud": "spica.io",
  "iss": "https://api.example.com/api"
}

Lifetime

OptionIdentitiesUsers
Default lifetime--passport-identity-token-expires-in (2 days)--passport-user-token-expires-in (2 days)
Maximum a client may request--passport-identity-token-expiration-seconds-limit--passport-user-token-expiration-seconds-limit

Clients can ask for a shorter or longer token with expires (seconds) in the login body, up to the maximum:

JSON
{"identifier": "spica", "password": "spica", "expires": 900}

Refresh tokens

Login responses also set an HTTP-only cookie with a refresh token:

HTTP
Set-Cookie: refreshToken=eyJ...; Max-Age=259200; Path=passport/identity/session/refresh; HttpOnly; Secure; SameSite=None

When the access token expires, exchange it for a new one. Send the old access token in Authorization and the cookie with the request:

CallerEndpoint
IdentityPOST /passport/identity/session/refresh
UserPOST /passport/user/session/refresh
JavaScript
const response = await fetch("https://api.example.com/api/passport/user/session/refresh", {
  method: "POST",
  credentials: "include",
  headers: {Authorization: `USER ${expiredToken}`}
});
const {token} = await response.json();

Without the cookie the endpoint returns 401 with Refresh token does not exist.

OptionDefault
--passport-identity-refresh-token-expires-in3 days
--passport-user-refresh-token-expires-in3 days
--refresh-token-hash-secretDerived from the master key. Refresh tokens are stored hashed with it.

Manage refresh tokens

Administrators can list and revoke sessions:

EndpointActionPurpose
GET /passport/refresh-tokenpassport:refresh-token:indexList sessions.
GET /passport/refresh-token/:idpassport:refresh-token:showGet one.
PUT /passport/refresh-token/:idpassport:refresh-token:updateDisable or enable it: {"disabled": true}.
DELETE /passport/refresh-token/:idpassport:refresh-token:deleteDelete it.
JSON
[{"_id": "6b0b1924a5611972c29c25f1", "identity": "6b0b191d988a814684c8fec2", "created_at": "2026-11-28T18:10:44.019Z", "expired_at": "2026-12-01T18:10:44.019Z", "last_used_at": null}]
The refresh tokens page listing active sessions

Verify a token

Decode and verify a token on your own backend:

CallerEndpoint
IdentityGET /passport/identity/verify
UserGET /passport/user/verify

Send the raw token, without the scheme, in the Authorization header. A valid token returns its payload, an invalid one returns 400.

Terminal
curl https://api.example.com/api/passport/user/verify -H "Authorization: eyJhbGciOiJIUzI1NiIs..."

Inside functions, use Auth.verifyToken from @spica-devkit/auth or Identity.verifyToken from @spica-devkit/identity.

Revoke access

GoalHow
End one sessionDisable or delete its refresh token. The current access token stays valid until it expires.
End every session of a user nowSet deactivateJwtsBefore on the user to the current Unix time.
Lock a user outSet bannedUntil on the user.
Change what a token can doEdit or detach policies. Changes apply to existing tokens immediately.
Invalidate every token of the instanceRotate passport-secret. All users and identities must log in again.

Best practices

  • Keep access tokens short-lived (minutes to hours) and rely on refresh tokens for long sessions.
  • Store access tokens in memory in browsers, and let the refresh cookie survive reloads.
  • Never put tokens in URLs, except for WebSocket connections that can't send headers.
  • Rotate passport-secret only as an emergency measure, because it signs everyone out.