U-net Docs
Example

Next.js login

Use browser QR login and server-side assertion verification.

@union-networks/web-login@union-networks/server

Scenario

A Next.js app needs a passwordless account system keyed by U-net scoped IDs.

Steps

  1. Start login in a client component.
  2. Post `assertionJws` to a route handler.
  3. Verify with `@union-networks/server`.
  4. Set your app session cookie.

Code

tsClient
import {
  createLoginSession,
  isApprovedLoginResult,
  pollLoginSession,
  renderLoginQrPayload,
} from '@union-networks/web-login';

const session = await createLoginSession(
  {
    serviceId: 'example-shop',
    origin: window.location.origin,
    expiresInSeconds: 120,
  },
  { issuerBaseUrl: 'https://issuer.egress.live' },
);

renderQr(renderLoginQrPayload(session));

const result = await pollLoginSession(session.sessionId, {
  issuerBaseUrl: 'https://issuer.egress.live',
  intervalMs: 1500,
  timeoutMs: 120000,
});

if (isApprovedLoginResult(result)) {
  await fetch('/api/unet/session', {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ assertionJws: result.assertionJws }),
  });
}
tsRoute handler
import { verifyLoginAssertion } from '@union-networks/server';

export async function POST(request: Request) {
  const { assertionJws } = await request.json();

  const claims = verifyLoginAssertion(assertionJws, {
    secret: process.env.UNET_WEB_LOGIN_ASSERTION_SECRET!,
    serviceId: 'example-shop',
  });

  // Store claims.scopedUserId as the account id for your service.
  await createSession({ scopedUserId: claims.scopedUserId });
  return Response.json({ ok: true });
}