Different Strategies to Update User Metadata in Post-Login Action

Problem statement

In Post-Login Actions it’s possible to save user metadata with the following methods:

api.user.setAppMetadata(name, value)
api.user.setUserMetadata(name, value)

However, it’s also possible to instantiate the ManagementClient in an Action to save user metadata. Are there any tradeoffs to choosing one method over the other?

This article addresses the following question:

  • Are there any tradeoffs to choosing one method over the other?

Solution

If actions 1, 2, and 3 all make changes to user_metadata or app_metadata, we collect all of those requested changes and persist them as a single transaction at the end. Calling ManagementClient will not make the changes visible in the subsequent Actions. This means the updated user metadata will not appear updated while the Actions are running for that login transaction.

However, between actions, we use a sort of ‘optimistic’ update to the event object that propagates through the pipeline. This can be implemented by calling the api.user.setAppMetadata(name, value) or api.user.setUserMetadata(name, value) method to make the update visible to subsequent Actions.