auth-proxy.ts 943 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Helpers for first-party Briven Auth proxy (same pattern as Mavi).
  3. * Browser → /api/auth/* → https://api.briven.tech/v1/auth-core/fdi/*
  4. */
  5. /** Strip Domain= so Set-Cookie sticks to localhost / your app host. */
  6. export function rewriteSetCookieForFirstParty(setCookie: string): string {
  7. return setCookie
  8. .replace(/;\s*[Dd]omain=[^;]*/g, "")
  9. .replace(/;\s*[Ss]ame[Ss]ite=None/gi, "; SameSite=Lax");
  10. }
  11. export function brivenUpstreamOrigin(): string {
  12. return (
  13. process.env.BRIVEN_API_ORIGIN ||
  14. process.env.NEXT_PUBLIC_BRIVEN_API_ORIGIN ||
  15. "https://api.briven.tech"
  16. ).replace(/\/$/, "");
  17. }
  18. export function collectSetCookies(headers: Headers): string[] {
  19. const anyHeaders = headers as Headers & { getSetCookie?: () => string[] };
  20. if (typeof anyHeaders.getSetCookie === "function") {
  21. return anyHeaders.getSetCookie();
  22. }
  23. const single = headers.get("set-cookie");
  24. return single ? [single] : [];
  25. }