I have a Nextjs client (using @auth0/nextjs-auth0 package). I have a post-login action that uses the provided email address to retrieve their full name from SalesForce and store it in user_metadata.
When I test this by logging in, I can see the user_metadata populating correctly, and the Nextjs front-end correctly displays their name (“Hello, [NAME]” at the top of the site).
But if the user is signing up for the first time, the Nextjs client says “Hello, undefined” until I log out/in after first signing up. I can see the user_metadata is populated correctly but it looks like the user_metadata isn’t available when Auth0 redirects back to the nextjs client.
It seems I can use the post-login action trigger to handle both logins and sign-ups as both of these events are triggering the action correctly and populating the user_metadata.
But why do I not have access to this user_metadata initially for sign-ups?
Here’s a snippet of code where I retrieve the User metadata. All the other Nextjs code is boilerplate
import Link from 'next/link';
import { useUser } from '@auth0/nextjs-auth0/client';
import { User } from '@/types/types';
const Profile = () => {
const { user, error, isLoading } = useUser();
const meta = user?.['NAMESPACE/meta'] as User;
if (isLoading) return <div>Loading...</div>;
if (error) return <div>{error.message}</div>;
if (!user) {
return (
<Link href="/api/auth/login">
Login
</Link>
);
}
return (
<div>
<Link href="#">{`Hello, ${meta.first_name}`}</Link> (<Link href="/api/auth/logout">log out</Link>)
</div>
);
};
export default Profile;
Just to confirm, are you setting user metadata inside the PostLogin trigger, right?
Basically, if that is the case, the cause is the fact that the user profile is already generated and the user metadata is set after that, thus your application receiving a profile without the user metadata.
If a user signs up, the metadata will not be available in the user profile but it will be visible inside the dashboard. However, the custom claims set will be accessible after a successful signup and might be more suitable for your use case.
An workaround for the issue that you are experiencing would be to perform a silent authentication once the user authenticates so that it returns the user profile containing the set metadata.