npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@appdirect/auth-bff

v0.0.7

Published

AppDirect OIDC BFF SDK for Node.js backends

Readme

@appdirect/auth-bff

AppDirect OIDC Backend-for-Frontend (BFF) SDK for Node.js backends.

This package extracts AppDirect OAuth2/OIDC login, FEJWT session management, and token refresh into a reusable library. The browser never sees client secrets — only HttpOnly cookies and controlled API endpoints.

Features

  • Authorization Code flow with AppDirect (/oauth2/authorize, /oauth2/token, /oauth2/userinfo)
  • AppDirect session exchange (/auth/token, /auth/refresh) for application session tokens
  • FEJWT-as-session — session cookie stores the AppDirect FEJWT (~5 min TTL)
  • Lazy auto-refresh — refreshes FEJWT only when expired (~once per TTL), via ensureValidFeJwt()
  • Single-flight refresh — concurrent requests share one IdP refresh per warm instance
  • Refresh token rotation via HttpOnly refresh_token cookie
  • OAuth state CSRF protection via short-lived oauth_state cookie
  • CHIPS (Partitioned) cookies for cross-site iframe embeds (preview, shared links)
  • Framework-agnostic core + optional adapters for Next.js and Node route handlers
  • Full TypeScript types

Install

Published to the public npm registry:

npm install @appdirect/auth-bff

For local development against this repo:

npm install file:../appdirect-auth-bff

Or install from a packed tarball:

npm install ./appdirect-auth-bff-0.0.1.tgz

Quick start — core SDK

Use createAppDirectAuth() in any Node 18+ backend. Flows return data (cookies, redirects, user) — you wire HTTP yourself:

import { createAppDirectAuth } from '@appdirect/auth-bff';

const auth = createAppDirectAuth({
  issuerBaseUrl: process.env.APPDIRECT_ISSUER_BASE_URL!,
  clientId: process.env.APPDIRECT_CLIENT_ID!,
  clientSecret: process.env.APPDIRECT_CLIENT_SECRET!,
  appBaseUrl: process.env.APP_BASE_URL ?? 'http://localhost:3000',
});

// Login: auth.buildLoginRedirect() → authorizeUrl + cookiesToSet
// Callback: auth.handleCallback({ code, state, cookies })
// Session: auth.getMe(), auth.getFeJwt(), auth.refreshSession()

See Usage Reference for the full API.

Quick start — Next.js App Router

1. Configure auth (lib/auth.ts):

import { createNextAuthHandlers } from '@appdirect/auth-bff/next';

export const authHandlers = createNextAuthHandlers({
  issuerBaseUrl: process.env.APPDIRECT_ISSUER_BASE_URL!,
  clientId: process.env.APPDIRECT_CLIENT_ID!,
  clientSecret: process.env.APPDIRECT_CLIENT_SECRET!,
  appBaseUrl: process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000',
});

2. Add route handlers (app/api/auth/login/route.ts):

import { authHandlers } from '@/lib/auth';
export const GET = (req: Request) => authHandlers.login(req);

Repeat for callback, me, jwt, refresh, and logout — see examples/nextjs-app-router.

Requires next as a peer dependency. On Vercel, set NEXT_PUBLIC_APP_URL to your public deployment URL so post-login redirects use the correct origin.

Quick start — Node API route handlers

For file-based api/auth/* routes or any Node (req, res) serverless handler:

import { createAuthRouteHandlers } from '@appdirect/auth-bff/handlers';

export const handlers = createAuthRouteHandlers({
  issuerBaseUrl: process.env.APPDIRECT_ISSUER_BASE_URL,
  clientId: process.env.APPDIRECT_CLIENT_ID,
  clientSecret: process.env.APPDIRECT_CLIENT_SECRET,
  appBaseUrl: process.env.APP_BASE_URL ?? process.env.BASE_URL ?? 'http://localhost:3000',
  // Cross-site iframe embeds (preview, shared links):
  cookieOptions: { partitioned: true },
});

export default function handler(req, res) {
  return handlers.login(req, res);
}

See examples/handlers.

Custom API routes (e.g. marketplace proxies) can use readAuthCookies and applyCookieMutations from @appdirect/auth-bff/handlers — see Custom API routes in the Usage Reference.

The appdirect-auth-example repo is a full reference app (Next.js UI, Users API, subscription management).

Cross-site iframe embeds

When the app runs inside a third-party iframe (e.g. App Builder preview or a shared-link viewer), enable CHIPS cookies:

cookieOptions: { partitioned: true }

The SDK then forces Secure and defaults SameSite to None (required for third-party contexts). See cookieOptions in the Usage Reference.

Environment variables

| Variable | Description | |----------|-------------| | APPDIRECT_ISSUER_BASE_URL | Marketplace base URL (no trailing slash) | | APPDIRECT_CLIENT_ID | OAuth client ID | | APPDIRECT_CLIENT_SECRET | OAuth client secret | | APP_BASE_URL / BASE_URL | App origin for OAuth redirect URI | | NEXT_PUBLIC_APP_URL | Next.js public app URL (required on Vercel) | | AUTH_BFF_DEBUG | Set to true to enable opt-in [auth-bff:jwt] / [auth-bff:refresh] debug logs |

Register callback: {APP_URL}/api/auth/callback

API routes provided by adapters

| Route | Method | Purpose | |-------|--------|---------| | /api/auth/login | GET | Redirect to AppDirect authorize | | /api/auth/callback | GET | OAuth callback, set session cookies | | /api/auth/me | GET | Current user from FEJWT session | | /api/auth/jwt | GET | FEJWT for Global Header (auto-refresh) | | /api/auth/refresh | GET/POST | Manual token refresh | | /api/auth/logout | GET/POST | Clear session cookies | | /api/auth/import-session | POST | Import session tokens into HttpOnly cookies (iframe preview handoff) |

Documentation

Development

npm install
npm run build
npm test
npm run typecheck

Examples

License

MIT