# How to distinguish first login after sign up

**URL:** <https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [November 22, 2022, 4:14am UTC](https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885 "2022-11-22T04:14:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![supergrounded](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/supergrounded/32/37589_2.png) [@supergrounded](https://community.auth0.com/u/supergrounded)\
**Post date:** [November 22, 2022, 4:14am UTC](https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885/1 "2022-11-22T04:14:41Z")

</div>

I am trying to add a new flow for updating Stripe to create a new customer profile after a user has successfully signed up. After the customer profile has been created with Stripe, it will add the customer id to the app metadata of the user. I added the following flow to the onExecutePostLogin step:

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count === 1) {
    const customer = await stripe.customers.create({
      email: event.user.email,
    });
    api.user.setAppMetadata('stripe_customer_id', customer.id);
  }
};

```

However, whenever I tried to retrieve the metadata using the management api in my SPA Angular application, this block of code is being executed again. It seems like it’s not able to distinguish first time log in with token retrieve I believe? and it’s causing the same customer profile to be created once again in Stripe.

Is there any way to add more check to make sure that this block of code is only executing right after sign up, and not any other time?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [November 22, 2022, 4:49am UTC](https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885/2 "2022-11-22T04:49:31Z")

</div>

Hey there @supergrounded welcome to the community!

> [@supergrounded](#):
>
> However, whenever I tried to retrieve the metadata using the management api in my SPA Angular application, this block of code is being executed again.

That’s super curious it’s being executed again, as it should only be run when there is a login event and in this case only on the first event 🤔 Are you able to see 2 separate login events in your dashboard logs (monitoring → logs)?

Could you move this to a [pre user registration](https://auth0.com/docs/customize/actions/flows-and-triggers/pre-user-registration-flow?_gl=1*aim4qq*rollup_ga*NDcxOTM4ODQwLjE2NjYwMjk3NzE.*rollup_ga_F1G3E656YZ*MTY2OTA5MTIxMi42OC4xLjE2NjkwOTE4NjEuNjAuMC4w&_ga=2.77069462.229810830.1669091680-471938840.1666029771#set-metadata-in-the-user-profile) action instead? It might be a good use case for it.

---

<div class="post-metadata">

**Author:** ![supergrounded](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/supergrounded/32/37589_2.png) [@supergrounded](https://community.auth0.com/u/supergrounded)\
**Post date:** [November 22, 2022, 3:19pm UTC](https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885/3 "2022-11-22T15:19:24Z")

</div>

> Are you able to see 2 separate login events in your dashboard logs (monitoring → logs)?

There is only one login event when I check the log, but I think the code was executed again during the silent auth.

 ![Screen Shot 2022-11-22 at 10.17.33 AM](https://global.discourse-cdn.com/auth0/original/3X/d/3/d3a7490320821b0e375e427825fda47c752da6b5.png)

> Could you move this to a [pre user registration](https://auth0.com/docs/customize/actions/flows-and-triggers/pre-user-registration-flow?_gl=1*aim4qq*rollup_ga*NDcxOTM4ODQwLjE2NjYwMjk3NzE.*rollup_ga_F1G3E656YZ*MTY2OTA5MTIxMi42OC4xLjE2NjkwOTE4NjEuNjAuMC4w&_ga=2.77069462.229810830.1669091680-471938840.1666029771#set-metadata-in-the-user-profile) action instead? It might be a good use case for it.

For putting it in pre user registration, what if the user’s registration wasn’t successful and the email provided wasn’t valid? Then in this case the Stripe customer creation wouldn’t make sense in the pre user registration right?

---

<div class="post-metadata">

**Author:** ![gparascandolo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gparascandolo/32/38788_2.png) [@gparascandolo](https://community.auth0.com/u/gparascandolo)\
**Post date:** [November 22, 2022, 8:51pm UTC](https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885/4 "2022-11-22T20:51:41Z")

</div>

Post-Login Actions do occur for silent authentication requests.

A reliable solution would be to see if `stripe_customer_id` already exists in the user app\_metadata object, and skip the action for the user if it does.

However, if you need to rely on first logins only in the Post-Login Action flow instead, something like below might help

```auto
// check if user has more than one login, or if this is a refresh token rotation, or a silent auth request
const not_first_login = event.stats.logins_count > 1 ||
                        event.transaction?.protocol === 'oauth2-refresh-token' ||
                        event.request.query?.prompt === 'none';
if (not_first_login) {
  console.log(`Skipping event for user ${event.user.user_id}`);
  return;
}

```

This will skip the action for the user if the user’s login\_count is greater than one, or if the Post-Login Action was triggered by refresh token rotations & silent authentication requests.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![supergrounded](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/supergrounded/32/37589_2.png) [@supergrounded](https://community.auth0.com/u/supergrounded)\
**Post date:** [November 23, 2022, 12:58am UTC](https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885/5 "2022-11-23T00:58:04Z")

</div>

Checking if the `stripe_customer_id` is already set definitely helped! Thanks!

---

<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:** [December 7, 2022, 12:58am UTC](https://community.auth0.com/t/how-to-distinguish-first-login-after-sign-up/94885/6 "2022-12-07T00:58:06Z")

</div>

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