Add Auth Middleware

Initialize the authentication middleware instance necessary for securing your Express application.

To secure your endpoints, you must instantiate an authentication middleware. Later sections will demonstrate how to attach this middleware to Express routes to validate incoming traffic.

Create Auth Middleware

Use the createAuthMiddleware() function in your Wristband configuration file to establish the middleware instance. This component relies on the SESSION authentication strategy, ensuring that any Express route utilizing it mandates a valid session cookie for incoming requests.

// 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 },
});


What’s Next

Next, you'll use the Wristband SDK to create the necessary authentication endpoints in your Express server.

Did this page help you?