Auth0 triggers auth/profile endpoint every few seconds. Is this expected behavior?

When I look to see what initiates the call, its the use-user.js hook within the Auth0 code. This is the code:

"use client";
import useSWR from "swr";
export function useUser() {
    const { data, error, isLoading } = useSWR(process.env.NEXT_PUBLIC_PROFILE_ROUTE || "/auth/profile", (...args) => fetch(...args).then((res) => {
        if (!res.ok) {
            throw new Error("Unauthorized");
        }
        return res.json();
    }));
    // if we have the user loaded via the provider, return it
    if (data) {
        return {
            user: data,
            isLoading: false,
            error: null
        };
    }
    if (error) {
        return {
            user: null,
            isLoading: false,
            error
        };
    }
    return {
        user: data,
        isLoading,
        error
    };
}

When I’m logged in it’s not really a problem. But when I’m not, it clogs up my logs with a 404 error. Is this expected behavior? The only thing I can think of is I set up my middleware wrong, but I followed the quick start.

import { NextRequest } from "next/server"
import { auth0 } from "./lib/auth0"

const GUEST_SECRET = process.env.GUEST_SECRET!
const secret = new TextEncoder().encode(GUEST_SECRET)

export async function middleware(request: NextRequest) {
  const response = await auth0.middleware(request)

  return response
}

export const config = {
  matcher: [
    "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
  ],
}

Hi @mmaccou

I am sorry about the delayed response to your inquiry!

Basically, if an user is not authenticated in your NextJS application, it should not be throwing 404 errors continually.

Are you redirecting to a proper auth endpoint as mentioned in this community post? Also, are you using loginWithRedirect() or loginWithPopup()? You could try to download our sample app and build upon it or replace parts and see if the behaviour is being reproduced.

Simply, I believe there might be a slight misconfiguration inside your application where it is not using a proper endpoint. You could examine the sample application and compare it with your own personal configuration.

If you have any other questions, let me know!

Kind Regards,
Nik

Hi Nik,

I’m not using loginWithRedirect() or loginWithPopup(). I’m I have a button that takes users to http://localhost:3000/auth/login. That works fine. And I double checked my settings and confirmed localhost is included where possible:

It’s just the profile endpoint that keeps triggering which I don’t have anywhere in my code. I looked at the link you gave but I don’t see a mention of using a proper endpoint vs an improper one…am I missing something? I also dont see a sample app to clone. The link in your docs HERE is broken

Hi again!

I am sorry about the late reply.

Regarding the sample application available on github, you can access it here.

Otherwise, I am still investigating the matter and come back with an update as soon as possible!

Kind Regards,
Nik