Next.js
Server sessions for the Next.js App Router.
Import these from @monarcode/session-kit/next. To set it up, see
Next.js.
Functions
nextJar
The CookieJar for cookies() from next/headers, to pass to serverAuth from @monarcode/session-kit/server.
It's read-only unless you pass writable: true, which only a Server Function or a Route Handler may do, as Next throws when a Server Component sets a cookie. A read-only jar can't refresh, so refresh before rendering, in Proxy, with proxyJar.
The read-only jar follows the writable jar for the same store, so a sign-in, sign-out or refresh through the writable one shows in it. The same store and writable always give the same jar.
nextSession
Gives a Next.js app its session (NextSession), over nextJar and proxyJar. Make it once, beside serverAuth, in a module only server code imports.
Server Components read it with get and guard pages with require. Server Functions and Route Handlers sign in and out with signIn and signOut. proxy.ts refreshes with refresh, as rendering can't set cookies.
Throws INVALID_OPTIONS without cookies or redirect, when loginPath doesn't start with /, or when within is negative.
proxyJar
The CookieJar for a request in Next's Proxy (proxy.ts). It reads request's cookies and sets new ones on response. Use it with serverAuth.refreshIfDue, so the pages that render the request see a fresh token through nextJar.
Return response from Proxy, or the new cookies are lost. Pages on the Edge runtime may still read the old cookie. Every call with the same response returns the same jar.
sessionRoute
Makes the Route Handlers for your session route, which answers createMirrorAuth's endpoint: the browser client reads the session, signs out and gets the access token through it. Pass cookies from next/headers (NextSessionRouteOptions).
It answers this site's own pages only, as sessionHandler from @monarcode/session-kit/server explains, and serves the access token only when createServerAuth has clientCredentials. Sign-in stays your own Server Function.
Throws INVALID_OPTIONS without cookies.
Types
NextCookies
A request's cookie store, as cookies() from next/headers resolves it. nextJar reads and writes it, and Next's own store fits.
NextJarOptions
Options for nextJar.
Prop
Type
NextSession
The session in a Next.js app, made by nextSession: reads and guards for Server Components, the request's session for Server Functions and Route Handlers, and the refresh for Proxy.
Members:
get: Resolves who is signed in, from the cookie, ornull. It doesn't set cookies, so it works in Server Components. Proxy refreshes first.require: Resolves the session, or redirects to the sign-in page with?redirectTo=set topath, the page's own path, as Server Components don't know their URL. Next adds abasePathto redirects from pages and Server Functions, but not from Route Handlers. With abasePath, a Route Handler should read the session withgetand answer 401 instead.redirectIfSignedIn: For your sign-in page: redirects a signed-in user toredirectTo, usually theredirectTosearch param, made safe withsafeReturnTo. It resolves for a signed-out user, or when the session can't be read.readOnly: The request's session for Server Components, which doesn't set cookies. Read itscredentialsto call your API while rendering.writable: The request's session for Server Functions and Route Handlers, which sets cookies.signIn,signOutandupdateUserare shortcuts to it. Use it for the rest, such as renewing the access token.signIn: Signs in with your backend's tokens, setting the cookie, asRequestAuth'ssignIndoes overwritable(). Use it in Server Functions and Route Handlers.signOut: Signs out and clears the cookie, asRequestAuth'ssignOutdoes overwritable(). Use it in Server Functions and Route Handlers.refresh: For Proxy: refreshes access that expires withinwithinseconds, setting the new cookie onresponse, and resolvesresponse. A failed refresh is left for the page, which reads the session as it is.updateUser: Replaces the signed-in user in the cookie, such as after a profile edit, asRequestAuth'supdateUserdoes overwritable(). Use it in Server Functions and Route Handlers.
NextSessionOptions
Options for nextSession. Pass Next's own cookies and redirect, as this entry point doesn't import next.
Prop
Type
NextSessionRoute
The Route Handlers sessionRoute makes, for GET and POST.
NextSessionRouteOptions
Options for sessionRoute. Pass Next's own cookies, as this entry point doesn't import next.
Prop
Type
ProxyRequest
The request proxyJar reads cookies from. A NextRequest fits.
ProxyResponse
The response proxyJar sets cookies on. A NextResponse fits.