Add Auth Middleware

Learn how to configure authentication middleware to protect your Express application's backend endpoints.

The Wristband Auth SDK provides authentication middleware to restrict access to backend endpoints that require a valid authenticated user session.

Configure Authentication Middleware

Update your Wristband configuration module (src/wristband.ts) to create an authentication middleware instance using the SDK's createAuthMiddleware() method.

  1. Set authStrategies to ['SESSION'] to enforce session-based authentication.
  2. Pass the session options configured earlier in this guide to sessionConfig.

The following example demonstrates how to configure authentication middleware.

// src/wristband.ts

import { createWristbandAuth } from '@wristband/express-auth';
import { createWristbandSession } from '@wristband/express-auth/session';

export const wristbandAuth = createWristbandAuth({
  clientId: '<WRISTBAND_CLIENT_ID>',
  clientSecret: '<WRISTBAND_CLIENT_SECRET>',
  wristbandApplicationVanityDomain: '<WRISTBAND_APPLICATION_VANITY_DOMAIN>',
});

const sessionOptions = {
  secrets: '<your-generated-secret>',
};

export function wristbandSession() {
  return createWristbandSession(sessionOptions);
}

// NEW: Create an auth middleware instance that can be applied to Express
// routes to enforce authentication.
export const requireWristbandAuth = wristbandAuth.createAuthMiddleware({
  authStrategies: ['SESSION'],
  sessionConfig: { sessionOptions },
});
// src/wristband.ts

import { createWristbandAuth } from '@wristband/express-auth';
import { createWristbandSession } from '@wristband/express-auth/session';

export const wristbandAuth = createWristbandAuth({
  clientId: '<WRISTBAND_CLIENT_ID>',
  clientSecret: '<WRISTBAND_CLIENT_SECRET>',
  wristbandApplicationVanityDomain: '<WRISTBAND_APPLICATION_VANITY_DOMAIN>',
  dangerouslyDisableSecureCookies: true,
});

const sessionOptions = {
  secrets: '<your-generated-secret>',
  secure: false,
};

export function wristbandSession() {
  return createWristbandSession(sessionOptions);
}

// NEW: Create an auth middleware instance that can be applied to Express
// routes to enforce authentication.
export const requireWristbandAuth = wristbandAuth.createAuthMiddleware({
  authStrategies: ['SESSION'],
  sessionConfig: { sessionOptions },
});

Later in this guide, you will apply the requireWristbandAuth middleware to Express routes that require an authenticated user session.

Authentication Middleware Behavior

When a request is made to a protected endpoint, the Wristband Auth SDK automatically:

  • Validates the user's application session.
  • Refreshes expired access tokens when the required refresh token data is available in the session.
  • Extends the session expiration to support rolling sessions.
  • Persists session updates to the encrypted session cookie.

If the user's application session is missing, expired, or invalid, or a required access token refresh fails, the SDK rejects the request with a 401 Unauthorized response.

Applying Authentication Middleware

Creating the authentication middleware does not automatically protect your application's endpoints. You must explicitly apply the middleware to Express routes or routers that require an authenticated user session.

For more information about authentication middleware configuration and supported authentication strategies, see the Wristband Express Auth SDK documentation.


What’s Next

Now that you've configured authentication middleware, you're ready to implement your application's authentication endpoints.

Did this page help you?