This is for social login providers. I am trying to set the user metadata and read it after setting it, in a single post login action file. However, I get an error.
Either the user metadatacannot set on Step 5 or the newly set user metadatacannot be read in Step 6 because I get an error
This is a fundamental issue and I have to solve it as quickly as possible.
Thank you so much!
Post Login Action - Code Flow (Please read)
Code flow is like this:
Check if the attempt is a social login.
→ event.connection.strategy
If true, check if there is a certain key/value pair in metadata by
→ event.user.user_metadata.hasuraUser.
If there is no metadata, it could be undefined, empty, null, etc. This means that I have not created this user also on my external database.
Hence, run a method that creates the user on external db (Hasura, in this case). Return the created user object. (This step works successfully. I am very grateful that Auth0 allows such an operation by the way.)
Using this user object, set user metadata as following:
→ api.user.setUserMetadata("hasuraUser", hasuraUser).
Now, get the newly setevent.user.user_metadata.hasuraUser attribute, then set custom claims for tokens.
→ _claim property just uses the id as event.user.user_metadata.hasuraUser.id. Then,
Thanks a lot. I didn’t know that using 2 action scripts, one after the first would solve the issue. I haven’t tried this yet. Currently, I solved it like this:
In step 5, I assign a key/value using the user data that returned as:
→ api.user.setUserMetadata("hasuraUser", hasuraUser)
nothing is changed in this step.
But in step 6 and after, instead of trying to use the newly set metadata by reading it (which is not possible):
→ const _user = event.user.user_metadata.hasuraUser
I use the user data from step 5 again
I am still curious why does event.user.user_metadatanot change even though api.user.setUserMetadata() method is called successfully?
I haven’t seen a doc saying that it is not possible in a single file and I struggled at this point too much while developing a Post Login action script.
I mean, everybody is working hard at Auth0. It is as clear as day and I appreciate it. Not complaining too much about it but when the work is 95% done and there is that one simple unknown situation made me question the whole logic
I’m glad to hear that you managed to find a workaround in your implementation.
This is a good point, and logically you should be able to get the user_metadata after being set on execution.
The reason this may happen is that some triggers are executed synchronously, blocking the flow in which they are involved, and some are executed asynchronously, as described in our Understand How Auth0 Actions Work docs.
Hoped this helps!
Please don’t hesitate to let me know if there’s anything else I can do to assist.