# Users are being redirected to login randomly a few times a day

**URL:** <https://community.auth0.com/t/users-are-being-redirected-to-login-randomly-a-few-times-a-day/191364>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [September 1, 2025, 11:35pm UTC](https://community.auth0.com/t/users-are-being-redirected-to-login-randomly-a-few-times-a-day/191364 "2025-09-01T23:35:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaferraram](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/isaferraram/32/63479_2.png) [@isaferraram](https://community.auth0.com/u/isaferraram)\
**Post date:** [September 1, 2025, 11:35pm UTC](https://community.auth0.com/t/users-are-being-redirected-to-login-randomly-a-few-times-a-day/191364/1 "2025-09-01T23:35:32Z")

</div>

**Description:**  
Logged-in users are redirected back to the login flow randomly several times per day. It happens even when their session should still be valid. Our app must attach a bearer token in the `Authorization` header for every API request, so we suspect our 401 handling or refresh flow may be contributing.

**Environment:**

- Next.js (App Router) with `@auth0/nextjs-auth0` (edge)

- Universal Login

- Organizations enabled

**Current approach (summary):**

- Refresh tokens via `GET /api/auth/refresh-session` using `getAccessToken(req, res, { refresh: true })`, then mirror to a cookie named `access_token`.

- Browser Axios client:

**Questions:**

1. Is there a recommended best practice for apps that must always attach `Authorization: Bearer <token>` to every API request?

2. Should we handle this differently than our current approach (refresh endpoint + header injection + redirect to login on 401)?

3. Are there known intermittent cases where `getAccessToken` can return 401 even when the session cookie appears valid (edge runtime and organizations enabled)?

**Relevant code (redacted):**

```auto
// app/api/auth/refresh-session/route.ts
import { getAccessToken } from "@auth0/nextjs-auth0/edge";
import { NextRequest, NextResponse } from "next/server";
import { jwtDecode } from "jwt-decode";
import { cookies } from "next/headers";
import { handleReqError } from "../../../../utils/errorHandler";

import { getAccessToken } from "@auth0/nextjs-auth0/edge";
import { NextRequest, NextResponse } from "next/server";
import { jwtDecode } from "jwt-decode";
import { cookies } from "next/headers";
import { handleReqError } from "../../../../utils/errorHandler";

export async function GET(req: NextRequest) {
  try {
    const cookieStore = await cookies();
    const existingToken = cookieStore.get("access_token")?.value;
    const res = new NextResponse();

    if (!existingToken) {
      // No cached token, get fresh one
      try {
        const { accessToken } = await getAccessToken(req, res, {
          refresh: true,
        });

        res.cookies.set("access_token", accessToken, {
          httpOnly: true,
          secure: true,
          sameSite: "lax",
          path: "/",
          maxAge: 60 * 60 * 24,
        });

        const updatedDecoded = jwtDecode(accessToken);
        return NextResponse.json(
          { token: accessToken, exp: updatedDecoded.exp },
          res
        );
      } catch (error) {
        return handleReqError(error, "Failed to get fresh token", req);
      }
    }

    const decoded: any = jwtDecode(existingToken);
    const now = Math.floor(Date.now() / 1000);

    const is_Expired = decoded.exp && decoded.exp < now;
    const hasMissingClaims =
      !decoded[`${process.env.BASE_API_URL}/claims/user_id`];

    if (is_Expired || hasMissingClaims) {
      try {
        const { accessToken } = await getAccessToken(req, res, {
          refresh: true,
        });

        if (!accessToken) {
          return handleReqError(
            { response: { status: 401 } },
            "No token received from Auth0",
            req
          );
        }

        res.cookies.set("access_token", accessToken, {
          httpOnly: true,
          secure: true,
          sameSite: "lax",
          path: "/",
          maxAge: 60 * 60 * 24,
        });

        const updatedDecoded = jwtDecode(accessToken);
        return NextResponse.json(
          {
            token: accessToken,
            exp: updatedDecoded.exp,
            isFirstLogin: hasMissingClaims,
          },
          res
        );
      } catch (error) {
        return handleReqError(error, "Failed to refresh token", req);
      }
    }

    // token is still valid and has the required claim
    return NextResponse.json({ ok: true, isFirstLogin: hasMissingClaims }, res);
  } catch (error) {
    return handleReqError(error, "Token validation failed", req);
  }
}

```

```auto
// browser Axios client (simplified)
// - Proactive refresh via /api/auth/refresh-session
// - Injects Authorization header
// - On 401 from refresh, clears cache and redirects to login
import axios from "axios";

let cachedToken: string | null = null;
let tokenExpiry = 0;
let refreshPromise: Promise<void> | null = null;

async function refreshIfNeeded() {
  const now = Math.floor(Date.now() / 1000);
  const needs = !cachedToken || !tokenExpiry || now >= tokenExpiry - 60;
  if (!needs) return;

  if (!refreshPromise) {
    refreshPromise = fetch("/api/auth/refresh-session", { credentials: "include" })
      .then(async (res) => {
        if (!res.ok) throw new Error(`Refresh failed: ${res.status}`);
        const { token, exp } = await res.json();
        cachedToken = token ?? null;
        tokenExpiry = exp ?? now + 3600;
      })
      .catch((e) => {
        cachedToken = null;
        tokenExpiry = 0;
        throw e;
      })
      .finally(() => { refreshPromise = null; });
  }
  await refreshPromise;
}

export function createBrowserAuthClient(baseURL = "/api") {
  const client = axios.create({ baseURL, withCredentials: true });

  client.interceptors.request.use(async (config) => {
    await refreshIfNeeded();
    if (cachedToken) {
     config.headers = config.headers ?? {};
     config.headers.Authorization = `Bearer ${cachedToken}`;
    }
    return config;
  });

  client.interceptors.response.use(
    (r) => r,
    (err) => {
      if (err?.response?.status === 401) {
        cachedToken = null;
        tokenExpiry = 0;
        // current behavior: send user to login
        window.location.replace("/api/auth/login");
      }
      return Promise.reject(err);
    }
  );

  return client;
}

```

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [September 5, 2025, 9:06pm UTC](https://community.auth0.com/t/users-are-being-redirected-to-login-randomly-a-few-times-a-day/191364/3 "2025-09-05T21:06:28Z")

</div>

Hi @isaferraram

Welcome to the Auth0 Community!

I am sorry about the delayed response to your inquiry!

From what I understand in the code that you have posted, it appears that you are refreshing tokens via the `/auth/access-token` as you have mentioned and demonstrated above.  
To my knowledge, the NextJS SKD v4.9 currently has a dedicated route for checking the user’s session and refreshing it with a refresh token which is /`auth/access-token`

> 1. `/auth/access-token`: the route to check the user’s session and return an access token (which will be automatically refreshed if a refresh token is available)

Also mentioned in the [documentation](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#getting-an-access-token), when using `getAccessToken()` the session should be automatically refreshed and persisted if an refresh token is available.

Could you please let me know what version are you exactly using at this time for your application?

Kind Regards,  
Nik

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [September 19, 2025, 9:06pm UTC](https://community.auth0.com/t/users-are-being-redirected-to-login-randomly-a-few-times-a-day/191364/4 "2025-09-19T21:06:44Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
