# Running an action at login that adds UUID to the user profile

**URL:** <https://community.auth0.com/t/running-an-action-at-login-that-adds-uuid-to-the-user-profile/129767>\
**Category:** Get Help\
**Tags:** login-prompt-new-experience, login-experience\
**Created:** [March 15, 2024, 3:47pm UTC](https://community.auth0.com/t/running-an-action-at-login-that-adds-uuid-to-the-user-profile/129767 "2024-03-15T15:47:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![moore.4708](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/moore.4708/32/55666_2.png) [@moore.4708](https://community.auth0.com/u/moore.4708)\
**Post date:** [March 15, 2024, 3:47pm UTC](https://community.auth0.com/t/running-an-action-at-login-that-adds-uuid-to-the-user-profile/129767/1 "2024-03-15T15:47:36Z")

</div>

I am trying to add a v4 UUID to each user at login. This after finding this is not possible at registration.  
My first stage was to get the value to populate. I ended with this:

* * *

//add-uuid-to-app-metadata Action  
exports.onExecutePostLogin = async (event, api) =\> {  
const { v4: uuidv4 } = require(‘uuid’);  
event.user.app\_metadata.uuid = event.user.app\_metadata.uuid || uuidv4();  
api.user.setAppMetadata(“uuid”, event.user.app\_metadata.uuid)  
};

* * *

This works, but of course it changes the uuid value in the metadata every time the same user logs in.  
I found some examples that used an “if exist” type logic to not change the uuid if already there.  
I have been working with the code below:

* * *

//add-uuid-to-app-metadata Action  
exports.onExecutePostLogin = async (event, api) =\> {  
const { v4: uuidv4 } = require(‘uuid’);  
const { uuid } = event.user.user\_metadata;  
// short-circuit if the user signed up already  
if ( uuid ) {  
return;  
}  
// first time login/signup  
api.user.setUserMetadata(“uuid”, uuidv4);  
return;  
};

* * *

My ‘if’ statement is not valid. I have tried variations and looked at other examples.  
Could someone with more experienced than I with the scripting see what I need to make this work?  
Any help is appreciated.

---

<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:** [March 15, 2024, 4:13pm UTC](https://community.auth0.com/t/running-an-action-at-login-that-adds-uuid-to-the-user-profile/129767/3 "2024-03-15T16:13:37Z")

</div>

Hi @moore.4708,

Welcome to the Auth0 Community!

Yes, I noticed that the if statement checks if the `uuid` object is true or false based on whether the user’s user\_metadata has any data.

What we should be doing instead is checking if the `event.user.user_metadata.uuid` exists and then assigning a `uuid` if there is not one.

I have attached a revised version, which you can use. It will set a new `uuid` if the user was not previously assigned with one.

```auto
//add-uuid-to-user-metadata Action
exports.onExecutePostLogin = async (event, api) => {
  const { v4: uuidv4 } = require(‘uuid’);
  event.user.user_metadata.uuid = event.user.user_metadata.uuid || uuidv4(); 
  api.user.setUserMetadata("uuid", event.user.user_metadata.uuid)
};

```

Let me know if you have any questions!

Cheers,  
Rueben

---

<div class="post-metadata">

**Author:** ![moore.4708](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/moore.4708/32/55666_2.png) [@moore.4708](https://community.auth0.com/u/moore.4708)\
**Post date:** [March 15, 2024, 7:57pm UTC](https://community.auth0.com/t/running-an-action-at-login-that-adds-uuid-to-the-user-profile/129767/4 "2024-03-15T19:57:20Z")

</div>

That worked first-try! Thanks, Rueben.

---

<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:** [March 19, 2024, 12:05am UTC](https://community.auth0.com/t/running-an-action-at-login-that-adds-uuid-to-the-user-profile/129767/5 "2024-03-19T00:05:24Z")

</div>

Hi @moore.4708,

I’m happy to hear that it worked on the first try!

Please feel free to reach out to us again if you have any additional questions.

Thanks,  
Rueben

---

<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:** [April 2, 2024, 12:06am UTC](https://community.auth0.com/t/running-an-action-at-login-that-adds-uuid-to-the-user-profile/129767/6 "2024-04-02T00:06:11Z")

</div>

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