JWT and refresh tokens explained
An app signs a user in once and keeps them signed in for weeks, yet the token that proves who they are on each request expires after about an hour. Three pieces make that work: a short-lived access token, a refresh token, and a session that connects them. This article uses Supabase as the example throughout.
What is inside a JWT
A JSON Web Token, defined in RFC 7519, is a compact way of passing claims between two parties. The common form is three base64url-encoded parts joined by dots: a header naming the signing algorithm, a payload of claims as JSON, and a signature.
A Supabase access token always carries iss, aud, exp, iat, sub, role, aal, session_id, email, phone and is_anonymous. sub is the user's ID. role is the Postgres role the request runs as. session_id ties the token to one sign-in. exp and iat are Unix timestamps for expiry and issue. Optional claims include app_metadata and user_metadata.
With the default settings, exp is 3,600 seconds after iat. The aal claim records the authentication assurance level, which shows, for example, whether a second factor was completed for this session.
Signed, not encrypted
The payload is base64url-encoded, which is an encoding, not encryption. Anyone holding the token can decode and read every claim. A JWT is signed, not encrypted, so sensitive data does not belong in claims.
The signature does a different job. It proves the token was issued by the auth server and has not been changed since. Altering one character of the payload invalidates it.
JWTs can be either signed (JWS, three parts) or encrypted (JWE, five parts in compact form). A Supabase access token is a signed JWS. Supabase now supports asymmetric signing keys (ES256 or RS256), where a private key signs and a public key, published at a JWKS endpoint, verifies. The older approach of a single shared HS256 secret is discouraged.
How the database uses the token
Each request from the app sends the access token in the Authorization header. The Data API verifies the signature and expiry, runs the query as the token's role, and makes the claims available inside Postgres for that request. An RLS policy written as auth.uid() = user_id compares against the sub claim. No session lookup takes place; the signature is the proof.
Why access tokens are short-lived
Because verification only checks the signature and expiry, an issued access token cannot be withdrawn. A copied token works for whoever holds it until exp. Keeping the lifetime short limits that window. The access token also travels further than anything else in the system: it is attached to every request, while the refresh token only ever goes to the auth server. Supabase's default is one hour and its documentation recommends most apps keep it. Values below five minutes are discouraged, because of extra load on the auth server and problems with clock skew between machines.
What a refresh token does
The refresh token arrives alongside the access token at sign-in. RFC 6749 specifies that refresh tokens are sent only to the authorization server, never to resource servers. When the access token is close to expiry, the client sends the refresh token to Supabase Auth and receives a new access token and a new refresh token. The Supabase client libraries do this automatically; the Expo guide configures the client with autoRefreshToken: true.
In Supabase, refresh tokens never expire but can be used only once. A short reuse interval, 10 seconds by default, allows for legitimate overlaps such as two parts of an app refreshing at once. If a used refresh token is presented outside that interval, Supabase treats the session as compromised and revokes every refresh token belonging to it.
Sessions
A session is one sign-in, and in Supabase it is a row in auth.sessions. Its refresh tokens belong to it, and every access token carries its session_id. The session is the long-lived record. Access tokens are short-lived statements derived from it, reissued as they expire.
A session ends when the user signs out or changes their password. On Pro plans and above it can also be set to end after a period of inactivity, after a fixed lifetime, or when the user signs in on another device.
Where the app stores them
On a phone the session needs to survive restarts. Expo's SecureStore uses the Keychain on iOS and SharedPreferences encrypted with the Android Keystore on Android. Expo notes that some iOS releases have rejected values above roughly 2048 bytes. Supabase's Expo guide therefore stores an AES-256 key in SecureStore and keeps the encrypted session in AsyncStorage. On iOS, Keychain entries can persist after the app is uninstalled and reinstalled; on Android they do not.
What signing out means
Signing out has a device side and a server side. On the device, the stored session is removed. On the server, the session ends and its refresh tokens are revoked. In the JavaScript library the default scope is global, which ends every session for that user; local ends only the current one; others ends every session except the current one.
Access tokens that were already issued remain valid until their exp. Supabase's sign-out documentation states this explicitly. Where a sign-out has to take effect immediately, checking that the token's session_id still exists in auth.sessions closes the gap.
Custom claims
Supabase's custom access token hook runs before a token is issued and can add claims, such as an app-specific role that RLS policies read directly. Those claims reflect the data at the moment of issue. A change to the underlying data appears in the next token issued, not in tokens already out.
Sources
- IETF: RFC 7519, JSON Web Token
- IETF: RFC 7516, JSON Web Encryption
- IETF: RFC 6749, The OAuth 2.0 Authorization Framework
- Supabase docs: JSON Web Tokens
- Supabase docs: JWT claims reference
- Supabase docs: User sessions
- Supabase docs: Signing out
- Supabase docs: Row Level Security
- Supabase docs: Build a user management app with Expo React Native
- Supabase docs: Custom access token hook
- Supabase docs: Auth hooks
- Supabase docs: Custom OIDC provider
- PostgREST docs: Transactions
- Expo docs: SecureStore