[PR #4901] [CLOSED] fix(vue): improve baseURL normalization for Nuxt SSR compatibility #5647

Closed
opened 2026-03-13 12:30:22 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4901
Author: @Mateleo
Created: 9/25/2025
Status: Closed

Base: canaryHead: fix/nuxtssr-custom-baseurl


📝 Commits (3)

  • 1389a9e fix(vue): improve baseURL normalization for SSR compatibility
  • 8a42301 fix: ensure baseURL normalization is case-insensitive for SSR compatibility
  • b859ce3 chore: lint

📊 Changes

1 file changed (+36 additions, -2 deletions)

View changed files

📝 packages/better-auth/src/client/vue/index.ts (+36 -2)

📄 Description

Following the discussion in #4722, and after the fix that landed in #4887, Nuxt useSession(useFetch) started working again only when using the default /api/auth base path.

However, the fix did not cover cases where a custom base path is configured (e.g. /api/v1/auth).

Like this :

export const authClient = createAuthClient({
  baseURL: "http://localhost:3000/api/v1/auth",
});

In these cases, the client still builds an absolute URL for get-session (like so http://localhost:3000/api/v1/auth), which in Nuxt SSR prevents cookies from being forwarded. The result is that session is always null, even though the endpoint responds correctly in the browser.

  const loggedFetch = ((url: string, opts?: any) => {
    console.log("[useSession] fetching:", url);
    return useFetch(url, opts);
  }) as any;

  const { data: session, error } = await authClient.useSession(loggedFetch);
  // return [useSession] fetching: http://localhost:3000/api/v1/auth/get-session
  // After this PR
  // return [useSession] fetching: /api/v1/auth/get-session

This PR ensures the client:

  • Normalizes the baseURL to extract only the pathname.
  • Defaults / to /api/auth.
  • Trims trailing slashes.
  • Always calls a relative endpoint for …/get-session, which allows Nuxt SSR to forward cookies properly.

For Nuxt SSR custom base url

  • Before #4887: regression → always null.
  • After #4887: works only for default baseURL.
  • With this PR: works for both default and custom base paths, without needing app-side workarounds (like wrapping useFetch to strip the origin).

Summary by cubic

Fixes Nuxt SSR cookie forwarding when using a custom auth baseURL. We now normalize baseURL and call a relative /get-session path so useSession returns the session for both default and custom paths.

  • Bug Fixes
    • Normalize baseURL to a relative pathname; strip origin if absolute.
    • Ensure leading slash, trim trailing slashes, fallback to /api/auth.
    • Build /get-session from the normalized path to avoid absolute requests in SSR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4901 **Author:** [@Mateleo](https://github.com/Mateleo) **Created:** 9/25/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/nuxtssr-custom-baseurl` --- ### 📝 Commits (3) - [`1389a9e`](https://github.com/better-auth/better-auth/commit/1389a9e08e2668595765101bb1823325e34501d8) fix(vue): improve baseURL normalization for SSR compatibility - [`8a42301`](https://github.com/better-auth/better-auth/commit/8a423017474cf38d92467a3eeea1ff353582ec5d) fix: ensure baseURL normalization is case-insensitive for SSR compatibility - [`b859ce3`](https://github.com/better-auth/better-auth/commit/b859ce3fdca30e39d10892c557f1014ab21134a2) chore: lint ### 📊 Changes **1 file changed** (+36 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/client/vue/index.ts` (+36 -2) </details> ### 📄 Description Following the discussion in [#4722](https://github.com/better-auth/better-auth/issues/4722), and after the fix that landed in [#4887](https://github.com/better-auth/better-auth/pull/4887), Nuxt `useSession(useFetch)` started working again **only** when using the default `/api/auth` base path. However, the fix did not cover cases where a **custom base path** is configured (e.g. `/api/v1/auth`). Like this : ```ts export const authClient = createAuthClient({ baseURL: "http://localhost:3000/api/v1/auth", }); ``` In these cases, the client still builds an absolute URL for `get-session` (like so `http://localhost:3000/api/v1/auth`), which in Nuxt SSR prevents cookies from being forwarded. The result is that `session` is always `null`, even though the endpoint responds correctly in the browser. ```ts const loggedFetch = ((url: string, opts?: any) => { console.log("[useSession] fetching:", url); return useFetch(url, opts); }) as any; const { data: session, error } = await authClient.useSession(loggedFetch); // return [useSession] fetching: http://localhost:3000/api/v1/auth/get-session // After this PR // return [useSession] fetching: /api/v1/auth/get-session ``` This PR ensures the client: * Normalizes the `baseURL` to extract only the pathname. * Defaults `/` to `/api/auth`. * Trims trailing slashes. * Always calls a **relative** endpoint for `…/get-session`, which allows Nuxt SSR to forward cookies properly. **For Nuxt SSR custom base url** * **Before #4887:** regression → always `null`. * **After #4887:** works only for default baseURL. * **With this PR:** works for both default and custom base paths, without needing app-side workarounds (like wrapping `useFetch` to strip the origin). <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes Nuxt SSR cookie forwarding when using a custom auth baseURL. We now normalize baseURL and call a relative /get-session path so useSession returns the session for both default and custom paths. - **Bug Fixes** - Normalize baseURL to a relative pathname; strip origin if absolute. - Ensure leading slash, trim trailing slashes, fallback to /api/auth. - Build /get-session from the normalized path to avoid absolute requests in SSR. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:30:22 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#5647