createSiwxApiHandler()
createSiwxApiHandler(
options):object
Defined in: siwx-server/src/next.ts:159
Creates the SIWX route handlers of the durable (production) profile for a Next.js App Router catch-all route,
for example app/api/siwx/[...siwx]/route.ts. The action is taken from the last path segment:
GET|POST …/nonce: issues a nonce, stores it innonceStorefor 300 seconds and returns{ nonce }.POST …/verify: accepts a JSON{ message, signature }of up to 64 KB, runsverifySiwxPayloadwith the policy, consumes the nonce (so only nonces issued by/nonceare accepted, once), creates a session insessionStore, sets the session ID as anHttpOnlycookie and returns theSiwxSessionJSON. Responds 400 for a malformed body, 401 for a failed verification or nonce, 413 for a too large body.GET …/session: returns the stored session of the cookie, ornull.DELETE …/sessionorPOST …/logout: revokes the session in the store and clears the cookie.GET …/token(with thejwtoption): signs a JWT for the session of the cookie withsignSiwxJwtand returns{ token, expiresAt }withCache-Control: no-store. Responds 401 when there is no session or it has expired. The token expires afterjwt.ttlSeconds(600 by default) and never after the session.GET …/jwks(with thejwtoption): returns the JWKS ofjwt.signingKeyandjwt.previousKeyswithCache-Control: public, max-age=300. Give its URL to the services that verify the tokens.
Other paths return 404; unexpected errors are logged with console.error and return 500.
Parameters
options
Stores, policy, cookie, verification and JWT options.
Returns
object
Route handlers to export as GET, POST and DELETE.
DELETE
DELETE: (
req) =>Promise<Response> =universalHandler
Parameters
req
Request
Returns
Promise<Response>
GET
GET: (
req) =>Promise<Response> =universalHandler
Parameters
req
Request
Returns
Promise<Response>
POST
POST: (
req) =>Promise<Response> =universalHandler
Parameters
req
Request
Returns
Promise<Response>
Throws
If sessionStore or nonceStore is missing.
Throws
If jwt.issuer is empty.
Throws
If jwt.ttlSeconds is not an integer from 1 to 604800.
Example
// app/api/siwx/[...siwx]/route.ts
import { createSiwxApiHandler } from '@tuwaio/siwx-server/next';
import { sessionStore, nonceStore } from '@/lib/authStores';
const handler = createSiwxApiHandler({
sessionStore,
nonceStore,
policy: { expectedDomain: 'app.tuwa.io' },
});
export const { GET, POST, DELETE } = handler;