@beinfi/nextjs
v0.3.0
Published
Infi building blocks for Next.js — drop-in App Router handlers for hosted auth and usage metering.
Readme
@beinfi/nextjs
Drop-in Next.js App Router handlers for Infi. Each block is one line — export it from a route and the capability is wired.
npm install @beinfi/nextjs @beinfi/sdkEnv:
INFI_SECRET_KEY=sk_test_... # server-side secret key
INFI_APP_SLUG=your-app # app slug the hosted login is scoped to
# optional
INFI_API_URL=https://api.beinfi.com
INFI_AUTH_BASE_URL=https://auth.beinfi.comAuth
Redirect to the hosted login:
// app/api/auth/login/route.ts
import { Login } from "@beinfi/nextjs";
export const GET = Login({
slug: process.env.INFI_APP_SLUG!,
redirectTo: "/api/auth/callback",
});Exchange the returned code for a session and set the cookie:
// app/api/auth/callback/route.ts
import { Callback } from "@beinfi/nextjs";
export const GET = Callback({
secretKey: process.env.INFI_SECRET_KEY!,
successUrl: "/dashboard",
});Read the session anywhere on the server:
import { getSessionToken } from "@beinfi/nextjs";
const token = await getSessionToken();
if (!token) redirect("/api/auth/login");Usage metering
Ingest events server-side (single event or { events: [...] }). Never ingest from the browser.
// app/api/usage/route.ts
import { Usage } from "@beinfi/nextjs";
export const POST = Usage({
secretKey: process.env.INFI_SECRET_KEY!,
// stamp the authed customer onto every event
resolveCustomerId: async (req) => (await getSessionCustomer(req)).id,
});Options
Login — slug, redirectTo (relative resolved against the request origin), authBaseUrl?,
state? (string or (req) => string).
Callback — secretKey, successUrl, baseUrl?, sessionMode? ("infi" | "byo"), cookie?
({ maxAgeSeconds, secure, path }), onAuth? (return a NextResponse to take over), onError?.
Usage — secretKey, baseUrl?, resolveCustomerId?.
