Proxying the auth endpoints under your own origin
Serve the auth endpoints from your app's own origin so every auth cookie is first-party: recipes for Next.js, Vite and other hosts.
A browser treats a cookie as first-party only when it comes from the origin the
page is on. An app on app.acme.com talking to an auth server on another
hostname is cross-site, so the refresh cookie is a third-party cookie — blocked
outright in Safari, and on borrowed time everywhere else.
There are two ways to stop being cross-site, and either is fine in production:
| How | In Safari | |
|---|---|---|
| Proxy (this page) | Forward two path prefixes from the origin your app is already served from to the auth server. No DNS record, no certificate, no verification; one rule wherever the app is hosted. | Sessions last as long as the app's refresh lifetime. |
| Custom auth domain | auth.acme.com, a CNAME to Pontive, called directly. | Safari caps cookies set through a CNAME to another company's servers at 7 days, so a user away for more than 7 days signs in again. Active users stay signed in: every refresh resets the 7 days. |
Prefer the proxy where your host can do it — almost all can, without any server code, as the recipes below show. Where it cannot (GitHub Pages, a bare S3 website), use a custom auth domain. An app that signs people in on the hosted pages, rather than with embedded widgets, needs neither.
The proxy carries only the auth domain's endpoints the browser calls with
fetch: pontkit's frontend API (/v1/frontend), the signed-in user's own
surface (/v1/me: profile, sessions, memberships, billing) and OAuth
(/oauth2). Organization management goes to the API host
(https://api.{region}.pontive.com/v1/…) with a bearer token, and needs no
proxy.
The two halves
Forward three prefixes to the auth server — /v1/frontend, /v1/me and
/oauth2, under a base path, /__pontive unless you choose another:
https://app.acme.com/__pontive/oauth2/token → https://<auth-host>/oauth2/tokenPoint the SDK at the base path instead of the auth hostname:
<Provider domain="/__pontive" appId={appId} signinUrl="/signin" signupUrl="/signup" />Those two have to agree. The helpers below derive both from one setting where they can.
If you mount the proxy anywhere but /__pontive or the root — your app runs
under a sub-path, or your gateway already routes /api/* — register that path
on the app (dashboard → the app's settings → Proxy paths). The links Pontive
emails during sign-up and sign-in come back through it, and an unregistered path
sends them to your bare origin instead. The dashboard tells you when that
happens.
Every recipe, the same four rules
Whatever does the forwarding:
- Send the auth host as
Host, not your app's. The auth server resolves which project it is answering as fromHost; your hostname arrives as "no such project". - Pass
Set-Cookiethrough untouched — every one of them, unscoped, unrenamed, unfolded. - Allow every method (
GET,POST,OPTIONSat least) and cache nothing. - Do not follow redirects on the browser's behalf.
The section at the end, What the proxy must not do, says why.
Next.js
A rewrite is enough. A rewrite to an external destination sends the
destination's Host upstream, which is what the auth server needs.
// next.config.mjs
import { authProxyRewrites } from "@pontive/pontkit-nextjs/server";
export default {
rewrites: async () => authProxyRewrites({ authHost: process.env.PONTIVE_AUTH_HOST }),
};Middleware is for when something has to happen on the way through — a client IP to resolve, a path decided at runtime:
// middleware.ts
import { createAuthProxy } from "@pontive/pontkit-nextjs/server";
import { NextResponse } from "next/server";
const authProxy = createAuthProxy({ authHost: process.env.PONTIVE_AUTH_HOST! });
export function middleware(request: Request) {
return authProxy.handles(request) ? authProxy.forward(request) : NextResponse.next();
}
export const config = { matcher: authProxy.matcher };Vite (development)
Vite has a proxy of its own; what it does not have is the prefixes and
changeOrigin.
// vite.config.ts
import { viteAuthProxy } from "@pontive/pontkit-loader/proxy";
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
return {
server: { proxy: viteAuthProxy({ authHost: env.VITE_PONTIVE_AUTH_HOST }) },
};
});Worth doing even where the browser tolerates the cross-site setup: a dev server that proxies is rehearsing the topology production runs on. It is also the only way an embedded sign-in works in Safari against a shared Pontive domain.
The Vite proxy runs only in development. In production, use the recipe for wherever the built app is hosted.
Vercel
An external rewrite in vercel.json. Put the auth rules before your SPA
fallback, so they win.
{
"rewrites": [
{ "source": "/__pontive/v1/frontend/:path*", "destination": "https://<auth-host>/v1/frontend/:path*" },
{ "source": "/__pontive/v1/me/:path*", "destination": "https://<auth-host>/v1/me/:path*" },
{ "source": "/__pontive/oauth2/:path*", "destination": "https://<auth-host>/oauth2/:path*" },
{ "source": "/(.*)", "destination": "/index.html" }
]
}Netlify
Proxying rewrites (status 200) in _redirects, again before the SPA fallback:
/__pontive/v1/frontend/* https://<auth-host>/v1/frontend/:splat 200
/__pontive/v1/me/* https://<auth-host>/v1/me/:splat 200
/__pontive/oauth2/* https://<auth-host>/oauth2/:splat 200
/* /index.html 200Or the same in netlify.toml as [[redirects]] entries with status = 200.
Cloudflare
A Worker in front of the app, or the Worker that serves it. No dependency needed:
export default {
async fetch(request, env) {
const url = new URL(request.url);
const auth = url.pathname.match(/^\/__pontive(\/(?:v1\/frontend|v1\/me|oauth2)\/.*)$/);
// Not an auth path: serve the app as usual (static assets binding here).
if (!auth) return env.ASSETS.fetch(request);
const upstream = new URL(auth[1] + url.search, `https://${env.PONTIVE_AUTH_HOST}`);
// A new URL means a new Host — the auth host's. Redirects go back to the
// browser as they are.
return fetch(new Request(upstream, request), { redirect: "manual" });
},
};createAuthProxy (from @pontive/pontkit-nextjs/server) is a Web-standard
handler and runs in Workers too, if you would rather use it.
AWS CloudFront (S3 or any origin)
No code beyond a four-line function, because CloudFront's Origin path can only
prepend.
-
Origin: add one for
<auth-host>, HTTPS only, origin path empty. -
Function (CloudFront Functions, viewer request, runtime
cloudfront-js-2.0), published:function handler(event) { var request = event.request; request.uri = request.uri.replace(/^\/__pontive(?=\/|$)/, ""); return request; } -
Behaviour on
/__pontive/*→ that origin, with:- viewer protocol policy: redirect HTTP to HTTPS
- allowed methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
- cache policy: Managed-CachingDisabled
- origin request policy: Managed-AllViewerExceptHostHeader — not
AllViewer, which forwards your app's
Host - function association: viewer request → the function above
- precedence above your app's own behaviours (e.g.
/*or a sub-path like/portal*), or they catch the request first and serve yourindex.html
An app served under a sub-path (/portal) uses /portal/__pontive/*, strips
/portal/__pontive in the function, and registers /portal/__pontive as a
proxy path.
nginx
location ~ ^/__pontive/(v1/frontend|v1/me|oauth2)/ {
rewrite ^/__pontive/(.*)$ /$1 break;
proxy_pass https://<auth-host>;
proxy_set_header Host <auth-host>;
proxy_ssl_server_name on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}Caddy
handle_path /__pontive/* {
@auth path /v1/frontend/* /v1/me/* /oauth2/*
reverse_proxy @auth https://<auth-host> {
header_up Host {upstream_hostport}
}
}Hosts that only serve files
GitHub Pages, a bare S3 website and the like have nowhere to forward a request. Put CloudFront (or Cloudflare) in front of them and use that recipe, or use a custom auth domain and accept Safari's 7-day limit for users who are away longer.
Checking it works
From a terminal, against the deployed app:
# 200, JSON — the request reached the auth server, as your project
curl -s https://app.acme.com/__pontive/v1/frontend/runtime-config
# 400 "grant_type is required" — POST is forwarded, and answered by the auth server
curl -s -X POST https://app.acme.com/__pontive/oauth2/tokenAn HTML page means your app's own routing caught the request: the rule is
missing, or ordered after the SPA fallback. A 401 or "no such project" means
your app's Host reached the auth server (rule 1).
What the proxy must not do
Each of these fails silently — the requests still work, the session just does not persist:
- Do not rewrite
Set-Cookie. The server writes it with noDomain, which is what makes it host-only on your origin. Scoping or renaming it undoes the whole exercise. - Do not fold repeated
Set-Cookieheaders into one. A cookie carrying anExpiresdate contains a comma, so the fold produces something the browser cannot parse.createAuthProxyusesgetSetCookiefor this reason. - Do not send your own Host upstream. The auth server resolves which project
it is answering as from
Host. Your hostname arrives as "no such project" and the symptom is a CORS refusal and an empty issuer. - Do not overwrite
X-Forwarded-Forwith the immediate peer. That forwards the address of whatever CDN is in front of you, so every user upstream looks like one client. - Do not cache, and do not follow redirects on the browser's behalf.
What is deliberately not proxied
Only the endpoints the browser calls with fetch for the session. /authorize,
the hosted sign-in page and a redirect-mode /logout are top-level navigations,
and a SameSite=Lax cookie is sent on those from any origin — so proxying them
buys nothing, and would break: the authorization server builds those URLs on its
own hostname, so the browser leaves the proxy at the first redirect regardless.
Nor the platform API: account, organization and member management and billing are called on the API host with the user's access token as a bearer token. No cookie is involved, so there is nothing for a proxy to make first-party.