# Set User Metadata Is Not Working Inside Post Login Action

**URL:** <https://community.auth0.com/t/set-user-metadata-is-not-working-inside-post-login-action/72744>\
**Category:** Get Help\
**Created:** [November 8, 2021, 10:24am UTC](https://community.auth0.com/t/set-user-metadata-is-not-working-inside-post-login-action/72744 "2021-11-08T10:24:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yasin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/yasin/32/16961_2.png) [@yasin](https://community.auth0.com/u/yasin)\
**Post date:** [November 8, 2021, 10:24am UTC](https://community.auth0.com/t/set-user-metadata-is-not-working-inside-post-login-action/72744/1 "2021-11-08T10:24:51Z")

</div>

Hi, this is Yasin.

## Problem

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 metadata` **cannot set** on Step 5 or the newly set `user metadata` **cannot 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:

1. Check if the attempt is a **social** login.  
 → `event.connection.strategy`

2. If true, check if there is a certain key/value pair in metadata by  
 → `event.user.user_metadata.hasuraUser`.

3. 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.

4. 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.)_

5. Using this user object, set user metadata as following:  
 → `api.user.setUserMetadata("hasuraUser", hasuraUser)`.

6. Now, **get the newly set** `event.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,

- → `api.accessToken.setCustomClaim(_namespace, _claim);`  
 → `api.idToken.setCustomClaim(_namespace, _claim);`

1. Finish Post Login action.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [November 8, 2021, 7:34pm UTC](https://community.auth0.com/t/set-user-metadata-is-not-working-inside-post-login-action/72744/3 "2021-11-08T19:34:34Z")

</div>

Hi @yasin,

Thanks for reaching out to the Auth0 Community!

I understand that you’ve been encountering issues trying to make the Post Login Action script work with a Google Social Login connection.

First, I noticed that you are trying to accomplish your logic in steps 1-6 using a **one** Post Login Action script.

This will not work as you have observed because the user\_metadata does not show the newly added attributes.

To make this work, you’ll need to use 2 Post Login Action scripts.

In your first script, I recommend that you implement the logic from steps 1 - 5.

And then, in your second script, I recommend implementing the logic from step 6.

Once that is done, you can set the user metadata to your social login users and append the custom claims to your Access and ID tokens.

Please let me know how there’s anything else I can do to help.

Thank you.

---

<div class="post-metadata">

**Author:** ![yasin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/yasin/32/16961_2.png) [@yasin](https://community.auth0.com/u/yasin)\
**Post date:** [November 9, 2021, 5:26pm UTC](https://community.auth0.com/t/set-user-metadata-is-not-working-inside-post-login-action/72744/4 "2021-11-09T17:26:38Z")

</div>

Hi @rueben.tiow,

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_metadata` **not 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 😃

Anyways, thanks a lot for caring.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [November 15, 2021, 9:09pm UTC](https://community.auth0.com/t/set-user-metadata-is-not-working-inside-post-login-action/72744/5 "2021-11-15T21:09:10Z")

</div>

Hi @yasin,

Thank you for your response.

I’m glad to hear that you managed to find a workaround in your implementation.

> [@yasin](#):
>
> I am still curious why does `event.user.user_metadata` **not change** even though `api.user.setUserMetadata()` method is called successfully?

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](https://auth0.com/docs/actions/actions-overview) docs.

Hoped this helps!

Please don’t hesitate to let me know if there’s anything else I can do to assist.

Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 30, 2021, 9:09pm UTC](https://community.auth0.com/t/set-user-metadata-is-not-working-inside-post-login-action/72744/6 "2021-11-30T21:09:26Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
