Middleware API

Middleware transform framework and runtime specific request/response objects passed to Auth.handleRequest() into Lucia’s specific API.

Middleware#

type Middleware<_Args extends any[] = any> = (context: {
	args: _Args;
	env: "DEV" | "PROD";
	sessionCookieName: string;
}) => RequestContext;
Parameters#
nametypedescription
context.args_ArgsArguments passed to Auth.handleRequest() when the middleware is used
context.env"DEV" | "PROD"Project env configuration
sessionCookieNamestringSession cookie name defined in sessionCookie.name configuration (or default name if undefined)
Generics#
nameextendsdescription
_Argsany[]Parameter type of Auth.handleRequest() when the middleware is used

RequestContext#

type RequestContext = {
	request: LuciaRequest;
	setCookie: (cookie: Cookie) => void;
};
Properties#
propertytype
requestLuciaRequest

setCookie()#

Sets the provided cookie.

const setCookie: (cookie: Cookie) => void;
Parameters#
nametypedescription
cookieCookieCookie to set

Typing Middleware#

When creating a middleware, it’s crucial that you explicitly declare type Middleware so that parameters context.args is properly typed.

export const customMiddleware = (): Middleware<[Request]> => {
	return (context) => {
		return {
			// ...
		};
	};
};