Skip to content

v4.1: Overridden user details in Auth0Provider are ignored when retrieving user details with useUser() #1993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
6 tasks done
payamtrack opened this issue Mar 24, 2025 · 5 comments
Labels

Comments

@payamtrack
Copy link

payamtrack commented Mar 24, 2025

Checklist

Description

Hi,
In v3, we could easily override user details by updating the user prop within <UserProvider />. For example:

import { UserProvider } from '@auth0/nextjs-auth0/client'
import { headers } from 'next/headers'

export default async function RootLayout({
    children,
}: {
    children: React.ReactNode
}) {
    const headersList = headers()
    const sub = headersList.get('user-id') ?? ''
    const email = headersList.get('user-email') ?? ''
    const name = headersList.get('user-name') ?? ''

    return (
        <>
            <UserProvider user={{ sub, email, name }}>
                {children}
            </UserProvider>
        </>
    )
}

Then in client components, we could then access the overridden user details via useUser().

However, in v4, even though we set the user={{ sub, email, name }} prop in <Auth0Provider />, useUser() ignores these overridden details and instead returns the default session.user details.

Reproduction

  1. In the root layout, set the user prop on Auth0Provider with test data:
<Auth0Provider user={{ sub: "test_id", email: "[email protected]", name: "test name" }}>
  {children}
</Auth0Provider>
  1. In a client component, retrieve user details using useUser():
'use client'

import { useUser } from '@auth0/nextjs-auth0'

export default function ClientComponent() {
   const { user } = useUser()

    console.log('User details:', user)
    
    return (
      <>{user?.sub} - {user?.email} - {user?.name}</>
    )
}

Additional context

No response

nextjs-auth0 version

4.1.0

Next.js version

14.2.25

Node.js version

18.19.0

@payamtrack payamtrack changed the title Overridden user details in Auth0Provider are ignored when retrieving user details with useUser() Version v4.1: Overridden user details in Auth0Provider are ignored when retrieving user details with useUser() Mar 24, 2025
@payamtrack payamtrack changed the title Version v4.1: Overridden user details in Auth0Provider are ignored when retrieving user details with useUser() v4.1: Overridden user details in Auth0Provider are ignored when retrieving user details with useUser() Mar 24, 2025
@frederikprijck
Copy link
Member

Can you elaborate on the use-case for this? Why would you not be able to rely on the actual values, rather than override them?

@tusharpandey13
Copy link
Contributor

tusharpandey13 commented Apr 4, 2025

Please see https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#passing-an-initial-user-from-the-server

The loaded user will then be used as a fallback in useUser() hook.

V4 does not override, it provides a fallback. Is this similar to what you are facing?

@payamtrack
Copy link
Author

That's correct, I was expecting it to behave like v3 and override the user, but I wasn't aware of the different behaviour in v4.
Thanks for pointing that out, @tusharpandey13!

@payamtrack
Copy link
Author

@frederikprijck Apologies for the delayed response. We need to use our internal user ID instead of the Auth0 one, and then retrieve the rest of the user data from Auth0.

@frederikprijck
Copy link
Member

We need to use our internal user ID instead of the Auth0 one, and then retrieve the rest of the user data from Auth0.

This is something to be solved in an action to enrich the token with whatever additional claim you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants