akrantz01
akrantz01

Reputation: 648

Access app_metadata from Auth0 Lock

I am trying to access the app_metadata from Auth0 and I am using the Lock client. I have found the custom claims and created the rule that they show, but now I have no idea how actually use that data. Here is the function that handles all my authentication:

setSession(authResult) {
    if (authResult && authResult.accessToken && authResult.idToken) {
        this.lock.getUserInfo(authResult.accessToken, function(err, profile) {
            if (err) {
                return;
            }

            localStorage.setItem("access_token", authResult.accessToken);
            localStorage.setItem("id_token", authResult.idToken);
            localStorage.setItem("expires_at", JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime()));
            localStorage.setItem("profile", JSON.stringify(profile));

            history.replace("/home");
        });
    }
}

I would expect to just be able to get the app_metadata from the profile after including that, but this does not appear to be the case. Am I doing anything wrong or am I forgetting something?

EDIT 1: Here is the rule that I have currently.

function (user, context, callback) {
   const namespace = "https://alexkrantz.com";
   context.accessToken[namespace + "user_metadata"] = user.user_metadata;
   context.accessToken[namespace + "app_metadata"] = user.app_metadata;
   callback(null, user, context);
}

Upvotes: 1

Views: 271

Answers (1)

zergcore
zergcore

Reputation: 49

you can make a request and get all the user data from the management API and there you will find the user_metadata and the app_metadata, this way

const res = await fetch(`${process.env.AUTH0_ISSUER}/api/v2/users/${user.id}`,{
          method:'GET',
          headers: {authorization: 'Bearer management_api_token'},
        })

here you will find more in documentation https://auth0.com/docs/manage-users/user-accounts/manage-users-using-the-management-api https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id

Upvotes: 0

Related Questions