Node.js (Traditional Web App)

This tutorial demonstrates how to add user login to a Node application using BlitzWare.

Build a secure server‑rendered web app using BlitzWare OAuth 2.0 Authorization Code + PKCE with the BlitzWare Node SDK.

circle-info

This tutorial is based on the example apparrow-up-right.

1) Configure BlitzWare

Get Your Application Keys

You will need some details about your application to communicate with BlitzWare. You can get these details from the Application Settings section in the BlitzWare dashboard.

You need the Client ID and Client Secret (given when app was created).

Configure Redirect URIs

A redirect URI is a URL in your application where BlitzWare redirects the user after they have authenticated. The redirect URI for your app must be added to the Redirect URIs list in your Application Settings under the Security tab. If this is not set, users will be unable to log in to the application and will get an error.

2) Install the BlitzWare Node Web SDK

Run the following command within your project directory to install the BlitzWare Node Web SDKarrow-up-right:

3) Configure environment

Create a .env file:

4) Express setup

5) Koa setup

6) How it works

  • PKCE + state: The SDK generates a state and PKCE verifier/challenge.

    • state defends against CSRF

    • PKCE protects the code exchange

Automatic Routes

When you use expressAuth() or koaAuth(), the following routes are created automatically:

  • GET /login - Initiates OAuth login flow

  • GET /logout - Logs out user and clears session

  • GET /callback - OAuth callback handler

Protection

The SDK provides middleware to protect routes and enforce authorization:

Authentication Middleware

  • expressRequiresAuth() / koaRequiresAuth() - Ensures a user is logged in before accessing a route. Redirects to /login if not authenticated.

Express Example:

Koa Example:

Role-Based Authorization Middleware

  • expressRequiresRole(role) / koaRequiresRole(role) - Ensures a user has a specific role. Returns 403 Forbidden if the user doesn't have the required role.

Express Example:

Koa Example:

Note: These middleware functions check for roles stored in user.roles array. They do not perform token introspection by default.

Logout (front-channel)

The SDK performs a front-channel logout: it serves a small HTML page that POSTs to the auth service (so auth-service cookies are sent) and then redirects back to your app.


If you need additional features — token introspection on each request, automatic refresh using session.refreshToken, or other behavior — open an issue or PR and I can add an opt-in option such as requiresAuth({ validateToken: true }).


License: MIT

Last updated