lucia/middleware

astro()#

Middleware for Astro 1.x, 2.x, and 3.x.

const astro: Middleware;
Usage#
auth.handleRequest(context as APIContext);
nametype
contextAPIContext|Astro
import { astro } from "lucia/middleware";

const auth = lucia({
	middleware: astro()
	// ...
});

elysia()#

Middleware for Elysia.

const elysia: Middleware;
Usage#
auth.handleRequest(context as Context);
nametype
contextContext
new Elysia().get("/", (context) => {
	auth.handleRequest(context);
});

express()#

Middleware for Express 4.x and 5.x.

const express: () => Middleware;
Usage#
import { express } from "lucia/middleware";

const auth = lucia({
	middleware: express()
	// ...
});
auth.handleRequest(request as Request, response as Response);
nametype
requestRequest
responseResponse

fastify()#

Middleware for Fastify.

const fastify = () => Middleware;
Usage#
import { fastify } from "lucia/middleware";

const auth = lucia({
	middleware: fastify()
	// ...
});
auth.handleRequest(request as FastifyRequest, reply as FastifyReply);
nametype
requestFastifyRequest
replyFastifyReply

h3()#

Middleware for H3 (Nuxt 3).

const h3: () => Middleware;

Usage#

import { h3 } from "lucia/middleware";

const auth = lucia({
	middleware: h3()
	// ...
});
auth.handleRequest(event as H3Event);
nametype
eventH3Event

hono()#

Middleware for Hono.

const hono: Middleware;
Usage#
auth.handleRequest(context as Context);
nametype
contextContext
app.get("/", (context) => {
	auth.handleRequest(context);
});

lucia()#

The default middleware.

const lucia: () => Middleware;
Usage#
import { lucia as luciaMiddleware } from "lucia/middleware";

const auth = lucia({
	middleware: luciaMiddleware()
	// ...
});
auth.handleRequest(requestContext as RequestContext);
nametype
requestContextRequestContext

nextjs()#

While this is not deprecated, it will be replaced with nextjs_future() in the next major release.

Middleware for Next.js v12 and v13 - supports both pages and app directory. AuthRequest.setSession() is disabled when:

const nextjs: () => Middleware;
Usage#
import { nextjs } from "lucia/middleware";

const auth = lucia({
	middleware: nextjs()
	// ...
});
auth.handleRequest({
	req: req as IncomingMessage,
	res: res as OutgoingMessage | undefined
});

auth.handleRequest({
	request: request as NextRequest | null,
	cookies: cookies as Cookies
});
// for middleware and API routes in edge runtime
const authRequest = auth.handleRequest(request as NextRequest);
authRequest.setSession(); // error!
nametypedescription
requestNextRequest| nullShould be provided when using API routes
cookiesCookies
nametype
reqIncomingMessage
nametype
requestNextRequest

nextjs_future()#

A newer version of nextjs() middleware for Lucia v3. We recommend using this middleware for future proofing your codebase.

Middleware for Next.js v12 and v13 - supports both pages and app directory. AuthRequest.setSession() is disabled when:

const nextjs_future: () => Middleware;
Usage#
import { nextjs } from "lucia/middleware";

const auth = lucia({
	middleware: nextjs()
	// ...
});
auth.handleRequest({
	req: req as IncomingMessage,
	res: res as OutgoingMessage | undefined
});

auth.handleRequest(requestMethod as string, {
	cookies: cookies as Cookies,
	headers: headers as Headers
});
// for middleware and API routes in edge runtime
const authRequest = auth.handleRequest(req as IncomingMessage);
const authRequest = auth.handleRequest(request as NextRequest);
authRequest.setSession(); // error!
nametypeoptional
reqIncomingMessage
resOutgoingMessage
nametypedescription
requestMethodstringCan be upper or lower case
context.cookiesCookies
context.headersHeaders
nametype
reqIncomingMessage
nametype
requestNextRequest

node()#

Middleware for Node.js.

const node = () => Middleware;
Usage#
import { node } from "lucia/middleware";

const auth = lucia({
	middleware: node()
	// ...
});
auth.handleRequest(request as IncomingMessage, response as OutgoingMessage);
nametype
requestIncomingMessage
responseOutgoingMessage

sveltekit()#

Middleware for SvelteKit 1.x.

const sveltekit: () => Middleware;
Usage#
import { sveltekit } from "lucia/middleware";

const auth = lucia({
	middleware: sveltekit()
	// ...
});
auth.handleRequest(event as RequestEvent);
nametype
eventRequestEvent

web()#

Middleware for web standard request. AuthRequest.setSession() is disabled when using the web() middleware.

const web: () => Middleware;
Usage#
import { web } from "lucia/middleware";

const auth = lucia({
	middleware: web()
	// ...
});
const authRequest = auth.handleRequest(request as Request);
authRequest.setSession(); // error!
nametype
requestRequest

qwik()#

Middleware for Qwik City.

const qwik: () => Middleware;
Usage#
import { qwik } from "lucia/middleware";

const auth = lucia({
	middleware: qwik()
	// ...
});
auth.handleRequest(requestEvent as RequestEventLoader);
auth.handleRequest(requestEvent as RequestEventAction);

elysia()#

Middleware for Elysia.

const elysia: () => Middleware;
Usage#
import { elysia } from "lucia/middleware";

const auth = lucia({
	middleware: elysia()
	// ...
});
auth.handleRequest(context as Context);
nametype
contextContext