#Refresh the page after authClient.signOut()

6 messages · Page 1 of 1 (latest)

daring heathBOT
#

Hi @inland hemlock. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

It sounds like after signing out, your client-side state (such as the session data) is not being properly cleared or updated, so the UI still displays the old session information until a full page refresh occurs. This is a common issue in Nuxt and other SSR frameworks when session or authentication state is managed inconsistently between client and server.

Key points and solutions from the knowledge sources:

  1. Session State Persistence:
    If you store session or user data in a composable or state (e.g., via useState or a custom composable), you need to ensure that this state is cleared when the user signs out. Otherwise, the UI will continue to display the old session data until the state is reset or the page is reloaded.

  2. Using Cookies for Auth State:
    The recommended approach in Nuxt 3 is to use useCookie for storing authentication/session data, as cookies are accessible both on the client and server. This ensures that your session state is consistent across navigation and reloads. When you sign out, you should clear the relevant cookies, which will also clear the session state on both client and server. Example:

    const accessToken = useCookie("accessToken");
    accessToken.value = undefined; // deletes the cookie
    

    This approach works both server-side and client-side, and avoids issues with localStorage or in-memory state not syncing properly after logout or navigation Accessing tokens stored in localStorage from server-side.

#
  1. Clearing Session on Logout:
    If you use a composable like useUserSession, make sure to call its clear method (or equivalent) on logout, and then redirect the user. For example:

    async function logout() {
      await clearSession()
      await navigateTo('/login')
    }
    

    This ensures the session is cleared before redirecting Protect App Routes.

  2. Client-Side Middleware:
    Protect your routes using client-side middleware that checks the session state and redirects unauthenticated users. If your session state is not cleared on logout, the middleware will still see the old session and allow access. Make sure your middleware checks the correct, up-to-date session state Protect App Routes.

Summary:
After signing out, ensure you clear both the session cookie (using useCookie) and any in-memory or composable state holding the session data. Then redirect the user. This will prevent stale session data from being displayed and ensure your middleware correctly protects your routes.

If you are already clearing cookies but still see the old session, double-check that your composable or state is also reset on logout, and that your middleware is referencing the correct source of truth for authentication state.

If you need a concrete example, here’s a pattern from the Nuxt docs:

const { user, clear: clearSession } = useUserSession()

async function logout() {
  await clearSession()
  await navigateTo('/login')
}

Home Page

#

If you’re using a custom auth client, make sure to clear all relevant state and cookies after authClient.signOut() before redirecting.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@inland hemlock Kapa.ai is still learning and improving, please let me know how I did by reacting below.

inland hemlock
#

Extra Info:
I have a global middleware like:

import { authClient } from "../../lib/auth-client";

export default defineNuxtRouteMiddleware(async (to) => {
  const isUserNavigatingToProtected = to.path.startsWith("/protected");
  const { data: loggedIn } = await authClient.useSession(useFetch);
  const isNavigatingToLogin = to.path.startsWith("/login");
  if (isUserNavigatingToProtected && !loggedIn.value) {
    return navigateTo("/login");
  }
  if (isNavigatingToLogin && loggedIn.value) {
    return navigateTo("/");
  }
});

The login is being done like:

const handleSignIn = () => {
  authClient.signIn.social({
    provider: "google",
    callbackURL: "/protected",
  });
};