Add Auth Middleware
Set up authentication middleware to secure your Session Endpoint and guard all future protected routes.
Secure your Session Endpoint and future API routes by initializing an authentication middleware instance with createMiddlewareAuth(). You can then integrate this instance directly into your Next.js middleware file to intercept and enforce authentication on all incoming requests.
Configure Auth Middleware
Create and configure the authentication middleware in your src/wristband.ts file. Make sure to update the sessionEndpoint configuration to match the path of the Session Endpoint you created earlier.
Initialize and set up the authentication middleware directly inside your src/wristband.ts file. During configuration, update the sessionEndpoint field to match the exact path of the Session Endpoint you created in the previous step.
// src/wristband.ts
import * as http from 'http';
import { createWristbandAuth, getSessionFromRequest, 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 getRequestSession(request: NextRequest) {
return getSessionFromRequest(request, 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 auth middleware, call it from your project's routing layer. The exact location depends 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
Below are code snippets showing how to invoke your auth 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 15 days ago
What’s Next
Now that your authentication endpoints are set up and configured, let's verify that they're working correctly.