Add Auth Middleware
Set up authentication middleware to protect your session endpoint and any future secure routes.
Secure your session endpoint and future API routes by initializing an auth middleware instance with createMiddlewareAuth(). You will then integrate this middleware into the Next.js edge middleware configuration to enforce authentication on incoming requests.
Configure Auth Middleware
Create and configure the authentication middleware in your src/wristband.ts file. Ensure that the sessionEndpoint configuration option matches the exact path of the Session Endpoint you set up in the previous step.
// src/wristband.ts
import * as http from 'http';
import { createWristbandAuth, getPagesRouterSession, SessionOptions } from '@wristband/nextjs-auth';
export const wristbandAuth = createWristbandAuth({
clientId: '<WRISTBAND_CLIENT_ID>',
clientSecret: '<WRISTBAND_CLIENT_SECRET>',
wristbandApplicationVanityDomain: '<WRISTBAND_APPLICATION_VANITY_DOMAIN>',
});
const sessionOptions: SessionOptions = {
secrets: '<your-generated-secret>'
};
export function getSession(req: http.IncomingMessage, res: http.ServerResponse) {
return getPagesRouterSession(req, res, sessionOptions);
}
// NEW: Configure the auth middleware to require authenticated sessions.
// By default the auth middleware only requires authentication for
// requests to your Session Endpoint. However, later on, we will show
// how to update it to enforce authentication on other pages and APIs.
export const requireAuth = wristbandAuth.createMiddlewareAuth({
authStrategies: ['SESSION'],
sessionConfig: {
sessionOptions,
sessionEndpoint: '/api/auth/session' // Update this value to match your Session Endpoint path.
},
});Use Auth Middleware
After configuring the authentication middleware, invoke it inside either a proxy or middleware file, depending on your Next.js version:
- Next.js 16+: Create the proxy file at
src/proxy.ts - Next.js 15 and earlier: Create the middleware file at
src/middleware.ts
The following code examples demonstrate how to invoke your authentication middleware instance:
// src/proxy.ts
import { NextRequest } from 'next/server';
import { requireAuth } from 'src/wristband';
export async function proxy(request: NextRequest) {
return await requireAuth(request);
}
export const config = {
/*
* Match all paths except for:
* 1. /_next (Next.js internals)
* 2. /fonts (inside /public)
* 3. /examples (inside /public)
* 4. all root files inside /public (e.g. /favicon.ico)
*/
matcher: ['/((?!_next|fonts|examples|[\\w-]+\\.\\w+).*)'],
};// src/middleware.ts
import { NextRequest } from 'next/server';
import { requireAuth } from 'src/wristband';
export async function middleware(request: NextRequest) {
return await requireAuth(request);
}
export const config = {
/*
* Match all paths except for:
* 1. /_next (Next.js internals)
* 2. /fonts (inside /public)
* 3. /examples (inside /public)
* 4. all root files inside /public (e.g. /favicon.ico)
*/
matcher: ['/((?!_next|fonts|examples|[\\w-]+\\.\\w+).*)'],
};Updated 9 days ago
What’s Next
Now that your authentication endpoints are set up and configured, let's verify that they're working correctly.