Bionic Docs

Web quickstart

Add Bionic Login to a Node web app with the framework-neutral Web adapter and the browser redirect controller.

Run the starter on your test domain

Download the web starter from your verified test app. Extract it and open the login-web-starter directory. Use Node 22.9 or later and a fresh PostgreSQL 16+ database owned by the app. The archive includes the pinned Login Kit package.

npm ci --ignore-scripts
(umask 077 && cp .env.example .env)

Set DATABASE_URL and copy your six console settings into BIONIC_ISSUER, BIONIC_API_BASE, BIONIC_CLIENT_ID, BIONIC_CLIENT_SECRET, BIONIC_RP_ORIGIN and BIONIC_REDIRECT_URI on the server.

npm run migrate
npm start

Put the listener behind HTTPS on the registered host and preserve the original Host header. Visit /account while signed out and check for HTTP 401. Start sign-in from /, complete the hosted passkey prompt and reload the protected account page. Sign out and confirm /account returns 401 again. The app issues and revokes its own session. To launch on your live domain, follow production activation.

Integrate into an existing Node app

The Web adapter supplies handlers that take a standard Request and return a Response, so they mount in any Node router. This guide uses the Next.js App Router. If your app already uses NextAuth, use the NextAuth integration instead; don't put a Web adapter session into a NextAuth cookie.

You'll need: Node 22, PostgreSQL 16+ with pg 8.16.3 or Prisma 6.19.2, the Login Kit package from the starter at vendor/login-kit.tgz, and a registration.

Apply the database schema

The Login Kit exports its schema as postgresSchema from @bionic/login-kit/postgres. Apply it through your normal migration process before the routes accept requests. The kit never creates or migrates tables on its own.

Write your session policy

Your policy object connects Bionic to your existing users and sessions. It implements four callbacks, each called inside the kit's database transaction:

CallbackWhat it decides
assertCanStartWhether this browser may begin a sign-in, including whether an existing session may be linked or replaced.
issueSessionWhich account the verified identity belongs to, and the session you issue for it.
revokeSessionsEnding the sessions linked to a sign-in.
authorizeWhether a presented session is still valid for this request and resource.

Do all database work through tx.native, the transaction client the kit passes in. Don't make network calls from these callbacks or retry them yourself. The kit creates no users, workspaces or memberships; your policy does.

When authorize confirms a denial, throw one of the standard errors:

import { BionicError } from '@bionic/login-kit';

if (!sessionStillValid) {
  throw new BionicError('host_session_changed', { httpStatus: 401 });
}
if (!memberCanAccessResource) {
  throw new BionicError('host_policy_denied', { httpStatus: 403 });
}

Create the server module

lib/bionic-server.ts
import 'server-only';
import { createDurableLogin } from '@bionic/login-kit/durable';
import { createPostgresRepository, createPrismaDatabase } from '@bionic/login-kit/postgres';
import { createWebHandlers } from '@bionic/login-kit/web';
import { prisma } from '@/lib/prisma';
import { bionicConfig } from '@/lib/bionic-config'; // your registration
import { bionicPolicy } from '@/lib/bionic-policy'; // the four callbacks

const repository = createPostgresRepository({
  database: createPrismaDatabase(prisma),
  policy: bionicPolicy,
});

export const bionicLogin = createDurableLogin({ config: bionicConfig, repository });
export const bionicHandlers = createWebHandlers({ login: bionicLogin });

Using pg instead of Prisma? Wrap your pool with createPgDatabase(pool).

Mount the routes

Create one route file per handler under app/auth/bionic/, on the Node runtime:

app/auth/bionic/bootstrap/route.ts
import { bionicHandlers } from '@/lib/bionic-server';
export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';
export const GET = bionicHandlers.bootstrap;

Add sign-in to the page

Render the public issuer from your server configuration into a meta tag named bionic-issuer, as the starter does. The browser controller runs the redirect. It holds no secrets and never verifies a proof.

import { createBionicLoginController } from '@bionic/login-kit/browser';
import { createBionicButton } from '@bionic/login-kit/button';
import '@bionic/login-kit/button.css';

const login = createBionicLoginController({
  issuer: document.querySelector('meta[name="bionic-issuer"]').content,
  onError(error) { showRetryMessage(error.message); },
});

const button = createBionicButton({ action: 'login', size: 'default' });
button.addEventListener('click', () => login.start({ returnTo: '/account' }).catch(() => {}));
document.querySelector('#sign-in').append(button);

On your registered callback page, finish the sign-in:

login.consumeRedirectProof().catch(() => {});

The controller removes the proof from the URL before anything else runs, sends it to complete, then follows the return path your server gives back.

Check sessions on protected resources

The session route only proves the current request. For each protected resource, call bionicLogin.authorize with the presented session and your own resource policy. For a write, pass your write as the transactional action so the write and the final session check share one database transaction.

HTTP contract

HandlerRequestSuccess
bootstrapGET, no body204. Sets a lifecycle cookie when one is needed.
startPOST JSON, { "returnTo": "/app" } or {}200, { "identityUrl": "https://…" }
completePOST JSON, { "state": "…", "id_token": "…" }200, { "returnTo": "/app" }. Sets the session cookie.
logoutPOST, no body204. Clears the browser and session cookies.
sessionGET, no body204 when the session is currently authorized.

Failures return only { error, retryable, restartRequired }. After start or complete has been accepted, any failure needs a fresh sign-in.

Cookies

Production cookies are __Host-bionic_lifecycle, __Host-bionic_browser and __Host-bionic_session, all Secure; HttpOnly; SameSite=Lax; Path=/. To rename them, override all three together:

createWebHandlers({
  login: bionicLogin,
  cookies: {
    names: {
      lifecycle: '__Host-myapp_bionic_lifecycle',
      browser: '__Host-myapp_bionic_browser',
      session: '__Host-myapp_session',
    },
  },
});

Bionic logout ends Bionic-linked sessions only. Route Google or email logout through your existing logout path.

Behind a proxy

Every handler checks Request.url against your exact rpOrigin, and the adapter never reads Forwarded or X-Forwarded-* headers. If your proxy hands the app an internal URL, validate the proxy, host and TLS boundary yourself and rebuild the public request:

import { validatedPublicRequest } from '@/lib/trusted-ingress';

export const bionicHandlers = createWebHandlers({
  login: bionicLogin,
  trustedRequest: validatedPublicRequest, // (request: Request) => Request
});

trustedRequest must be synchronous, reject untrusted traffic and keep the method, headers and body unchanged.

Local development

HTTP on localhost works only when you opt in on both sides: allowInsecureLoopback: true in your configuration, cookies: { secure: false, names: { … } } with three custom names (the __Host- names require HTTPS), and a non-production NODE_ENV.

On this page