I need to store some info in the users app_metadata before their first login (The data is required for another login action). These details are determined by hitting an api with the user’s ID, data is then returned and my plan was to store it in the users app_metadata using the pre-user signup action.
However, It doesn’t look like the user ID is available in this action?
I can try using the post-user signup action but my concerns are;
a) Will the user have signed in before this is run not giving it enough time to update the user app metadata?
b) There is no api event on this action so not sure if updating the app_metadata is possible from here.
The user ID (remoteUser.id) in this scenario is coming from a remote DB. It is an external user ID that is being stored in auth0 in addition to the user ID that is created by Auth0.
Can you post the code (or some pseudocode) for your Action? I’d like to have a better understanding of what the rule does.
Ok so here is some very basic pseudocode for what I want to happen during the signup process.
function (event, api) => {
// get Auth0 user ID
// Post to external API, create an organisation with Auth0 user id as the owner ID
const organisation = await createOrgApi(auth0_user_id);
// Update users app_metadata
updateAppMetadata(organisation.id)
}
Once the user is signed up, I have another action that is part of the login flow, and will use the oprganisation ID on the users app_metadata.
Could you to handle this in a post-login action (in this type of action the user’s ID will be available and you can update the user’s metadata)? If you run it as the first action in the post-login pipeline, the data should be available for the subsequent actions.
This could work, I would just need to ensure that I add some checks before hitting my external API to ensure I do not create the new organisation with every login and to ensure that the login flow isn’t slowed down too much.
One strategy for this would be to check for an organization ID in the users app metadata before running the rest of the action. If the parameters exists you can skip the action.
Just thinking about this a little more, once the app_metadata is updated will the rest of the action have access to the updated metadata?
basic implementation of my login action
const login = (event, api) => {
const { user: { app_metadata } } = event;
// New user, fetch from external api and update app_metadata
if (!app_metdata.organisation) {
const data = await fetch("url")
api.user.setAppMetaData("organisation", { id: data.id })
}
// Existing user
if (app_metdata.organisation) {
// extra code to generate access token and set custom claim with org id
}
}
Will the existing user conditional statement have access to the newly updated app_metadata? If not I can just run the extra code I need in the first conditional so not a problem.