JWT
Verify the tokens your sign-in provider signs. Point a guard at the provider's public keys, and the claims become the caller.
When someone signs in with a provider like Supabase Auth, Better Auth or Microsoft Entra ID, the provider hands their app a JWT. The app sends it to your API as a bearer token. verifyJwt checks it against the provider's public keys and turns its claims into the caller.
Verify in the guard
verifyJwt builds the guard for a bearer identity:
Your provider's page tells you its jwks, issuer, audience and algorithms. See Supabase Auth, Better Auth and Microsoft Entra ID.
Name it on your config
It is an identity like any other, so routes name it in their auth:
Send the token
The app that signed in sends the token in the Authorization header. Providers refresh it every few minutes, so the fetch client reads the current one in onRequest, before every request:
What it checks
Every token has to pass all of these, and none of them can be turned off:
- The signature, against a key from
jwks, with one ofalgorithms. issis yourissuer.audnames youraudience.expis set and has not passed, andnbf, when set, has.- The claims match your
claimsschema.
algorithms lists what your provider signs with. With jwks it takes public-key algorithms only, like RS256, ES256 or EdDSA. A token can never pick its own algorithm, and none is never accepted.
The jwks URL is https, or http on localhost. Keys are fetched on the first request and cached. When the provider rotates its keys, a token signed with a new one fetches them again.
Turn claims into the caller
context receives the verified claims, typed from your schema, and returns the identity's context. When the identity declares roles, it returns the role too:
It may be async, and it may call deny, like any guard:
A JWT stays valid until it expires, even after the person signs out. When your API needs to cut someone off sooner, look up their session here.
What a failure answers
- No token, or one that fails a check, answers
401with the identity'sWWW-Authenticatechallenge. - Keys that cannot be fetched throw, so the request answers
500. A provider outage never looks like a wrong password. - An option read from an environment variable that is not set throws on the first request, so it answers
500.kizuna generateloads no environment, so it never needs one.
A shared secret
Some providers sign with a secret your API shares instead of a key pair. Pass secret in place of jwks, with HS256, HS384 or HS512:
The secret is at least as long as the algorithm needs: 32 bytes for HS256, 48 for HS384 and 64 for HS512. Anyone holding it can sign tokens, so prefer public keys when your provider offers them.
OAuth
An oauth2 or openIdConnect identity tells clients where to get a token. That token is usually a JWT too, so verifyJwt checks it the same way. OAuth shows it with scopes as permissions.