azirid-react 0.11.0 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -8
- package/dist/index.cjs +34 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -3
- package/dist/index.d.ts +33 -3
- package/dist/index.js +33 -6
- package/dist/index.js.map +1 -1
- package/dist/next-handlers.cjs +148 -0
- package/dist/next-handlers.cjs.map +1 -0
- package/dist/next-handlers.js +138 -0
- package/dist/next-handlers.js.map +1 -0
- package/dist/next-proxy.cjs +45 -0
- package/dist/next-proxy.cjs.map +1 -0
- package/dist/next-proxy.d.cts +79 -0
- package/dist/next-proxy.d.ts +79 -0
- package/dist/next-proxy.js +40 -0
- package/dist/next-proxy.js.map +1 -0
- package/dist/next.cjs +1 -3
- package/dist/next.cjs.map +1 -1
- package/dist/next.d.cts +44 -25
- package/dist/next.d.ts +44 -25
- package/dist/next.js +1 -3
- package/dist/next.js.map +1 -1
- package/package.json +21 -1
- package/dist/server.d.cts +0 -102
- package/dist/server.d.ts +0 -102
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/next-proxy.ts"],"names":[],"mappings":";;;AAmDA,SAAS,yBAAyB,OAAA,EAA8B;AAC9D,EAAA,MAAM,UAAA,GAAa,SAAS,UAAA,IAAc,WAAA;AAC1C,EAAA,MAAM,QAAA,GAAW,SAAS,QAAA,IAAY,QAAA;AACtC,EAAA,MAAM,YAAA,GAAe,OAAA,EAAS,YAAA,IAAgB,CAAC,UAAU,SAAS,CAAA;AAClE,EAAA,MAAM,kBAAkB,OAAA,EAAS,eAAA;AAEjC,EAAA,OAAO,SAAS,QAAQ,OAAA,EAAsB;AAC5C,IAAA,MAAM,EAAE,QAAA,EAAS,GAAI,OAAA,CAAQ,OAAA;AAC7B,IAAA,MAAM,KAAA,GAAQ,OAAA,CAAQ,OAAA,CAAQ,GAAA,CAAI,UAAU,CAAA,EAAG,KAAA;AAE/C,IAAA,MAAM,QAAA,GAAW,aAAa,IAAA,EAAK;AAGnC,IAAA,IAAI,KAAA,EAAO;AACT,MAAA,QAAA,CAAS,OAAA,CAAQ,GAAA,CAAI,gBAAA,EAAkB,KAAK,CAAA;AAAA,IAC9C;AAGA,IAAA,IAAI,eAAA,EAAiB;AACnB,MAAA,MAAM,QAAA,GAAW,aAAa,IAAA,CAAK,CAAC,MAAM,QAAA,CAAS,UAAA,CAAW,CAAC,CAAC,CAAA;AAChE,MAAA,MAAM,WAAA,GAAc,gBAAgB,IAAA,CAAK,CAAC,MAAM,QAAA,CAAS,UAAA,CAAW,CAAC,CAAC,CAAA;AAEtE,MAAA,IAAI,WAAA,IAAe,CAAC,QAAA,IAAY,CAAC,KAAA,EAAO;AACtC,QAAA,MAAM,GAAA,GAAM,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAM;AAClC,QAAA,GAAA,CAAI,QAAA,GAAW,QAAA;AACf,QAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,UAAA,EAAY,QAAQ,CAAA;AACzC,QAAA,OAAO,YAAA,CAAa,SAAS,GAAG,CAAA;AAAA,MAClC;AAAA,IACF;AAEA,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AACF;AAyBO,SAAS,kBAAkB,OAAA,EAA8B;AAC9D,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAGO,IAAM,cAAc,iBAAA;AAOpB,SAAS,uBAAuB,OAAA,EAA8B;AACnE,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAKO,IAAM,mBAAmB,sBAAA","file":"next-proxy.js","sourcesContent":["/**\n * Next.js proxy and middleware utilities for Azirid Access.\n *\n * This entry point requires `next/server` and provides request interception\n * for route protection and token forwarding.\n *\n * @example Next.js 16+ (proxy.ts)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 16+ with custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next/proxy\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)\n * ```ts\n * // middleware.ts\n * export { aziridMiddleware as default } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @packageDocumentation\n */\n\nimport { NextResponse, type NextRequest } from 'next/server'\n\n// ─── Proxy / Middleware Options ──────────────────────────────\n\nexport interface AziridProxyOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Routes that require authentication (glob patterns via startsWith) */\n protectedRoutes?: string[]\n /** Route to redirect to when not authenticated (default: \"/login\") */\n loginUrl?: string\n /** Routes that are always public (default: [\"/login\", \"/signup\"]) */\n publicRoutes?: string[]\n}\n\n/** @deprecated Use `AziridProxyOptions` instead */\nexport type AziridMiddlewareOptions = AziridProxyOptions\n\n// ─── Shared proxy/middleware logic ───────────────────────────\n\nfunction createRequestInterceptor(options?: AziridProxyOptions) {\n const cookieName = options?.cookieName ?? '__session'\n const loginUrl = options?.loginUrl ?? '/login'\n const publicRoutes = options?.publicRoutes ?? ['/login', '/signup']\n const protectedRoutes = options?.protectedRoutes\n\n return function handler(request: NextRequest) {\n const { pathname } = request.nextUrl\n const token = request.cookies.get(cookieName)?.value\n\n const response = NextResponse.next()\n\n // Expose token as internal header for server components\n if (token) {\n response.headers.set('x-azirid-token', token)\n }\n\n // Route protection (only when protectedRoutes is configured)\n if (protectedRoutes) {\n const isPublic = publicRoutes.some((r) => pathname.startsWith(r))\n const isProtected = protectedRoutes.some((r) => pathname.startsWith(r))\n\n if (isProtected && !isPublic && !token) {\n const url = request.nextUrl.clone()\n url.pathname = loginUrl\n url.searchParams.set('redirect', pathname)\n return NextResponse.redirect(url)\n }\n }\n\n return response\n }\n}\n\n// ─── Proxy (Next.js 16+) ────────────────────────────────────\n\n/**\n * Create a customized Azirid proxy for Next.js 16+.\n *\n * In Next.js 16, `middleware.ts` is renamed to `proxy.ts` and the\n * exported function must be named `proxy`.\n *\n * @example Default (one line)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next/proxy\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n */\nexport function createAziridProxy(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/** Default proxy export for Next.js 16+ (no route protection, only forwards token) */\nexport const aziridProxy = createAziridProxy()\n\n// ─── Middleware (Next.js 14/15 — deprecated in 16) ──────────\n\n/**\n * @deprecated In Next.js 16+, use `createAziridProxy` and `proxy.ts` instead.\n */\nexport function createAziridMiddleware(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/**\n * @deprecated In Next.js 16+, use `aziridProxy` and `proxy.ts` instead.\n */\nexport const aziridMiddleware = createAziridMiddleware()\n"]}
|
package/dist/next.cjs
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var server = require('next/server');
|
|
4
4
|
|
|
5
|
-
// src/next.ts
|
|
6
|
-
|
|
7
5
|
// src/server.ts
|
|
8
6
|
function createSessionSyncHandler(options) {
|
|
9
7
|
const name = options?.cookieName ?? "__session";
|
|
@@ -40,7 +38,7 @@ function createSessionSyncHandler(options) {
|
|
|
40
38
|
};
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
// src/next.ts
|
|
41
|
+
// src/next-handlers.ts
|
|
44
42
|
var AZIRID_API_URL = process.env.AZIRID_API_URL ?? "https://api.azirid.com";
|
|
45
43
|
function createHandler(options) {
|
|
46
44
|
const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\/$/, "");
|
package/dist/next.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server.ts","../src/next.ts"],"names":["NextResponse"],"mappings":";;;;;;;AA2HO,SAAS,yBAAyB,OAAA,EAA8B;AACrE,EAAA,MAAM,IAAA,GAAO,SAAS,UAAA,IAAc,WAAA;AACpC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,KAAA;AAClC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,IAAA;AAElC,EAAA,OAAO;AAAA,IACL,MAAM,KAAK,GAAA,EAAc;AACvB,MAAA,MAAM,EAAE,KAAA,EAAM,GAAI,MAAM,IAAI,IAAA,EAAK;AACjC,MAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,QAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,KAAA,EAAO,eAAA,EAAiB,CAAA,EAAG;AAAA,UAC9D,MAAA,EAAQ,GAAA;AAAA,UACR,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA;AAAmB,SAC/C,CAAA;AAAA,MACH;AACA,MAAA,MAAM,KAAA,GAAQ,CAAC,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA,EAAI,QAAA,EAAU,UAAA,EAAY,cAAA,EAAgB,CAAA,QAAA,EAAW,MAAM,CAAA,CAAE,CAAA;AAC5F,MAAA,IAAI,MAAA,EAAQ,KAAA,CAAM,IAAA,CAAK,QAAQ,CAAA;AAC/B,MAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,EAAA,EAAI,IAAA,EAAM,CAAA,EAAG;AAAA,QAChD,OAAA,EAAS;AAAA,UACP,cAAA,EAAgB,kBAAA;AAAA,UAChB,YAAA,EAAc,KAAA,CAAM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;AACf,QAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,UAAA,EAAY,QAAQ,CAAA;AACzC,QAAA,OAAOA,mBAAA,CAAa,SAAS,GAAG,CAAA;AAAA,MAClC;AAAA,IACF;AAEA,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AACF;AAyBO,SAAS,kBAAkB,OAAA,EAA8B;AAC9D,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAGO,IAAM,cAAc,iBAAA;AAOpB,SAAS,uBAAuB,OAAA,EAA8B;AACnE,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAKO,IAAM,mBAAmB,sBAAA","file":"next.cjs","sourcesContent":["/**\n * Server-side utilities for Next.js (App Router).\n *\n * Use this entry point in Server Components, Server Actions, and Route Handlers\n * to read the authenticated user's session token from the httpOnly `__session` cookie.\n *\n * @example\n * ```ts\n * // lib/access-server.ts\n * import { cookies } from \"next/headers\";\n * import { createServerAccess } from \"azirid-react/server\";\n * export const { getSessionToken } = createServerAccess({ cookies });\n * ```\n *\n * ```ts\n * // app/actions/profile.ts\n * \"use server\";\n * import { getSessionToken } from \"@/lib/access-server\";\n *\n * export async function getProfile() {\n * const token = await getSessionToken();\n * if (!token) throw new Error(\"Not authenticated\");\n *\n * const res = await fetch(\"https://api.myapp.com/v1/users/auth/me\", {\n * headers: { Authorization: `Bearer ${token}` },\n * });\n * return res.json();\n * }\n * ```\n *\n * @packageDocumentation\n */\n\n/** Minimal cookie store interface (subset of Next.js ReadonlyRequestCookies) */\ninterface CookieStore {\n get(name: string): { value: string } | undefined\n}\n\n/** The `cookies` function from `next/headers` */\ntype CookiesFn = () => CookieStore | Promise<CookieStore>\n\nexport interface ServerAccessOptions {\n /**\n * The `cookies` function imported from `next/headers`.\n * Required — pass it explicitly to avoid bundler issues with dynamic imports.\n *\n * @example\n * ```ts\n * import { cookies } from \"next/headers\";\n * createServerAccess({ cookies });\n * ```\n */\n cookies: CookiesFn\n\n /**\n * Name of the httpOnly cookie that carries the access token.\n * Must match the cookie set by the Azirid API.\n * @default \"__session\"\n */\n cookieName?: string\n}\n\nexport interface ServerAccess {\n /**\n * Read the raw JWT access token from the session cookie.\n * Returns `null` when no cookie is present or when called\n * outside a Next.js server context.\n */\n getSessionToken: () => Promise<string | null>\n\n /** Alias for {@link getSessionToken}. */\n getAccessToken: () => Promise<string | null>\n}\n\n/**\n * Create server-side helpers bound to the session cookie.\n *\n * Call once at module scope, then use the returned functions\n * in any Server Component, Server Action, or Route Handler.\n */\nexport function createServerAccess(options: ServerAccessOptions): ServerAccess {\n const { cookies, cookieName = '__session' } = options\n\n async function getSessionToken(): Promise<string | null> {\n try {\n const result = cookies()\n // Next.js 15+: cookies() returns a Promise\n const cookieStore = result instanceof Promise ? await result : result\n return cookieStore.get(cookieName)?.value ?? null\n } catch {\n return null\n }\n }\n\n return {\n getSessionToken,\n getAccessToken: getSessionToken,\n }\n}\n\n// ─── Session Sync Handler (cross-origin dev support) ─────────\n\nexport interface SessionSyncOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Set Secure flag on cookie (default: false) */\n secure?: boolean\n /** Cookie max age in seconds (default: 3600 = 1h) */\n maxAge?: number\n}\n\n/**\n * Create a route handler that syncs the access token to a local httpOnly cookie.\n *\n * Use this in a Next.js App Router route to bridge cross-origin sessions:\n *\n * @example\n * ```ts\n * // app/api/auth/session/route.ts\n * import { createSessionSyncHandler } from \"azirid-react/server\";\n * export const { POST, DELETE } = createSessionSyncHandler();\n * ```\n */\nexport function createSessionSyncHandler(options?: SessionSyncOptions) {\n const name = options?.cookieName ?? '__session'\n const secure = options?.secure ?? false\n const maxAge = options?.maxAge ?? 3600\n\n return {\n async POST(req: Request) {\n const { token } = await req.json()\n if (!token || typeof token !== 'string') {\n return new Response(JSON.stringify({ error: 'Missing token' }), {\n status: 400,\n headers: { 'Content-Type': 'application/json' },\n })\n }\n const parts = [`${name}=${token}`, 'Path=/', 'HttpOnly', 'SameSite=Lax', `Max-Age=${maxAge}`]\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n async DELETE() {\n const parts = [`${name}=`, 'Path=/', 'HttpOnly', 'SameSite=Lax', 'Max-Age=0']\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n }\n}\n","/**\n * Next.js utilities for Azirid Access — route handlers, proxy, and middleware.\n *\n * This entry point runs in Node.js (NO \"use client\").\n * Supports Next.js 14, 15, and 16+.\n *\n * @example Route handler (simplest — one line)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next\";\n * ```\n *\n * @example Route handler (custom API URL)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n *\n * @example Next.js 16+ (proxy.ts)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)\n * ```ts\n * // middleware.ts\n * export { aziridMiddleware as default } from \"azirid-react/next\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @packageDocumentation\n */\n\nimport { NextResponse, type NextRequest } from 'next/server'\n\n// ─── Constants ───────────────────────────────────────────────\n\n/**\n * Resolve the Azirid API URL.\n *\n * Priority:\n * 1. `AZIRID_API_URL` env var (server-only, recommended)\n * 2. `https://api.azirid.com` (production default)\n *\n * For local development, set `AZIRID_API_URL=http://localhost:3000` in your `.env`.\n */\nconst AZIRID_API_URL = process.env.AZIRID_API_URL ?? 'https://api.azirid.com'\n\n// ─── Types ───────────────────────────────────────────────────\n\ntype RouteHandler = (\n request: Request,\n context: { params: Promise<{ path: string[] }> | { path: string[] } },\n) => Promise<Response>\n\nexport interface AziridRouteHandlerOptions {\n /** Override the Azirid API URL (default: production API) */\n apiUrl?: string\n /** Enable debug logging of proxy requests (default: false) */\n debug?: boolean\n}\n\n// ─── Proxy Route Handler (core logic) ────────────────────────\n\nfunction createHandler(options?: AziridRouteHandlerOptions): RouteHandler {\n const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\\/$/, '')\n const debug = options?.debug ?? false\n\n return async function handler(request, context) {\n // Handle Next.js 14 (sync params), 15 (async params), and 16+ (always async)\n const resolvedParams = context.params instanceof Promise ? await context.params : context.params\n const path = resolvedParams.path.join('/')\n const targetUrl = `${destination}/v1/users/auth/${path}`\n\n if (debug) {\n console.log(`[azirid-proxy] ${request.method} /api/auth/${path} → ${targetUrl}`)\n console.log(`[azirid-proxy] Cookies:`, request.headers.get('cookie') ?? '(none)')\n }\n\n // Forward request headers (strip host so the API sees its own host)\n const forwardHeaders = new Headers()\n request.headers.forEach((value, key) => {\n const lower = key.toLowerCase()\n if (lower !== 'host' && lower !== 'connection') {\n forwardHeaders.set(key, value)\n }\n })\n\n // Forward body for non-GET methods\n const body =\n request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined\n\n // Proxy request to the Azirid API\n const apiResponse = await fetch(targetUrl, {\n method: request.method,\n headers: forwardHeaders,\n body,\n })\n\n // Build response headers, fixing Set-Cookie for first-party context\n const responseHeaders = new Headers()\n apiResponse.headers.forEach((value, key) => {\n if (key.toLowerCase() !== 'set-cookie') {\n responseHeaders.set(key, value)\n }\n })\n\n // Fix Set-Cookie headers: strip Domain, adjust SameSite/Secure for proxy\n const isSecure =\n request.headers.get('x-forwarded-proto') === 'https' ||\n new URL(request.url).protocol === 'https:'\n\n const setCookies: string[] =\n typeof apiResponse.headers.getSetCookie === 'function'\n ? apiResponse.headers.getSetCookie()\n : splitSetCookieHeader(apiResponse.headers.get('set-cookie') ?? '')\n\n for (const cookie of setCookies) {\n let fixed = cookie\n // Strip Domain — cookie will default to the proxy's domain (first-party)\n .replace(/;\\s*[Dd]omain=[^;]*/g, '')\n // Normalize Path to \"/\" — the API may set restrictive paths (e.g. /v1/users/auth/login)\n // which prevents cookies from being sent to other auth endpoints like /refresh or /bootstrap.\n // In proxy mode all auth cookies must be available on all routes.\n .replace(/;\\s*[Pp]ath=[^;]*/g, '')\n // Change SameSite=None to Lax (now same-origin, None is unnecessary)\n .replace(/[Ss]ame[Ss]ite=[Nn]one/g, 'SameSite=Lax')\n\n // Always set Path=/ for first-party proxy cookies\n fixed += '; Path=/'\n\n // In development (HTTP), remove Secure flag so cookies work on localhost\n if (!isSecure) {\n fixed = fixed.replace(/;\\s*[Ss]ecure/g, '')\n }\n\n responseHeaders.append('set-cookie', fixed)\n }\n\n if (debug) {\n console.log(`[azirid-proxy] API responded: ${apiResponse.status}`)\n const responseCookies: string[] = []\n responseHeaders.forEach((value, key) => {\n if (key.toLowerCase() === 'set-cookie') responseCookies.push(value)\n })\n if (responseCookies.length > 0) {\n console.log(`[azirid-proxy] Set-Cookie:`, responseCookies)\n }\n }\n\n return new Response(apiResponse.body, {\n status: apiResponse.status,\n statusText: apiResponse.statusText,\n headers: responseHeaders,\n })\n }\n}\n\n/** Split a raw Set-Cookie header string into individual cookies */\nfunction splitSetCookieHeader(header: string): string[] {\n if (!header) return []\n const cookies: string[] = []\n let current = ''\n for (const part of header.split(', ')) {\n if (current && /^[a-zA-Z0-9_\\-.]+=/.test(part)) {\n cookies.push(current)\n current = part\n } else {\n current = current ? `${current}, ${part}` : part\n }\n }\n if (current) cookies.push(current)\n return cookies\n}\n\n// ─── Route Handlers (ready to re-export) ─────────────────────\n\n/**\n * Create all route handlers for the Azirid auth proxy.\n *\n * Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported\n * from your `app/v1/auth/[...path]/route.ts`.\n *\n * @example Default (zero config)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();\n * ```\n *\n * @example Custom options\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n */\nexport function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {\n GET: RouteHandler\n POST: RouteHandler\n PUT: RouteHandler\n PATCH: RouteHandler\n DELETE: RouteHandler\n} {\n const handler = createHandler(options)\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n }\n}\n\n/**\n * Pre-built route handlers with default config.\n *\n * Just re-export these directly — no setup required:\n *\n * @example\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next\";\n * ```\n */\nconst defaultHandlers = createAziridRouteHandlers()\nexport const GET = defaultHandlers.GET\nexport const POST = defaultHandlers.POST\nexport const PUT = defaultHandlers.PUT\nexport const PATCH = defaultHandlers.PATCH\nexport const DELETE = defaultHandlers.DELETE\n\n/**\n * Create a single Next.js route handler function.\n *\n * Use this if you need a single handler reference (advanced use).\n * For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.\n *\n * @deprecated Use `createAziridRouteHandlers()` instead.\n */\nexport function createAziridProxyHandler(apiUrl: string = AZIRID_API_URL): RouteHandler {\n return createHandler({ apiUrl })\n}\n\n// ─── Config Helper ───────────────────────────────────────────\n\n/**\n * Wrap your Next.js config with Azirid settings.\n *\n * In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.\n * This helper is kept for backward compatibility with Next.js 14/15.\n *\n * @param apiUrl - Override the Azirid API URL (default: production API)\n */\nexport function withAziridProxy(apiUrl: string = AZIRID_API_URL) {\n void apiUrl\n\n return (nextConfig: Record<string, unknown> = {}) => ({\n ...nextConfig,\n })\n}\n\n// ─── Proxy / Middleware Options ──────────────────────────────\n\nexport interface AziridProxyOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Routes that require authentication (glob patterns via startsWith) */\n protectedRoutes?: string[]\n /** Route to redirect to when not authenticated (default: \"/login\") */\n loginUrl?: string\n /** Routes that are always public (default: [\"/login\", \"/signup\"]) */\n publicRoutes?: string[]\n}\n\n/** @deprecated Use `AziridProxyOptions` instead */\nexport type AziridMiddlewareOptions = AziridProxyOptions\n\n// ─── Shared proxy/middleware logic ───────────────────────────\n\nfunction createRequestInterceptor(options?: AziridProxyOptions) {\n const cookieName = options?.cookieName ?? '__session'\n const loginUrl = options?.loginUrl ?? '/login'\n const publicRoutes = options?.publicRoutes ?? ['/login', '/signup']\n const protectedRoutes = options?.protectedRoutes\n\n return function handler(request: NextRequest) {\n const { pathname } = request.nextUrl\n const token = request.cookies.get(cookieName)?.value\n\n const response = NextResponse.next()\n\n // Expose token as internal header for server components\n if (token) {\n response.headers.set('x-azirid-token', token)\n }\n\n // Route protection (only when protectedRoutes is configured)\n if (protectedRoutes) {\n const isPublic = publicRoutes.some((r) => pathname.startsWith(r))\n const isProtected = protectedRoutes.some((r) => pathname.startsWith(r))\n\n if (isProtected && !isPublic && !token) {\n const url = request.nextUrl.clone()\n url.pathname = loginUrl\n url.searchParams.set('redirect', pathname)\n return NextResponse.redirect(url)\n }\n }\n\n return response\n }\n}\n\n// ─── Proxy (Next.js 16+) ────────────────────────────────────\n\n/**\n * Create a customized Azirid proxy for Next.js 16+.\n *\n * In Next.js 16, `middleware.ts` is renamed to `proxy.ts` and the\n * exported function must be named `proxy`.\n *\n * @example Default (one line)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n */\nexport function createAziridProxy(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/** Default proxy export for Next.js 16+ (no route protection, only forwards token) */\nexport const aziridProxy = createAziridProxy()\n\n// ─── Middleware (Next.js 14/15 — deprecated in 16) ──────────\n\n/**\n * @deprecated In Next.js 16+, use `createAziridProxy` and `proxy.ts` instead.\n */\nexport function createAziridMiddleware(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/**\n * @deprecated In Next.js 16+, use `aziridProxy` and `proxy.ts` instead.\n */\nexport const aziridMiddleware = createAziridMiddleware()\n\n// ─── Re-export session sync for backward compat ──────────────\n\nexport { createSessionSyncHandler } from './server'\nexport type { SessionSyncOptions } from './server'\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/server.ts","../src/next-handlers.ts","../src/next-proxy.ts"],"names":["NextResponse"],"mappings":";;;;;AA2HO,SAAS,yBAAyB,OAAA,EAA8B;AACrE,EAAA,MAAM,IAAA,GAAO,SAAS,UAAA,IAAc,WAAA;AACpC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,KAAA;AAClC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,IAAA;AAElC,EAAA,OAAO;AAAA,IACL,MAAM,KAAK,GAAA,EAAc;AACvB,MAAA,MAAM,EAAE,KAAA,EAAM,GAAI,MAAM,IAAI,IAAA,EAAK;AACjC,MAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,QAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,KAAA,EAAO,eAAA,EAAiB,CAAA,EAAG;AAAA,UAC9D,MAAA,EAAQ,GAAA;AAAA,UACR,OAAA,EAAS,EAAE,cAAA,EA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A,CAAQ,GAAA,CAAI,gBAAA,EAAkB,KAAK,CAAA;AAAA,IAC9C;AAGA,IAAA,IAAI,eAAA,EAAiB;AACnB,MAAA,MAAM,QAAA,GAAW,aAAa,IAAA,CAAK,CAAC,MAAM,QAAA,CAAS,UAAA,CAAW,CAAC,CAAC,CAAA;AAChE,MAAA,MAAM,WAAA,GAAc,gBAAgB,IAAA,CAAK,CAAC,MAAM,QAAA,CAAS,UAAA,CAAW,CAAC,CAAC,CAAA;AAEtE,MAAA,IAAI,WAAA,IAAe,CAAC,QAAA,IAAY,CAAC,KAAA,EAAO;AACtC,QAAA,MAAM,GAAA,GAAM,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAM;AAClC,QAAA,GAAA,CAAI,QAAA,GAAW,QAAA;AACf,QAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,UAAA,EAAY,QAAQ,CAAA;AACzC,QAAA,OAAOA,mBAAA,CAAa,SAAS,GAAG,CAAA;AAAA,MAClC;AAAA,IACF;AAEA,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AACF;AAyBO,SAAS,kBAAkB,OAAA,EAA8B;AAC9D,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAGO,IAAM,cAAc,iBAAA;AAOpB,SAAS,uBAAuB,OAAA,EAA8B;AACnE,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAKO,IAAM,mBAAmB,sBAAA","file":"next.cjs","sourcesContent":["/**\n * Server-side utilities for Next.js (App Router).\n *\n * Use this entry point in Server Components, Server Actions, and Route Handlers\n * to read the authenticated user's session token from the httpOnly `__session` cookie.\n *\n * @example\n * ```ts\n * // lib/access-server.ts\n * import { cookies } from \"next/headers\";\n * import { createServerAccess } from \"azirid-react/server\";\n * export const { getSessionToken } = createServerAccess({ cookies });\n * ```\n *\n * ```ts\n * // app/actions/profile.ts\n * \"use server\";\n * import { getSessionToken } from \"@/lib/access-server\";\n *\n * export async function getProfile() {\n * const token = await getSessionToken();\n * if (!token) throw new Error(\"Not authenticated\");\n *\n * const res = await fetch(\"https://api.myapp.com/v1/users/auth/me\", {\n * headers: { Authorization: `Bearer ${token}` },\n * });\n * return res.json();\n * }\n * ```\n *\n * @packageDocumentation\n */\n\n/** Minimal cookie store interface (subset of Next.js ReadonlyRequestCookies) */\ninterface CookieStore {\n get(name: string): { value: string } | undefined\n}\n\n/** The `cookies` function from `next/headers` */\ntype CookiesFn = () => CookieStore | Promise<CookieStore>\n\nexport interface ServerAccessOptions {\n /**\n * The `cookies` function imported from `next/headers`.\n * Required — pass it explicitly to avoid bundler issues with dynamic imports.\n *\n * @example\n * ```ts\n * import { cookies } from \"next/headers\";\n * createServerAccess({ cookies });\n * ```\n */\n cookies: CookiesFn\n\n /**\n * Name of the httpOnly cookie that carries the access token.\n * Must match the cookie set by the Azirid API.\n * @default \"__session\"\n */\n cookieName?: string\n}\n\nexport interface ServerAccess {\n /**\n * Read the raw JWT access token from the session cookie.\n * Returns `null` when no cookie is present or when called\n * outside a Next.js server context.\n */\n getSessionToken: () => Promise<string | null>\n\n /** Alias for {@link getSessionToken}. */\n getAccessToken: () => Promise<string | null>\n}\n\n/**\n * Create server-side helpers bound to the session cookie.\n *\n * Call once at module scope, then use the returned functions\n * in any Server Component, Server Action, or Route Handler.\n */\nexport function createServerAccess(options: ServerAccessOptions): ServerAccess {\n const { cookies, cookieName = '__session' } = options\n\n async function getSessionToken(): Promise<string | null> {\n try {\n const result = cookies()\n // Next.js 15+: cookies() returns a Promise\n const cookieStore = result instanceof Promise ? await result : result\n return cookieStore.get(cookieName)?.value ?? null\n } catch {\n return null\n }\n }\n\n return {\n getSessionToken,\n getAccessToken: getSessionToken,\n }\n}\n\n// ─── Session Sync Handler (cross-origin dev support) ─────────\n\nexport interface SessionSyncOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Set Secure flag on cookie (default: false) */\n secure?: boolean\n /** Cookie max age in seconds (default: 3600 = 1h) */\n maxAge?: number\n}\n\n/**\n * Create a route handler that syncs the access token to a local httpOnly cookie.\n *\n * Use this in a Next.js App Router route to bridge cross-origin sessions:\n *\n * @example\n * ```ts\n * // app/api/auth/session/route.ts\n * import { createSessionSyncHandler } from \"azirid-react/server\";\n * export const { POST, DELETE } = createSessionSyncHandler();\n * ```\n */\nexport function createSessionSyncHandler(options?: SessionSyncOptions) {\n const name = options?.cookieName ?? '__session'\n const secure = options?.secure ?? false\n const maxAge = options?.maxAge ?? 3600\n\n return {\n async POST(req: Request) {\n const { token } = await req.json()\n if (!token || typeof token !== 'string') {\n return new Response(JSON.stringify({ error: 'Missing token' }), {\n status: 400,\n headers: { 'Content-Type': 'application/json' },\n })\n }\n const parts = [`${name}=${token}`, 'Path=/', 'HttpOnly', 'SameSite=Lax', `Max-Age=${maxAge}`]\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n async DELETE() {\n const parts = [`${name}=`, 'Path=/', 'HttpOnly', 'SameSite=Lax', 'Max-Age=0']\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n }\n}\n","/**\n * Next.js route handlers for the Azirid auth proxy.\n *\n * This entry point uses **only standard Web APIs** (Request, Response, fetch)\n * and has **zero dependency on `next/server`**. This makes it safe to use in\n * Next.js standalone builds (`output: 'standalone'`) where external `next/server`\n * imports may fail at runtime.\n *\n * @example Route handler (simplest — one line)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n *\n * @example Route handler (custom API URL)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n *\n * @packageDocumentation\n */\n\n// ─── Constants ───────────────────────────────────────────────\n\n/**\n * Resolve the Azirid API URL.\n *\n * Priority:\n * 1. `AZIRID_API_URL` env var (server-only, recommended)\n * 2. `https://api.azirid.com` (production default)\n *\n * For local development, set `AZIRID_API_URL=http://localhost:3000` in your `.env`.\n */\nconst AZIRID_API_URL = process.env.AZIRID_API_URL ?? 'https://api.azirid.com'\n\n// ─── Types ───────────────────────────────────────────────────\n\ntype RouteHandler = (\n request: Request,\n context: { params: Promise<{ path: string[] }> | { path: string[] } },\n) => Promise<Response>\n\nexport interface AziridRouteHandlerOptions {\n /** Override the Azirid API URL (default: production API) */\n apiUrl?: string\n /** Enable debug logging of proxy requests (default: false) */\n debug?: boolean\n}\n\n// ─── Proxy Route Handler (core logic) ────────────────────────\n\nfunction createHandler(options?: AziridRouteHandlerOptions): RouteHandler {\n const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\\/$/, '')\n const debug = options?.debug ?? false\n\n return async function handler(request, context) {\n // Handle Next.js 14 (sync params), 15 (async params), and 16+ (always async)\n const resolvedParams = context.params instanceof Promise ? await context.params : context.params\n const path = resolvedParams.path.join('/')\n const targetUrl = `${destination}/v1/users/auth/${path}`\n\n if (debug) {\n console.log(`[azirid-proxy] ${request.method} /api/auth/${path} → ${targetUrl}`)\n console.log(`[azirid-proxy] Cookies:`, request.headers.get('cookie') ?? '(none)')\n }\n\n // Forward request headers (strip host so the API sees its own host)\n const forwardHeaders = new Headers()\n request.headers.forEach((value, key) => {\n const lower = key.toLowerCase()\n if (lower !== 'host' && lower !== 'connection') {\n forwardHeaders.set(key, value)\n }\n })\n\n // Forward body for non-GET methods\n const body =\n request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined\n\n // Proxy request to the Azirid API\n const apiResponse = await fetch(targetUrl, {\n method: request.method,\n headers: forwardHeaders,\n body,\n })\n\n // Build response headers, fixing Set-Cookie for first-party context\n const responseHeaders = new Headers()\n apiResponse.headers.forEach((value, key) => {\n if (key.toLowerCase() !== 'set-cookie') {\n responseHeaders.set(key, value)\n }\n })\n\n // Fix Set-Cookie headers: strip Domain, adjust SameSite/Secure for proxy\n const isSecure =\n request.headers.get('x-forwarded-proto') === 'https' ||\n new URL(request.url).protocol === 'https:'\n\n const setCookies: string[] =\n typeof apiResponse.headers.getSetCookie === 'function'\n ? apiResponse.headers.getSetCookie()\n : splitSetCookieHeader(apiResponse.headers.get('set-cookie') ?? '')\n\n for (const cookie of setCookies) {\n let fixed = cookie\n // Strip Domain — cookie will default to the proxy's domain (first-party)\n .replace(/;\\s*[Dd]omain=[^;]*/g, '')\n // Normalize Path to \"/\" — the API may set restrictive paths (e.g. /v1/users/auth/login)\n // which prevents cookies from being sent to other auth endpoints like /refresh or /bootstrap.\n // In proxy mode all auth cookies must be available on all routes.\n .replace(/;\\s*[Pp]ath=[^;]*/g, '')\n // Change SameSite=None to Lax (now same-origin, None is unnecessary)\n .replace(/[Ss]ame[Ss]ite=[Nn]one/g, 'SameSite=Lax')\n\n // Always set Path=/ for first-party proxy cookies\n fixed += '; Path=/'\n\n // In development (HTTP), remove Secure flag so cookies work on localhost\n if (!isSecure) {\n fixed = fixed.replace(/;\\s*[Ss]ecure/g, '')\n }\n\n responseHeaders.append('set-cookie', fixed)\n }\n\n if (debug) {\n console.log(`[azirid-proxy] API responded: ${apiResponse.status}`)\n const responseCookies: string[] = []\n responseHeaders.forEach((value, key) => {\n if (key.toLowerCase() === 'set-cookie') responseCookies.push(value)\n })\n if (responseCookies.length > 0) {\n console.log(`[azirid-proxy] Set-Cookie:`, responseCookies)\n }\n }\n\n return new Response(apiResponse.body, {\n status: apiResponse.status,\n statusText: apiResponse.statusText,\n headers: responseHeaders,\n })\n }\n}\n\n/** Split a raw Set-Cookie header string into individual cookies */\nfunction splitSetCookieHeader(header: string): string[] {\n if (!header) return []\n const cookies: string[] = []\n let current = ''\n for (const part of header.split(', ')) {\n if (current && /^[a-zA-Z0-9_\\-.]+=/.test(part)) {\n cookies.push(current)\n current = part\n } else {\n current = current ? `${current}, ${part}` : part\n }\n }\n if (current) cookies.push(current)\n return cookies\n}\n\n// ─── Route Handlers (ready to re-export) ─────────────────────\n\n/**\n * Create all route handlers for the Azirid auth proxy.\n *\n * Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported\n * from your `app/api/auth/[...path]/route.ts`.\n *\n * @example Default (zero config)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();\n * ```\n *\n * @example Custom options\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n */\nexport function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {\n GET: RouteHandler\n POST: RouteHandler\n PUT: RouteHandler\n PATCH: RouteHandler\n DELETE: RouteHandler\n} {\n const handler = createHandler(options)\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n }\n}\n\n/**\n * Pre-built route handlers with default config.\n *\n * Just re-export these directly — no setup required:\n *\n * @example\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n */\nconst defaultHandlers = createAziridRouteHandlers()\nexport const GET = defaultHandlers.GET\nexport const POST = defaultHandlers.POST\nexport const PUT = defaultHandlers.PUT\nexport const PATCH = defaultHandlers.PATCH\nexport const DELETE = defaultHandlers.DELETE\n\n/**\n * Create a single Next.js route handler function.\n *\n * Use this if you need a single handler reference (advanced use).\n * For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.\n *\n * @deprecated Use `createAziridRouteHandlers()` instead.\n */\nexport function createAziridProxyHandler(apiUrl: string = AZIRID_API_URL): RouteHandler {\n return createHandler({ apiUrl })\n}\n\n// ─── Config Helper ───────────────────────────────────────────\n\n/**\n * Wrap your Next.js config with Azirid settings.\n *\n * In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.\n * This helper is kept for backward compatibility with Next.js 14/15.\n *\n * @param apiUrl - Override the Azirid API URL (default: production API)\n */\nexport function withAziridProxy(apiUrl: string = AZIRID_API_URL) {\n void apiUrl\n\n return (nextConfig: Record<string, unknown> = {}) => ({\n ...nextConfig,\n })\n}\n\n// ─── Re-export session sync for backward compat ──────────────\n\nexport { createSessionSyncHandler } from './server'\nexport type { SessionSyncOptions } from './server'\n","/**\n * Next.js proxy and middleware utilities for Azirid Access.\n *\n * This entry point requires `next/server` and provides request interception\n * for route protection and token forwarding.\n *\n * @example Next.js 16+ (proxy.ts)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 16+ with custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next/proxy\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)\n * ```ts\n * // middleware.ts\n * export { aziridMiddleware as default } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @packageDocumentation\n */\n\nimport { NextResponse, type NextRequest } from 'next/server'\n\n// ─── Proxy / Middleware Options ──────────────────────────────\n\nexport interface AziridProxyOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Routes that require authentication (glob patterns via startsWith) */\n protectedRoutes?: string[]\n /** Route to redirect to when not authenticated (default: \"/login\") */\n loginUrl?: string\n /** Routes that are always public (default: [\"/login\", \"/signup\"]) */\n publicRoutes?: string[]\n}\n\n/** @deprecated Use `AziridProxyOptions` instead */\nexport type AziridMiddlewareOptions = AziridProxyOptions\n\n// ─── Shared proxy/middleware logic ───────────────────────────\n\nfunction createRequestInterceptor(options?: AziridProxyOptions) {\n const cookieName = options?.cookieName ?? '__session'\n const loginUrl = options?.loginUrl ?? '/login'\n const publicRoutes = options?.publicRoutes ?? ['/login', '/signup']\n const protectedRoutes = options?.protectedRoutes\n\n return function handler(request: NextRequest) {\n const { pathname } = request.nextUrl\n const token = request.cookies.get(cookieName)?.value\n\n const response = NextResponse.next()\n\n // Expose token as internal header for server components\n if (token) {\n response.headers.set('x-azirid-token', token)\n }\n\n // Route protection (only when protectedRoutes is configured)\n if (protectedRoutes) {\n const isPublic = publicRoutes.some((r) => pathname.startsWith(r))\n const isProtected = protectedRoutes.some((r) => pathname.startsWith(r))\n\n if (isProtected && !isPublic && !token) {\n const url = request.nextUrl.clone()\n url.pathname = loginUrl\n url.searchParams.set('redirect', pathname)\n return NextResponse.redirect(url)\n }\n }\n\n return response\n }\n}\n\n// ─── Proxy (Next.js 16+) ────────────────────────────────────\n\n/**\n * Create a customized Azirid proxy for Next.js 16+.\n *\n * In Next.js 16, `middleware.ts` is renamed to `proxy.ts` and the\n * exported function must be named `proxy`.\n *\n * @example Default (one line)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next/proxy\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n */\nexport function createAziridProxy(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/** Default proxy export for Next.js 16+ (no route protection, only forwards token) */\nexport const aziridProxy = createAziridProxy()\n\n// ─── Middleware (Next.js 14/15 — deprecated in 16) ──────────\n\n/**\n * @deprecated In Next.js 16+, use `createAziridProxy` and `proxy.ts` instead.\n */\nexport function createAziridMiddleware(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/**\n * @deprecated In Next.js 16+, use `aziridProxy` and `proxy.ts` instead.\n */\nexport const aziridMiddleware = createAziridMiddleware()\n"]}
|
package/dist/next.d.cts
CHANGED
|
@@ -26,44 +26,31 @@ declare function createSessionSyncHandler(options?: SessionSyncOptions): {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* Next.js
|
|
29
|
+
* Next.js route handlers for the Azirid auth proxy.
|
|
30
30
|
*
|
|
31
|
-
* This entry point
|
|
32
|
-
*
|
|
31
|
+
* This entry point uses **only standard Web APIs** (Request, Response, fetch)
|
|
32
|
+
* and has **zero dependency on `next/server`**. This makes it safe to use in
|
|
33
|
+
* Next.js standalone builds (`output: 'standalone'`) where external `next/server`
|
|
34
|
+
* imports may fail at runtime.
|
|
33
35
|
*
|
|
34
36
|
* @example Route handler (simplest — one line)
|
|
35
37
|
* ```ts
|
|
36
38
|
* // app/api/auth/[...path]/route.ts
|
|
37
|
-
* export { GET, POST, PUT, PATCH, DELETE } from "azirid-react/next";
|
|
39
|
+
* export { GET, POST, PUT, PATCH, DELETE } from "azirid-react/next/handlers";
|
|
38
40
|
* ```
|
|
39
41
|
*
|
|
40
42
|
* @example Route handler (custom API URL)
|
|
41
43
|
* ```ts
|
|
42
44
|
* // app/api/auth/[...path]/route.ts
|
|
43
|
-
* import { createAziridRouteHandlers } from "azirid-react/next";
|
|
45
|
+
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
44
46
|
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
45
47
|
* apiUrl: "https://my-custom-api.com",
|
|
46
48
|
* debug: true,
|
|
47
49
|
* });
|
|
48
50
|
* ```
|
|
49
51
|
*
|
|
50
|
-
* @example Next.js 16+ (proxy.ts)
|
|
51
|
-
* ```ts
|
|
52
|
-
* // proxy.ts
|
|
53
|
-
* export { aziridProxy as proxy } from "azirid-react/next";
|
|
54
|
-
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)
|
|
58
|
-
* ```ts
|
|
59
|
-
* // middleware.ts
|
|
60
|
-
* export { aziridMiddleware as default } from "azirid-react/next";
|
|
61
|
-
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
52
|
* @packageDocumentation
|
|
65
53
|
*/
|
|
66
|
-
|
|
67
54
|
type RouteHandler = (request: Request, context: {
|
|
68
55
|
params: Promise<{
|
|
69
56
|
path: string[];
|
|
@@ -81,19 +68,19 @@ interface AziridRouteHandlerOptions {
|
|
|
81
68
|
* Create all route handlers for the Azirid auth proxy.
|
|
82
69
|
*
|
|
83
70
|
* Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported
|
|
84
|
-
* from your `app/
|
|
71
|
+
* from your `app/api/auth/[...path]/route.ts`.
|
|
85
72
|
*
|
|
86
73
|
* @example Default (zero config)
|
|
87
74
|
* ```ts
|
|
88
75
|
* // app/api/auth/[...path]/route.ts
|
|
89
|
-
* import { createAziridRouteHandlers } from "azirid-react/next";
|
|
76
|
+
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
90
77
|
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();
|
|
91
78
|
* ```
|
|
92
79
|
*
|
|
93
80
|
* @example Custom options
|
|
94
81
|
* ```ts
|
|
95
82
|
* // app/api/auth/[...path]/route.ts
|
|
96
|
-
* import { createAziridRouteHandlers } from "azirid-react/next";
|
|
83
|
+
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
97
84
|
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
98
85
|
* apiUrl: "https://my-custom-api.com",
|
|
99
86
|
* debug: true,
|
|
@@ -132,6 +119,38 @@ declare function createAziridProxyHandler(apiUrl?: string): RouteHandler;
|
|
|
132
119
|
declare function withAziridProxy(apiUrl?: string): (nextConfig?: Record<string, unknown>) => {
|
|
133
120
|
[x: string]: unknown;
|
|
134
121
|
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Next.js proxy and middleware utilities for Azirid Access.
|
|
125
|
+
*
|
|
126
|
+
* This entry point requires `next/server` and provides request interception
|
|
127
|
+
* for route protection and token forwarding.
|
|
128
|
+
*
|
|
129
|
+
* @example Next.js 16+ (proxy.ts)
|
|
130
|
+
* ```ts
|
|
131
|
+
* // proxy.ts
|
|
132
|
+
* export { aziridProxy as proxy } from "azirid-react/next/proxy";
|
|
133
|
+
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @example Next.js 16+ with custom options
|
|
137
|
+
* ```ts
|
|
138
|
+
* // proxy.ts
|
|
139
|
+
* import { createAziridProxy } from "azirid-react/next/proxy";
|
|
140
|
+
* export const proxy = createAziridProxy({ protectedRoutes: ["/dashboard"] });
|
|
141
|
+
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)
|
|
145
|
+
* ```ts
|
|
146
|
+
* // middleware.ts
|
|
147
|
+
* export { aziridMiddleware as default } from "azirid-react/next/proxy";
|
|
148
|
+
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @packageDocumentation
|
|
152
|
+
*/
|
|
153
|
+
|
|
135
154
|
interface AziridProxyOptions {
|
|
136
155
|
/** Cookie name (default: "__session") */
|
|
137
156
|
cookieName?: string;
|
|
@@ -153,14 +172,14 @@ type AziridMiddlewareOptions = AziridProxyOptions;
|
|
|
153
172
|
* @example Default (one line)
|
|
154
173
|
* ```ts
|
|
155
174
|
* // proxy.ts
|
|
156
|
-
* export { aziridProxy as proxy } from "azirid-react/next";
|
|
175
|
+
* export { aziridProxy as proxy } from "azirid-react/next/proxy";
|
|
157
176
|
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
158
177
|
* ```
|
|
159
178
|
*
|
|
160
179
|
* @example Custom options
|
|
161
180
|
* ```ts
|
|
162
181
|
* // proxy.ts
|
|
163
|
-
* import { createAziridProxy } from "azirid-react/next";
|
|
182
|
+
* import { createAziridProxy } from "azirid-react/next/proxy";
|
|
164
183
|
* export const proxy = createAziridProxy({ protectedRoutes: ["/dashboard"] });
|
|
165
184
|
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
166
185
|
* ```
|
package/dist/next.d.ts
CHANGED
|
@@ -26,44 +26,31 @@ declare function createSessionSyncHandler(options?: SessionSyncOptions): {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* Next.js
|
|
29
|
+
* Next.js route handlers for the Azirid auth proxy.
|
|
30
30
|
*
|
|
31
|
-
* This entry point
|
|
32
|
-
*
|
|
31
|
+
* This entry point uses **only standard Web APIs** (Request, Response, fetch)
|
|
32
|
+
* and has **zero dependency on `next/server`**. This makes it safe to use in
|
|
33
|
+
* Next.js standalone builds (`output: 'standalone'`) where external `next/server`
|
|
34
|
+
* imports may fail at runtime.
|
|
33
35
|
*
|
|
34
36
|
* @example Route handler (simplest — one line)
|
|
35
37
|
* ```ts
|
|
36
38
|
* // app/api/auth/[...path]/route.ts
|
|
37
|
-
* export { GET, POST, PUT, PATCH, DELETE } from "azirid-react/next";
|
|
39
|
+
* export { GET, POST, PUT, PATCH, DELETE } from "azirid-react/next/handlers";
|
|
38
40
|
* ```
|
|
39
41
|
*
|
|
40
42
|
* @example Route handler (custom API URL)
|
|
41
43
|
* ```ts
|
|
42
44
|
* // app/api/auth/[...path]/route.ts
|
|
43
|
-
* import { createAziridRouteHandlers } from "azirid-react/next";
|
|
45
|
+
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
44
46
|
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
45
47
|
* apiUrl: "https://my-custom-api.com",
|
|
46
48
|
* debug: true,
|
|
47
49
|
* });
|
|
48
50
|
* ```
|
|
49
51
|
*
|
|
50
|
-
* @example Next.js 16+ (proxy.ts)
|
|
51
|
-
* ```ts
|
|
52
|
-
* // proxy.ts
|
|
53
|
-
* export { aziridProxy as proxy } from "azirid-react/next";
|
|
54
|
-
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)
|
|
58
|
-
* ```ts
|
|
59
|
-
* // middleware.ts
|
|
60
|
-
* export { aziridMiddleware as default } from "azirid-react/next";
|
|
61
|
-
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
52
|
* @packageDocumentation
|
|
65
53
|
*/
|
|
66
|
-
|
|
67
54
|
type RouteHandler = (request: Request, context: {
|
|
68
55
|
params: Promise<{
|
|
69
56
|
path: string[];
|
|
@@ -81,19 +68,19 @@ interface AziridRouteHandlerOptions {
|
|
|
81
68
|
* Create all route handlers for the Azirid auth proxy.
|
|
82
69
|
*
|
|
83
70
|
* Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported
|
|
84
|
-
* from your `app/
|
|
71
|
+
* from your `app/api/auth/[...path]/route.ts`.
|
|
85
72
|
*
|
|
86
73
|
* @example Default (zero config)
|
|
87
74
|
* ```ts
|
|
88
75
|
* // app/api/auth/[...path]/route.ts
|
|
89
|
-
* import { createAziridRouteHandlers } from "azirid-react/next";
|
|
76
|
+
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
90
77
|
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();
|
|
91
78
|
* ```
|
|
92
79
|
*
|
|
93
80
|
* @example Custom options
|
|
94
81
|
* ```ts
|
|
95
82
|
* // app/api/auth/[...path]/route.ts
|
|
96
|
-
* import { createAziridRouteHandlers } from "azirid-react/next";
|
|
83
|
+
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
97
84
|
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
98
85
|
* apiUrl: "https://my-custom-api.com",
|
|
99
86
|
* debug: true,
|
|
@@ -132,6 +119,38 @@ declare function createAziridProxyHandler(apiUrl?: string): RouteHandler;
|
|
|
132
119
|
declare function withAziridProxy(apiUrl?: string): (nextConfig?: Record<string, unknown>) => {
|
|
133
120
|
[x: string]: unknown;
|
|
134
121
|
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Next.js proxy and middleware utilities for Azirid Access.
|
|
125
|
+
*
|
|
126
|
+
* This entry point requires `next/server` and provides request interception
|
|
127
|
+
* for route protection and token forwarding.
|
|
128
|
+
*
|
|
129
|
+
* @example Next.js 16+ (proxy.ts)
|
|
130
|
+
* ```ts
|
|
131
|
+
* // proxy.ts
|
|
132
|
+
* export { aziridProxy as proxy } from "azirid-react/next/proxy";
|
|
133
|
+
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @example Next.js 16+ with custom options
|
|
137
|
+
* ```ts
|
|
138
|
+
* // proxy.ts
|
|
139
|
+
* import { createAziridProxy } from "azirid-react/next/proxy";
|
|
140
|
+
* export const proxy = createAziridProxy({ protectedRoutes: ["/dashboard"] });
|
|
141
|
+
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)
|
|
145
|
+
* ```ts
|
|
146
|
+
* // middleware.ts
|
|
147
|
+
* export { aziridMiddleware as default } from "azirid-react/next/proxy";
|
|
148
|
+
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @packageDocumentation
|
|
152
|
+
*/
|
|
153
|
+
|
|
135
154
|
interface AziridProxyOptions {
|
|
136
155
|
/** Cookie name (default: "__session") */
|
|
137
156
|
cookieName?: string;
|
|
@@ -153,14 +172,14 @@ type AziridMiddlewareOptions = AziridProxyOptions;
|
|
|
153
172
|
* @example Default (one line)
|
|
154
173
|
* ```ts
|
|
155
174
|
* // proxy.ts
|
|
156
|
-
* export { aziridProxy as proxy } from "azirid-react/next";
|
|
175
|
+
* export { aziridProxy as proxy } from "azirid-react/next/proxy";
|
|
157
176
|
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
158
177
|
* ```
|
|
159
178
|
*
|
|
160
179
|
* @example Custom options
|
|
161
180
|
* ```ts
|
|
162
181
|
* // proxy.ts
|
|
163
|
-
* import { createAziridProxy } from "azirid-react/next";
|
|
182
|
+
* import { createAziridProxy } from "azirid-react/next/proxy";
|
|
164
183
|
* export const proxy = createAziridProxy({ protectedRoutes: ["/dashboard"] });
|
|
165
184
|
* export const config = { matcher: ["/((?!_next|favicon.ico|api/).*)"] };
|
|
166
185
|
* ```
|
package/dist/next.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { NextResponse } from 'next/server';
|
|
2
2
|
|
|
3
|
-
// src/next.ts
|
|
4
|
-
|
|
5
3
|
// src/server.ts
|
|
6
4
|
function createSessionSyncHandler(options) {
|
|
7
5
|
const name = options?.cookieName ?? "__session";
|
|
@@ -38,7 +36,7 @@ function createSessionSyncHandler(options) {
|
|
|
38
36
|
};
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
// src/next.ts
|
|
39
|
+
// src/next-handlers.ts
|
|
42
40
|
var AZIRID_API_URL = process.env.AZIRID_API_URL ?? "https://api.azirid.com";
|
|
43
41
|
function createHandler(options) {
|
|
44
42
|
const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\/$/, "");
|
package/dist/next.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server.ts","../src/next.ts"],"names":[],"mappings":";;;;;AA2HO,SAAS,yBAAyB,OAAA,EAA8B;AACrE,EAAA,MAAM,IAAA,GAAO,SAAS,UAAA,IAAc,WAAA;AACpC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,KAAA;AAClC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,IAAA;AAElC,EAAA,OAAO;AAAA,IACL,MAAM,KAAK,GAAA,EAAc;AACvB,MAAA,MAAM,EAAE,KAAA,EAAM,GAAI,MAAM,IAAI,IAAA,EAAK;AACjC,MAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,QAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,KAAA,EAAO,eAAA,EAAiB,CAAA,EAAG;AAAA,UAC9D,MAAA,EAAQ,GAAA;AAAA,UACR,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA;AAAmB,SAC/C,CAAA;AAAA,MACH;AACA,MAAA,MAAM,KAAA,GAAQ,CAAC,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA,EAAI,QAAA,EAAU,UAAA,EAAY,cAAA,EAAgB,CAAA,QAAA,EAAW,MAAM,CAAA,CAAE,CAAA;AAC5F,MAAA,IAAI,MAAA,EAAQ,KAAA,CAAM,IAAA,CAAK,QAAQ,CAAA;AAC/B,MAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,EAAA,EAAI,IAAA,EAAM,CAAA,EAAG;AAAA,QAChD,OAAA,EAAS;AAAA,UACP,cAAA,EAAgB,kBAAA;AAAA,UAChB,YAAA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AAA,GAAW,QAAA;AACf,QAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,UAAA,EAAY,QAAQ,CAAA;AACzC,QAAA,OAAO,YAAA,CAAa,SAAS,GAAG,CAAA;AAAA,MAClC;AAAA,IACF;AAEA,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AACF;AAyBO,SAAS,kBAAkB,OAAA,EAA8B;AAC9D,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAGO,IAAM,cAAc,iBAAA;AAOpB,SAAS,uBAAuB,OAAA,EAA8B;AACnE,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAKO,IAAM,mBAAmB,sBAAA","file":"next.js","sourcesContent":["/**\n * Server-side utilities for Next.js (App Router).\n *\n * Use this entry point in Server Components, Server Actions, and Route Handlers\n * to read the authenticated user's session token from the httpOnly `__session` cookie.\n *\n * @example\n * ```ts\n * // lib/access-server.ts\n * import { cookies } from \"next/headers\";\n * import { createServerAccess } from \"azirid-react/server\";\n * export const { getSessionToken } = createServerAccess({ cookies });\n * ```\n *\n * ```ts\n * // app/actions/profile.ts\n * \"use server\";\n * import { getSessionToken } from \"@/lib/access-server\";\n *\n * export async function getProfile() {\n * const token = await getSessionToken();\n * if (!token) throw new Error(\"Not authenticated\");\n *\n * const res = await fetch(\"https://api.myapp.com/v1/users/auth/me\", {\n * headers: { Authorization: `Bearer ${token}` },\n * });\n * return res.json();\n * }\n * ```\n *\n * @packageDocumentation\n */\n\n/** Minimal cookie store interface (subset of Next.js ReadonlyRequestCookies) */\ninterface CookieStore {\n get(name: string): { value: string } | undefined\n}\n\n/** The `cookies` function from `next/headers` */\ntype CookiesFn = () => CookieStore | Promise<CookieStore>\n\nexport interface ServerAccessOptions {\n /**\n * The `cookies` function imported from `next/headers`.\n * Required — pass it explicitly to avoid bundler issues with dynamic imports.\n *\n * @example\n * ```ts\n * import { cookies } from \"next/headers\";\n * createServerAccess({ cookies });\n * ```\n */\n cookies: CookiesFn\n\n /**\n * Name of the httpOnly cookie that carries the access token.\n * Must match the cookie set by the Azirid API.\n * @default \"__session\"\n */\n cookieName?: string\n}\n\nexport interface ServerAccess {\n /**\n * Read the raw JWT access token from the session cookie.\n * Returns `null` when no cookie is present or when called\n * outside a Next.js server context.\n */\n getSessionToken: () => Promise<string | null>\n\n /** Alias for {@link getSessionToken}. */\n getAccessToken: () => Promise<string | null>\n}\n\n/**\n * Create server-side helpers bound to the session cookie.\n *\n * Call once at module scope, then use the returned functions\n * in any Server Component, Server Action, or Route Handler.\n */\nexport function createServerAccess(options: ServerAccessOptions): ServerAccess {\n const { cookies, cookieName = '__session' } = options\n\n async function getSessionToken(): Promise<string | null> {\n try {\n const result = cookies()\n // Next.js 15+: cookies() returns a Promise\n const cookieStore = result instanceof Promise ? await result : result\n return cookieStore.get(cookieName)?.value ?? null\n } catch {\n return null\n }\n }\n\n return {\n getSessionToken,\n getAccessToken: getSessionToken,\n }\n}\n\n// ─── Session Sync Handler (cross-origin dev support) ─────────\n\nexport interface SessionSyncOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Set Secure flag on cookie (default: false) */\n secure?: boolean\n /** Cookie max age in seconds (default: 3600 = 1h) */\n maxAge?: number\n}\n\n/**\n * Create a route handler that syncs the access token to a local httpOnly cookie.\n *\n * Use this in a Next.js App Router route to bridge cross-origin sessions:\n *\n * @example\n * ```ts\n * // app/api/auth/session/route.ts\n * import { createSessionSyncHandler } from \"azirid-react/server\";\n * export const { POST, DELETE } = createSessionSyncHandler();\n * ```\n */\nexport function createSessionSyncHandler(options?: SessionSyncOptions) {\n const name = options?.cookieName ?? '__session'\n const secure = options?.secure ?? false\n const maxAge = options?.maxAge ?? 3600\n\n return {\n async POST(req: Request) {\n const { token } = await req.json()\n if (!token || typeof token !== 'string') {\n return new Response(JSON.stringify({ error: 'Missing token' }), {\n status: 400,\n headers: { 'Content-Type': 'application/json' },\n })\n }\n const parts = [`${name}=${token}`, 'Path=/', 'HttpOnly', 'SameSite=Lax', `Max-Age=${maxAge}`]\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n async DELETE() {\n const parts = [`${name}=`, 'Path=/', 'HttpOnly', 'SameSite=Lax', 'Max-Age=0']\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n }\n}\n","/**\n * Next.js utilities for Azirid Access — route handlers, proxy, and middleware.\n *\n * This entry point runs in Node.js (NO \"use client\").\n * Supports Next.js 14, 15, and 16+.\n *\n * @example Route handler (simplest — one line)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next\";\n * ```\n *\n * @example Route handler (custom API URL)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n *\n * @example Next.js 16+ (proxy.ts)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)\n * ```ts\n * // middleware.ts\n * export { aziridMiddleware as default } from \"azirid-react/next\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @packageDocumentation\n */\n\nimport { NextResponse, type NextRequest } from 'next/server'\n\n// ─── Constants ───────────────────────────────────────────────\n\n/**\n * Resolve the Azirid API URL.\n *\n * Priority:\n * 1. `AZIRID_API_URL` env var (server-only, recommended)\n * 2. `https://api.azirid.com` (production default)\n *\n * For local development, set `AZIRID_API_URL=http://localhost:3000` in your `.env`.\n */\nconst AZIRID_API_URL = process.env.AZIRID_API_URL ?? 'https://api.azirid.com'\n\n// ─── Types ───────────────────────────────────────────────────\n\ntype RouteHandler = (\n request: Request,\n context: { params: Promise<{ path: string[] }> | { path: string[] } },\n) => Promise<Response>\n\nexport interface AziridRouteHandlerOptions {\n /** Override the Azirid API URL (default: production API) */\n apiUrl?: string\n /** Enable debug logging of proxy requests (default: false) */\n debug?: boolean\n}\n\n// ─── Proxy Route Handler (core logic) ────────────────────────\n\nfunction createHandler(options?: AziridRouteHandlerOptions): RouteHandler {\n const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\\/$/, '')\n const debug = options?.debug ?? false\n\n return async function handler(request, context) {\n // Handle Next.js 14 (sync params), 15 (async params), and 16+ (always async)\n const resolvedParams = context.params instanceof Promise ? await context.params : context.params\n const path = resolvedParams.path.join('/')\n const targetUrl = `${destination}/v1/users/auth/${path}`\n\n if (debug) {\n console.log(`[azirid-proxy] ${request.method} /api/auth/${path} → ${targetUrl}`)\n console.log(`[azirid-proxy] Cookies:`, request.headers.get('cookie') ?? '(none)')\n }\n\n // Forward request headers (strip host so the API sees its own host)\n const forwardHeaders = new Headers()\n request.headers.forEach((value, key) => {\n const lower = key.toLowerCase()\n if (lower !== 'host' && lower !== 'connection') {\n forwardHeaders.set(key, value)\n }\n })\n\n // Forward body for non-GET methods\n const body =\n request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined\n\n // Proxy request to the Azirid API\n const apiResponse = await fetch(targetUrl, {\n method: request.method,\n headers: forwardHeaders,\n body,\n })\n\n // Build response headers, fixing Set-Cookie for first-party context\n const responseHeaders = new Headers()\n apiResponse.headers.forEach((value, key) => {\n if (key.toLowerCase() !== 'set-cookie') {\n responseHeaders.set(key, value)\n }\n })\n\n // Fix Set-Cookie headers: strip Domain, adjust SameSite/Secure for proxy\n const isSecure =\n request.headers.get('x-forwarded-proto') === 'https' ||\n new URL(request.url).protocol === 'https:'\n\n const setCookies: string[] =\n typeof apiResponse.headers.getSetCookie === 'function'\n ? apiResponse.headers.getSetCookie()\n : splitSetCookieHeader(apiResponse.headers.get('set-cookie') ?? '')\n\n for (const cookie of setCookies) {\n let fixed = cookie\n // Strip Domain — cookie will default to the proxy's domain (first-party)\n .replace(/;\\s*[Dd]omain=[^;]*/g, '')\n // Normalize Path to \"/\" — the API may set restrictive paths (e.g. /v1/users/auth/login)\n // which prevents cookies from being sent to other auth endpoints like /refresh or /bootstrap.\n // In proxy mode all auth cookies must be available on all routes.\n .replace(/;\\s*[Pp]ath=[^;]*/g, '')\n // Change SameSite=None to Lax (now same-origin, None is unnecessary)\n .replace(/[Ss]ame[Ss]ite=[Nn]one/g, 'SameSite=Lax')\n\n // Always set Path=/ for first-party proxy cookies\n fixed += '; Path=/'\n\n // In development (HTTP), remove Secure flag so cookies work on localhost\n if (!isSecure) {\n fixed = fixed.replace(/;\\s*[Ss]ecure/g, '')\n }\n\n responseHeaders.append('set-cookie', fixed)\n }\n\n if (debug) {\n console.log(`[azirid-proxy] API responded: ${apiResponse.status}`)\n const responseCookies: string[] = []\n responseHeaders.forEach((value, key) => {\n if (key.toLowerCase() === 'set-cookie') responseCookies.push(value)\n })\n if (responseCookies.length > 0) {\n console.log(`[azirid-proxy] Set-Cookie:`, responseCookies)\n }\n }\n\n return new Response(apiResponse.body, {\n status: apiResponse.status,\n statusText: apiResponse.statusText,\n headers: responseHeaders,\n })\n }\n}\n\n/** Split a raw Set-Cookie header string into individual cookies */\nfunction splitSetCookieHeader(header: string): string[] {\n if (!header) return []\n const cookies: string[] = []\n let current = ''\n for (const part of header.split(', ')) {\n if (current && /^[a-zA-Z0-9_\\-.]+=/.test(part)) {\n cookies.push(current)\n current = part\n } else {\n current = current ? `${current}, ${part}` : part\n }\n }\n if (current) cookies.push(current)\n return cookies\n}\n\n// ─── Route Handlers (ready to re-export) ─────────────────────\n\n/**\n * Create all route handlers for the Azirid auth proxy.\n *\n * Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported\n * from your `app/v1/auth/[...path]/route.ts`.\n *\n * @example Default (zero config)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();\n * ```\n *\n * @example Custom options\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n */\nexport function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {\n GET: RouteHandler\n POST: RouteHandler\n PUT: RouteHandler\n PATCH: RouteHandler\n DELETE: RouteHandler\n} {\n const handler = createHandler(options)\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n }\n}\n\n/**\n * Pre-built route handlers with default config.\n *\n * Just re-export these directly — no setup required:\n *\n * @example\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next\";\n * ```\n */\nconst defaultHandlers = createAziridRouteHandlers()\nexport const GET = defaultHandlers.GET\nexport const POST = defaultHandlers.POST\nexport const PUT = defaultHandlers.PUT\nexport const PATCH = defaultHandlers.PATCH\nexport const DELETE = defaultHandlers.DELETE\n\n/**\n * Create a single Next.js route handler function.\n *\n * Use this if you need a single handler reference (advanced use).\n * For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.\n *\n * @deprecated Use `createAziridRouteHandlers()` instead.\n */\nexport function createAziridProxyHandler(apiUrl: string = AZIRID_API_URL): RouteHandler {\n return createHandler({ apiUrl })\n}\n\n// ─── Config Helper ───────────────────────────────────────────\n\n/**\n * Wrap your Next.js config with Azirid settings.\n *\n * In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.\n * This helper is kept for backward compatibility with Next.js 14/15.\n *\n * @param apiUrl - Override the Azirid API URL (default: production API)\n */\nexport function withAziridProxy(apiUrl: string = AZIRID_API_URL) {\n void apiUrl\n\n return (nextConfig: Record<string, unknown> = {}) => ({\n ...nextConfig,\n })\n}\n\n// ─── Proxy / Middleware Options ──────────────────────────────\n\nexport interface AziridProxyOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Routes that require authentication (glob patterns via startsWith) */\n protectedRoutes?: string[]\n /** Route to redirect to when not authenticated (default: \"/login\") */\n loginUrl?: string\n /** Routes that are always public (default: [\"/login\", \"/signup\"]) */\n publicRoutes?: string[]\n}\n\n/** @deprecated Use `AziridProxyOptions` instead */\nexport type AziridMiddlewareOptions = AziridProxyOptions\n\n// ─── Shared proxy/middleware logic ───────────────────────────\n\nfunction createRequestInterceptor(options?: AziridProxyOptions) {\n const cookieName = options?.cookieName ?? '__session'\n const loginUrl = options?.loginUrl ?? '/login'\n const publicRoutes = options?.publicRoutes ?? ['/login', '/signup']\n const protectedRoutes = options?.protectedRoutes\n\n return function handler(request: NextRequest) {\n const { pathname } = request.nextUrl\n const token = request.cookies.get(cookieName)?.value\n\n const response = NextResponse.next()\n\n // Expose token as internal header for server components\n if (token) {\n response.headers.set('x-azirid-token', token)\n }\n\n // Route protection (only when protectedRoutes is configured)\n if (protectedRoutes) {\n const isPublic = publicRoutes.some((r) => pathname.startsWith(r))\n const isProtected = protectedRoutes.some((r) => pathname.startsWith(r))\n\n if (isProtected && !isPublic && !token) {\n const url = request.nextUrl.clone()\n url.pathname = loginUrl\n url.searchParams.set('redirect', pathname)\n return NextResponse.redirect(url)\n }\n }\n\n return response\n }\n}\n\n// ─── Proxy (Next.js 16+) ────────────────────────────────────\n\n/**\n * Create a customized Azirid proxy for Next.js 16+.\n *\n * In Next.js 16, `middleware.ts` is renamed to `proxy.ts` and the\n * exported function must be named `proxy`.\n *\n * @example Default (one line)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n */\nexport function createAziridProxy(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/** Default proxy export for Next.js 16+ (no route protection, only forwards token) */\nexport const aziridProxy = createAziridProxy()\n\n// ─── Middleware (Next.js 14/15 — deprecated in 16) ──────────\n\n/**\n * @deprecated In Next.js 16+, use `createAziridProxy` and `proxy.ts` instead.\n */\nexport function createAziridMiddleware(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/**\n * @deprecated In Next.js 16+, use `aziridProxy` and `proxy.ts` instead.\n */\nexport const aziridMiddleware = createAziridMiddleware()\n\n// ─── Re-export session sync for backward compat ──────────────\n\nexport { createSessionSyncHandler } from './server'\nexport type { SessionSyncOptions } from './server'\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/server.ts","../src/next-handlers.ts","../src/next-proxy.ts"],"names":[],"mappings":";;;AA2HO,SAAS,yBAAyB,OAAA,EAA8B;AACrE,EAAA,MAAM,IAAA,GAAO,SAAS,UAAA,IAAc,WAAA;AACpC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,KAAA;AAClC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,IAAA;AAElC,EAAA,OAAO;AAAA,IACL,MAAM,KAAK,GAAA,EAAc;AACvB,MAAA,MAAM,EAAE,KAAA,EAAM,GAAI,MAAM,IAAI,IAAA,EAAK;AACjC,MAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,QAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,KAAA,EAAO,eAAA,EAAiB,CAAA,EAAG;AAAA,UAC9D,MAAA,EAAQ,GAAA;AAAA,UACR,OAAA,EAAS,EAAE,cAAA,E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,CAAQ,GAAA,CAAI,gBAAA,EAAkB,KAAK,CAAA;AAAA,IAC9C;AAGA,IAAA,IAAI,eAAA,EAAiB;AACnB,MAAA,MAAM,QAAA,GAAW,aAAa,IAAA,CAAK,CAAC,MAAM,QAAA,CAAS,UAAA,CAAW,CAAC,CAAC,CAAA;AAChE,MAAA,MAAM,WAAA,GAAc,gBAAgB,IAAA,CAAK,CAAC,MAAM,QAAA,CAAS,UAAA,CAAW,CAAC,CAAC,CAAA;AAEtE,MAAA,IAAI,WAAA,IAAe,CAAC,QAAA,IAAY,CAAC,KAAA,EAAO;AACtC,QAAA,MAAM,GAAA,GAAM,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAM;AAClC,QAAA,GAAA,CAAI,QAAA,GAAW,QAAA;AACf,QAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,UAAA,EAAY,QAAQ,CAAA;AACzC,QAAA,OAAO,YAAA,CAAa,SAAS,GAAG,CAAA;AAAA,MAClC;AAAA,IACF;AAEA,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AACF;AAyBO,SAAS,kBAAkB,OAAA,EAA8B;AAC9D,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAGO,IAAM,cAAc,iBAAA;AAOpB,SAAS,uBAAuB,OAAA,EAA8B;AACnE,EAAA,OAAO,yBAAyB,OAAO,CAAA;AACzC;AAKO,IAAM,mBAAmB,sBAAA","file":"next.js","sourcesContent":["/**\n * Server-side utilities for Next.js (App Router).\n *\n * Use this entry point in Server Components, Server Actions, and Route Handlers\n * to read the authenticated user's session token from the httpOnly `__session` cookie.\n *\n * @example\n * ```ts\n * // lib/access-server.ts\n * import { cookies } from \"next/headers\";\n * import { createServerAccess } from \"azirid-react/server\";\n * export const { getSessionToken } = createServerAccess({ cookies });\n * ```\n *\n * ```ts\n * // app/actions/profile.ts\n * \"use server\";\n * import { getSessionToken } from \"@/lib/access-server\";\n *\n * export async function getProfile() {\n * const token = await getSessionToken();\n * if (!token) throw new Error(\"Not authenticated\");\n *\n * const res = await fetch(\"https://api.myapp.com/v1/users/auth/me\", {\n * headers: { Authorization: `Bearer ${token}` },\n * });\n * return res.json();\n * }\n * ```\n *\n * @packageDocumentation\n */\n\n/** Minimal cookie store interface (subset of Next.js ReadonlyRequestCookies) */\ninterface CookieStore {\n get(name: string): { value: string } | undefined\n}\n\n/** The `cookies` function from `next/headers` */\ntype CookiesFn = () => CookieStore | Promise<CookieStore>\n\nexport interface ServerAccessOptions {\n /**\n * The `cookies` function imported from `next/headers`.\n * Required — pass it explicitly to avoid bundler issues with dynamic imports.\n *\n * @example\n * ```ts\n * import { cookies } from \"next/headers\";\n * createServerAccess({ cookies });\n * ```\n */\n cookies: CookiesFn\n\n /**\n * Name of the httpOnly cookie that carries the access token.\n * Must match the cookie set by the Azirid API.\n * @default \"__session\"\n */\n cookieName?: string\n}\n\nexport interface ServerAccess {\n /**\n * Read the raw JWT access token from the session cookie.\n * Returns `null` when no cookie is present or when called\n * outside a Next.js server context.\n */\n getSessionToken: () => Promise<string | null>\n\n /** Alias for {@link getSessionToken}. */\n getAccessToken: () => Promise<string | null>\n}\n\n/**\n * Create server-side helpers bound to the session cookie.\n *\n * Call once at module scope, then use the returned functions\n * in any Server Component, Server Action, or Route Handler.\n */\nexport function createServerAccess(options: ServerAccessOptions): ServerAccess {\n const { cookies, cookieName = '__session' } = options\n\n async function getSessionToken(): Promise<string | null> {\n try {\n const result = cookies()\n // Next.js 15+: cookies() returns a Promise\n const cookieStore = result instanceof Promise ? await result : result\n return cookieStore.get(cookieName)?.value ?? null\n } catch {\n return null\n }\n }\n\n return {\n getSessionToken,\n getAccessToken: getSessionToken,\n }\n}\n\n// ─── Session Sync Handler (cross-origin dev support) ─────────\n\nexport interface SessionSyncOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Set Secure flag on cookie (default: false) */\n secure?: boolean\n /** Cookie max age in seconds (default: 3600 = 1h) */\n maxAge?: number\n}\n\n/**\n * Create a route handler that syncs the access token to a local httpOnly cookie.\n *\n * Use this in a Next.js App Router route to bridge cross-origin sessions:\n *\n * @example\n * ```ts\n * // app/api/auth/session/route.ts\n * import { createSessionSyncHandler } from \"azirid-react/server\";\n * export const { POST, DELETE } = createSessionSyncHandler();\n * ```\n */\nexport function createSessionSyncHandler(options?: SessionSyncOptions) {\n const name = options?.cookieName ?? '__session'\n const secure = options?.secure ?? false\n const maxAge = options?.maxAge ?? 3600\n\n return {\n async POST(req: Request) {\n const { token } = await req.json()\n if (!token || typeof token !== 'string') {\n return new Response(JSON.stringify({ error: 'Missing token' }), {\n status: 400,\n headers: { 'Content-Type': 'application/json' },\n })\n }\n const parts = [`${name}=${token}`, 'Path=/', 'HttpOnly', 'SameSite=Lax', `Max-Age=${maxAge}`]\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n async DELETE() {\n const parts = [`${name}=`, 'Path=/', 'HttpOnly', 'SameSite=Lax', 'Max-Age=0']\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n }\n}\n","/**\n * Next.js route handlers for the Azirid auth proxy.\n *\n * This entry point uses **only standard Web APIs** (Request, Response, fetch)\n * and has **zero dependency on `next/server`**. This makes it safe to use in\n * Next.js standalone builds (`output: 'standalone'`) where external `next/server`\n * imports may fail at runtime.\n *\n * @example Route handler (simplest — one line)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n *\n * @example Route handler (custom API URL)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n *\n * @packageDocumentation\n */\n\n// ─── Constants ───────────────────────────────────────────────\n\n/**\n * Resolve the Azirid API URL.\n *\n * Priority:\n * 1. `AZIRID_API_URL` env var (server-only, recommended)\n * 2. `https://api.azirid.com` (production default)\n *\n * For local development, set `AZIRID_API_URL=http://localhost:3000` in your `.env`.\n */\nconst AZIRID_API_URL = process.env.AZIRID_API_URL ?? 'https://api.azirid.com'\n\n// ─── Types ───────────────────────────────────────────────────\n\ntype RouteHandler = (\n request: Request,\n context: { params: Promise<{ path: string[] }> | { path: string[] } },\n) => Promise<Response>\n\nexport interface AziridRouteHandlerOptions {\n /** Override the Azirid API URL (default: production API) */\n apiUrl?: string\n /** Enable debug logging of proxy requests (default: false) */\n debug?: boolean\n}\n\n// ─── Proxy Route Handler (core logic) ────────────────────────\n\nfunction createHandler(options?: AziridRouteHandlerOptions): RouteHandler {\n const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\\/$/, '')\n const debug = options?.debug ?? false\n\n return async function handler(request, context) {\n // Handle Next.js 14 (sync params), 15 (async params), and 16+ (always async)\n const resolvedParams = context.params instanceof Promise ? await context.params : context.params\n const path = resolvedParams.path.join('/')\n const targetUrl = `${destination}/v1/users/auth/${path}`\n\n if (debug) {\n console.log(`[azirid-proxy] ${request.method} /api/auth/${path} → ${targetUrl}`)\n console.log(`[azirid-proxy] Cookies:`, request.headers.get('cookie') ?? '(none)')\n }\n\n // Forward request headers (strip host so the API sees its own host)\n const forwardHeaders = new Headers()\n request.headers.forEach((value, key) => {\n const lower = key.toLowerCase()\n if (lower !== 'host' && lower !== 'connection') {\n forwardHeaders.set(key, value)\n }\n })\n\n // Forward body for non-GET methods\n const body =\n request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined\n\n // Proxy request to the Azirid API\n const apiResponse = await fetch(targetUrl, {\n method: request.method,\n headers: forwardHeaders,\n body,\n })\n\n // Build response headers, fixing Set-Cookie for first-party context\n const responseHeaders = new Headers()\n apiResponse.headers.forEach((value, key) => {\n if (key.toLowerCase() !== 'set-cookie') {\n responseHeaders.set(key, value)\n }\n })\n\n // Fix Set-Cookie headers: strip Domain, adjust SameSite/Secure for proxy\n const isSecure =\n request.headers.get('x-forwarded-proto') === 'https' ||\n new URL(request.url).protocol === 'https:'\n\n const setCookies: string[] =\n typeof apiResponse.headers.getSetCookie === 'function'\n ? apiResponse.headers.getSetCookie()\n : splitSetCookieHeader(apiResponse.headers.get('set-cookie') ?? '')\n\n for (const cookie of setCookies) {\n let fixed = cookie\n // Strip Domain — cookie will default to the proxy's domain (first-party)\n .replace(/;\\s*[Dd]omain=[^;]*/g, '')\n // Normalize Path to \"/\" — the API may set restrictive paths (e.g. /v1/users/auth/login)\n // which prevents cookies from being sent to other auth endpoints like /refresh or /bootstrap.\n // In proxy mode all auth cookies must be available on all routes.\n .replace(/;\\s*[Pp]ath=[^;]*/g, '')\n // Change SameSite=None to Lax (now same-origin, None is unnecessary)\n .replace(/[Ss]ame[Ss]ite=[Nn]one/g, 'SameSite=Lax')\n\n // Always set Path=/ for first-party proxy cookies\n fixed += '; Path=/'\n\n // In development (HTTP), remove Secure flag so cookies work on localhost\n if (!isSecure) {\n fixed = fixed.replace(/;\\s*[Ss]ecure/g, '')\n }\n\n responseHeaders.append('set-cookie', fixed)\n }\n\n if (debug) {\n console.log(`[azirid-proxy] API responded: ${apiResponse.status}`)\n const responseCookies: string[] = []\n responseHeaders.forEach((value, key) => {\n if (key.toLowerCase() === 'set-cookie') responseCookies.push(value)\n })\n if (responseCookies.length > 0) {\n console.log(`[azirid-proxy] Set-Cookie:`, responseCookies)\n }\n }\n\n return new Response(apiResponse.body, {\n status: apiResponse.status,\n statusText: apiResponse.statusText,\n headers: responseHeaders,\n })\n }\n}\n\n/** Split a raw Set-Cookie header string into individual cookies */\nfunction splitSetCookieHeader(header: string): string[] {\n if (!header) return []\n const cookies: string[] = []\n let current = ''\n for (const part of header.split(', ')) {\n if (current && /^[a-zA-Z0-9_\\-.]+=/.test(part)) {\n cookies.push(current)\n current = part\n } else {\n current = current ? `${current}, ${part}` : part\n }\n }\n if (current) cookies.push(current)\n return cookies\n}\n\n// ─── Route Handlers (ready to re-export) ─────────────────────\n\n/**\n * Create all route handlers for the Azirid auth proxy.\n *\n * Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported\n * from your `app/api/auth/[...path]/route.ts`.\n *\n * @example Default (zero config)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();\n * ```\n *\n * @example Custom options\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n */\nexport function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {\n GET: RouteHandler\n POST: RouteHandler\n PUT: RouteHandler\n PATCH: RouteHandler\n DELETE: RouteHandler\n} {\n const handler = createHandler(options)\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n }\n}\n\n/**\n * Pre-built route handlers with default config.\n *\n * Just re-export these directly — no setup required:\n *\n * @example\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n */\nconst defaultHandlers = createAziridRouteHandlers()\nexport const GET = defaultHandlers.GET\nexport const POST = defaultHandlers.POST\nexport const PUT = defaultHandlers.PUT\nexport const PATCH = defaultHandlers.PATCH\nexport const DELETE = defaultHandlers.DELETE\n\n/**\n * Create a single Next.js route handler function.\n *\n * Use this if you need a single handler reference (advanced use).\n * For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.\n *\n * @deprecated Use `createAziridRouteHandlers()` instead.\n */\nexport function createAziridProxyHandler(apiUrl: string = AZIRID_API_URL): RouteHandler {\n return createHandler({ apiUrl })\n}\n\n// ─── Config Helper ───────────────────────────────────────────\n\n/**\n * Wrap your Next.js config with Azirid settings.\n *\n * In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.\n * This helper is kept for backward compatibility with Next.js 14/15.\n *\n * @param apiUrl - Override the Azirid API URL (default: production API)\n */\nexport function withAziridProxy(apiUrl: string = AZIRID_API_URL) {\n void apiUrl\n\n return (nextConfig: Record<string, unknown> = {}) => ({\n ...nextConfig,\n })\n}\n\n// ─── Re-export session sync for backward compat ──────────────\n\nexport { createSessionSyncHandler } from './server'\nexport type { SessionSyncOptions } from './server'\n","/**\n * Next.js proxy and middleware utilities for Azirid Access.\n *\n * This entry point requires `next/server` and provides request interception\n * for route protection and token forwarding.\n *\n * @example Next.js 16+ (proxy.ts)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 16+ with custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next/proxy\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Next.js 14/15 (middleware.ts — deprecated in Next.js 16)\n * ```ts\n * // middleware.ts\n * export { aziridMiddleware as default } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @packageDocumentation\n */\n\nimport { NextResponse, type NextRequest } from 'next/server'\n\n// ─── Proxy / Middleware Options ──────────────────────────────\n\nexport interface AziridProxyOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Routes that require authentication (glob patterns via startsWith) */\n protectedRoutes?: string[]\n /** Route to redirect to when not authenticated (default: \"/login\") */\n loginUrl?: string\n /** Routes that are always public (default: [\"/login\", \"/signup\"]) */\n publicRoutes?: string[]\n}\n\n/** @deprecated Use `AziridProxyOptions` instead */\nexport type AziridMiddlewareOptions = AziridProxyOptions\n\n// ─── Shared proxy/middleware logic ───────────────────────────\n\nfunction createRequestInterceptor(options?: AziridProxyOptions) {\n const cookieName = options?.cookieName ?? '__session'\n const loginUrl = options?.loginUrl ?? '/login'\n const publicRoutes = options?.publicRoutes ?? ['/login', '/signup']\n const protectedRoutes = options?.protectedRoutes\n\n return function handler(request: NextRequest) {\n const { pathname } = request.nextUrl\n const token = request.cookies.get(cookieName)?.value\n\n const response = NextResponse.next()\n\n // Expose token as internal header for server components\n if (token) {\n response.headers.set('x-azirid-token', token)\n }\n\n // Route protection (only when protectedRoutes is configured)\n if (protectedRoutes) {\n const isPublic = publicRoutes.some((r) => pathname.startsWith(r))\n const isProtected = protectedRoutes.some((r) => pathname.startsWith(r))\n\n if (isProtected && !isPublic && !token) {\n const url = request.nextUrl.clone()\n url.pathname = loginUrl\n url.searchParams.set('redirect', pathname)\n return NextResponse.redirect(url)\n }\n }\n\n return response\n }\n}\n\n// ─── Proxy (Next.js 16+) ────────────────────────────────────\n\n/**\n * Create a customized Azirid proxy for Next.js 16+.\n *\n * In Next.js 16, `middleware.ts` is renamed to `proxy.ts` and the\n * exported function must be named `proxy`.\n *\n * @example Default (one line)\n * ```ts\n * // proxy.ts\n * export { aziridProxy as proxy } from \"azirid-react/next/proxy\";\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n *\n * @example Custom options\n * ```ts\n * // proxy.ts\n * import { createAziridProxy } from \"azirid-react/next/proxy\";\n * export const proxy = createAziridProxy({ protectedRoutes: [\"/dashboard\"] });\n * export const config = { matcher: [\"/((?!_next|favicon.ico|api/).*)\"] };\n * ```\n */\nexport function createAziridProxy(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/** Default proxy export for Next.js 16+ (no route protection, only forwards token) */\nexport const aziridProxy = createAziridProxy()\n\n// ─── Middleware (Next.js 14/15 — deprecated in 16) ──────────\n\n/**\n * @deprecated In Next.js 16+, use `createAziridProxy` and `proxy.ts` instead.\n */\nexport function createAziridMiddleware(options?: AziridProxyOptions) {\n return createRequestInterceptor(options)\n}\n\n/**\n * @deprecated In Next.js 16+, use `aziridProxy` and `proxy.ts` instead.\n */\nexport const aziridMiddleware = createAziridMiddleware()\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "azirid-react",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
4
4
|
"description": "Authentication components for React and Next.js — Login, Register, powered by TanStack Query and Zod.",
|
|
5
5
|
"author": "Azirid",
|
|
6
6
|
"license": "MIT",
|
|
@@ -57,6 +57,26 @@
|
|
|
57
57
|
"default": "./dist/server.cjs"
|
|
58
58
|
}
|
|
59
59
|
},
|
|
60
|
+
"./next/handlers": {
|
|
61
|
+
"import": {
|
|
62
|
+
"types": "./dist/next-handlers.d.ts",
|
|
63
|
+
"default": "./dist/next-handlers.js"
|
|
64
|
+
},
|
|
65
|
+
"require": {
|
|
66
|
+
"types": "./dist/next-handlers.d.cts",
|
|
67
|
+
"default": "./dist/next-handlers.cjs"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"./next/proxy": {
|
|
71
|
+
"import": {
|
|
72
|
+
"types": "./dist/next-proxy.d.ts",
|
|
73
|
+
"default": "./dist/next-proxy.js"
|
|
74
|
+
},
|
|
75
|
+
"require": {
|
|
76
|
+
"types": "./dist/next-proxy.d.cts",
|
|
77
|
+
"default": "./dist/next-proxy.cjs"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
60
80
|
"./next": {
|
|
61
81
|
"import": {
|
|
62
82
|
"types": "./dist/next.d.ts",
|