Auth0 Home Blog Docs

Cached user is not refreshed with custom database

custom-database
user-profile

#1

Hi awesome Auth0 and community,

I have a setup where I use custom database (and scripts) to authenticate and return a user profile.

In the Custom Database dashboard when i “try” my scripts, the user is returned with updated values.

When I search for the user in Auth0, these values (e.g. in app_metadata) are not updated. I am aware that Auth0 caches the user. But it states in the documentation that the user will be refreshed each time the user authenticates. (https://auth0.com/docs/user-profile/user-profile-details#caching-of-the-user-profile-in-auth0)

The only way I can force an update through is by deleting the user account - which then will be regenerated upon next authentication and the values are updates. I’d rather not use this approach since it’s will remove stats an etc.

Am I missing something?

Kind regards,
Emrah


#2

Bump.

I am still facing this issue - any news/suggestions?


#3

For future searches:
I managed to resolve my issue and get updated values by returning the custom fields I need as a part of a user_profile property in the normalized user object parameter of the callback function (of the login script in my custom database connection).

Example:

return callback(null, { 
    userId: userId, 
    // ... omitted
    user_profile: { 
        foo: "bar",
        custom: "field" 
    }});

Then I add the custom values I need as a part of the idToken in the form of a Rule.

function (user, context, callback) {
    var namespace = 'https://some.namespace.dk/';
    if (context.idToken) {
        context.idToken[namespace + 'foo'] = user.user_profile.foo;
        context.idToken[namespace + 'custom'] = user.user_profile.custom;
    }
  
   callback(null, user, context);
}

Now when the user gets updated and logs back in, the properties in the user_profile gets updated every time.

However I’d like to point out that this is not as clear as it could be in the docs - atleast for me.
Reading about user profiles and custom database scripts at https://auth0.com/docs/user-profile/user-profile-details#creating-users-in-a-custom-database makes it sound like the app_metadata property can be used to store user related values.
… and the fact I couldn’t find any articles about the user_profile field.
But now after figuring how to solve my problem and how to handle user profile claims in the OIDC way as described in https://auth0.com/docs/api-auth/tutorials/adoption/scope-custom-claims it all makes sense again.

It doesn’t really explain why app_metadata doesn’t update on subsequent logins - now it’s just a mystery.

Big shoutout to @jacob.wang who got me in the right direction! Thanks for the help and updating your issue with how you resolved it (User profile not updated when user login (custom connection OAuth2))!