Skip to content

Quick start guide

Greg Bowler edited this page Nov 1, 2026 · 2 revisions

In this guide we add login and a greeting to a small application. Every return route must run the same authentication setup.

1. Install the package

The client requires Node.js 22 or later. Install it in your application:

npm install @authwave/typescript-client

The package provides ESM exports with TypeScript declarations and works from both JavaScript and TypeScript.

2. Register the application

Register a deployment and configure its client return URI. Put the complete client key in AUTHWAVE_CLIENT_KEY, outside public files and version control. The final underscore separates deployment ID from the raw 32-byte secret. Do not decode or transform the supplied key.

3. Create the shared setup

For Node.js, create server.mjs:

import { createServer } from 'node:http';
import { createNodeHandler, MemorySessionStore } from '@authwave/typescript-client/node';

const clientKey = process.env.AUTHWAVE_CLIENT_KEY;
if (!clientKey) throw new Error('Set AUTHWAVE_CLIENT_KEY');
const store = new MemorySessionStore(); // local development only
const handle = createNodeHandler({ clientKey, store, secureCookie: false }, (auth, req) => {
  const path = new URL(req.url, 'http://localhost:3000').pathname;
  if (path === '/login') { auth.setReturnUri('/'); auth.login(); }
  if (path === '/logout') { auth.logout('/'); }
  return JSON.stringify(auth.isLoggedIn() ? { id: auth.getAuthUser().id, email: auth.getAuthUser().email } : { login: '/login' });
});
createServer(handle).listen(3000);

Run node server.mjs. Configure the provider's return URI as http://localhost:3000/ for local development. Production needs HTTPS, secureCookie: true (the default), and a shared persistent SessionStore. The handler returns a body; the adapter saves the session before writing it.

4. Add a login endpoint

The Node.js example above handles /login. In Express, mount express-session first and share a wrapped handler across all return routes:

import express from 'express';
import session from 'express-session';
import { createExpressHandler } from '@authwave/typescript-client/express';

const app = express();
app.use(session({
  secret: process.env.SESSION_SECRET, // independent session-signing secret
  resave: false, saveUninitialized: false,
  cookie: { httpOnly: true, sameSite: 'lax', secure: false }, // local HTTP only
}));
app.use(createExpressHandler({ clientKey: process.env.AUTHWAVE_CLIENT_KEY }, (auth, req) => {
  const path = new URL(req.originalUrl, 'http://localhost:3000').pathname;
  if (path === '/login') { auth.setReturnUri('/'); auth.login(); }
  if (path === '/logout') auth.logout('/');
  return auth.isLoggedIn() ? { id: auth.getAuthUser().id, email: auth.getAuthUser().email } : { login: '/login' };
}));
app.listen(3000);

Install express and express-session in the application. Use an appropriate production session store, strong SESSION_SECRET, secure cookies and trusted proxy configuration for your deployment. The adapter calls session.save() before sending redirects or bodies. Return the response body from the handler; use Express error middleware for application errors.

5. Show the visitor's state

The examples return JSON to avoid inserting unescaped user data into HTML. Check isLoggedIn() before calling getAuthUser() and escape user data when rendering HTML.

For Next.js App Router, place the following in app/[[...authwave]]/route.ts (a standalone example covering /, /login and /logout; avoid overlapping application pages):

import { createNextHandler, MemorySessionStore } from '@authwave/typescript-client/next';
export const runtime = 'nodejs';
const clientKey = process.env.AUTHWAVE_CLIENT_KEY;
if (!clientKey) throw new Error('Set AUTHWAVE_CLIENT_KEY');
const store = new MemorySessionStore(); // local development only
export const GET = createNextHandler({ clientKey, store, secureCookie: false }, (auth, request) => {
  const path = new URL(request.url).pathname;
  if (path === '/login') { auth.setReturnUri('/'); auth.login(); }
  if (path === '/logout') auth.logout('/');
  return Response.json(auth.isLoggedIn() ? { id: auth.getAuthUser().id, email: auth.getAuthUser().email } : { login: '/login' });
});

For an existing Next.js site, export wrapped handlers on explicit login, logout and callback routes, and make setReturnUri() point to the wrapped callback route. Share the same persistent store and cookie options across routes. This integration targets Route Handlers, not Edge middleware or Client Components. Session cookies are HTTP-only and contain an opaque ID; authentication data stays in the store.

Visit /login, complete provider authentication, and follow the two return redirects. The final response reads the stored user. An already logged-in login() returns normally.


Continue with Logging out and Working with the authenticated user.

Clone this wiki locally