Your agent can build the integration.See the two prompts
Documentation navigation

Custom integration

Customize the managed router, connect your own sessions, or use the two manual login methods.

On this page

Install the current SDK source for these helpers. For existing 0.1.0 installations, use the versioned package walkthrough.

Use the Quickstart unless you need custom storage or routes. The managed router is available in the Node SDK; other backend SDKs provide the manual methods below. Browser-only authentication is deferred. Browser helpers always call your backend.

React, Vue, Astro, HTML and other frontend examples use the same managed handler.

Hono and Workers

The same managed handler uses standard Request/Response objects:

Hono / Workers
import { createAuth } from "@anyoauth/node/auth";

const auth = createAuth({
clientId: process.env.ANYOAUTH_CLIENT_ID,
clientSecret: process.env.ANYOAUTH_CLIENT_SECRET,
origin: "http://localhost:3000",
});

app.all("/auth/*", (c) => auth.handle(c.req.raw));

npm install @anyoauth/nodeManaged backend routerCurrent source example · published 0.1.0 predates these helpers

In Workers, construct the handler with credentials from your Worker bindings. The encrypted cookies work across processes and deployments with the same configuration.

Fastify

Use the SDK’s Node HTTP middleware with Fastify’s raw request and response:

Fastify backend
import { createAuth } from "@anyoauth/node/express";

const auth = createAuth({
clientId: process.env.ANYOAUTH_CLIENT_ID,
clientSecret: process.env.ANYOAUTH_CLIENT_SECRET,
origin: "http://localhost:3000",
});

const router = auth.router();

app.all("/auth/*", async (request, reply) => {
reply.hijack();
await router(request.raw, reply.raw);
});

npm install @anyoauth/nodeManaged backend routerCurrent source example · published 0.1.0 predates these helpers

reply.hijack() hands the response to the SDK, which writes the status, headers and cookies. The /express entrypoint is Node HTTP-compatible middleware; it does not require Express. Keep your other routes on Fastify as usual.

Configure the router

Defaults: authPath: '/auth', returnTo: '/', sessionMaxAge: 86400 (one day). Register ${origin}${authPath}/callback in your AnyOAuth project. returnTo must be a fixed local path; the router does not accept arbitrary return URLs from visitors.

Optional sessionSecret supplies a separate cookie key. Otherwise keys are derived from the backend client secret, separated by purpose and bound to the project, origin and auth path. Rotating the effective key invalidates existing cookie sessions. The SDK contract describes all configuration fields.

Connect your user database

JS
const auth = createAuth({
...config,
onLogin: async (profile) => {
await users.upsert(profile.subject, profile);
},
});

users is your database service. The hook runs after verification and before session creation. Identify users by subject, not email; do not merge accounts by email. The SDK authenticates users; your application still decides their permissions.

Use your own sessions

Supply an adapter instead of the default cookie session:

JS
const auth = createAuth({
...config,
session: {
read: (request) => sessions.readProfile(request),
create: (profile, request) => sessions.signIn(profile, request),
destroy: (request) => sessions.signOut(request),
},
});

sessions is your session service. read returns a normalized profile or null; create and destroy return Set-Cookie headers. Async implementations are supported. Locate sessions from their authenticated cookie, never a visitor-supplied user ID. The SDK still manages the private login cookie and callback validation.

Default sessions are stateless encrypted cookies. Logout clears this browser’s cookie; a copied cookie remains valid until expiry. Use the adapter for immediate server-side revocation, your existing database sessions, or profiles too large for a cookie.

Protect backend routes

Read the authenticated profile on the server before allowing access:

JS
app.get('/account', async (req, res) => {
const profile = await auth.getProfile(req);
if (!profile) return res.sendStatus(401);
res.json({ subject: profile.subject });
});

The Express adapter accepts its request object. The Fetch handler accepts a standard Request. A profile displayed or stored in browser JavaScript is not a backend credential.

Manage the login routes yourself

All backend SDKs retain the simple manual flow. Configure an AnyOAuth client with your backend credentials and call startLogin in your login route:

start Login
import { AnyOAuth } from "@anyoauth/node";

const client = new AnyOAuth({
clientId: process.env.ANYOAUTH_CLIENT_ID ?? "YOUR_CLIENT_ID",
clientSecret: process.env.ANYOAUTH_CLIENT_SECRET ?? "YOUR_CLIENT_SECRET",
baseUrl: process.env.ANYOAUTH_API_ORIGIN ?? "https://api.anyoauth.com",
});

// After this call, save loginAttempt in the browser-bound server session, persist it, then redirect to url.
const login = await client.startLogin({
provider: "google",
redirectUri: process.env.ANYOAUTH_REDIRECT_URI ?? "https://your-app.example/auth/callback"
});

npm install @anyoauth/nodeConfidential backend SDKCurrent source example · published 0.1.0 predates these helpers

Save login.loginAttempt in the initiating browser’s server session, persist it, then redirect to login.url. In your registered callback:

complete Login
import { AnyOAuth } from "@anyoauth/node";

const client = new AnyOAuth({
clientId: process.env.ANYOAUTH_CLIENT_ID ?? "YOUR_CLIENT_ID",
clientSecret: process.env.ANYOAUTH_CLIENT_SECRET ?? "YOUR_CLIENT_SECRET",
baseUrl: process.env.ANYOAUTH_API_ORIGIN ?? "https://api.anyoauth.com",
});

// Inside your callback route: session is the initiating browser's server-side session.
// pendingLogin is the loginAttempt you saved before redirecting. Reject a missing session/attempt.
const result = await client.completeLogin({
loginAttempt: session.pendingLogin
});

npm install @anyoauth/nodeConfidential backend SDKCurrent source example · published 0.1.0 predates these helpers

Keep the attempt for pending. Remove it for terminal results; only succeeded with a profile creates your application session. Rotate that session’s ID on login. Do not automatically retry uncertain retrieval: start a fresh login instead.

Existing createLoginSession, loginResult, authorization-code helpers and HTTP payloads remain compatible. API reference · Callback protection.