# Login/logout events with @auth0/auth0-react?

**URL:** <https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239>\
**Category:** Get Help\
**Tags:** auth0-react, event\
**Created:** [May 5, 2021, 9:05am UTC](https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239 "2021-05-05T09:05:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanpascal](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jeanpascal/32/27831_2.png) [@jeanpascal](https://community.auth0.com/u/jeanpascal)\
**Post date:** [May 5, 2021, 9:05am UTC](https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239/1 "2021-05-05T09:05:40Z")

</div>

Hello,  
I use `@auth0/auth0-react` and I would like to display a welcome message (like “Hello user.nickname”) after a login and eventually store `welcome=true` in `localStorage` to prevent same message again when reloading the page.  
But I can’t find the right event or hook to do something like that.  
I tried `onRedirectCallback` on the `Auth0Provider` component but I don’t have the context of the user yet.  
I tried `useEffect` hook on `user` value but I can’t remove `welcome=true` when the user logout.

Are there any events to do this? Like onLogin/onLogout or something like that ?

Thanks.

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 5, 2021, 1:44pm UTC](https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239/3 "2021-05-05T13:44:03Z")

</div>

Hi @jeanpascal,

Welcome to the Community!

You could add a custom claim to the user’s ID Token to inform your app if it is the first time they have logged in by using a rule:

```auto
function(user, context, callback) {
  const namespace = 'https://your-app.com/';
  context.idToken[namespace + 'firstLogin'] = (context.stats || {}).loginsCount == 0;
  callback(null, user, context);
}

```

And then in your app you could conditionally display the message if the value of `user['https://your-app.com/firstLogin']` is true.

> **[Sample Use Cases: Scopes and Claims](https://auth0.com/docs/get-started/apis/scopes/sample-use-cases-scopes-and-claims)**
>
> Learn how to use scopes and claims with applications and APIs.

---

<div class="post-metadata">

**Author:** ![jeanpascal](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jeanpascal/32/27831_2.png) [@jeanpascal](https://community.auth0.com/u/jeanpascal)\
**Post date:** [May 5, 2021, 1:50pm UTC](https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239/4 "2021-05-05T13:50:39Z")

</div>

Thank you for your answer !  
But it is not exactly what I would like to do.  
I would like to have a “Welcome user.nickname” on every successful login (on my webapp).  
I could easily do that with some `onSuccessLogin` event on the `Auth0Provider` component of the `@auth0/auth0-react` package (but I don’t find this type of event).

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 5, 2021, 2:33pm UTC](https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239/5 "2021-05-05T14:33:55Z")

</div>

Oh I see! Since the `onRedirectCallback` runs after login, you could add a query string param to the URL:

```auto
  onRedirectCallback = (appState) => {
    const redirectUri = `${appState?.returnTo || window.location.pathname}?welcome=true`;
    this.props.history.push(redirectUri);
  };

  render() {
    return (
      <Auth0Provider
        domain={this.domain}
        clientId={this.clientId}
        redirectUri={window.location.origin}
        onRedirectCallback={this.onRedirectCallback}
        audience={this.audience}
      >
        {this.props.children}
      </Auth0Provider>
    );
  }

```

And then in your component check for the param:

```auto
const showWelcome = (window.location.search || '').includes('welcome');

```

---

<div class="post-metadata">

**Author:** ![jeanpascal](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jeanpascal/32/27831_2.png) [@jeanpascal](https://community.auth0.com/u/jeanpascal)\
**Post date:** [May 5, 2021, 2:35pm UTC](https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239/6 "2021-05-05T14:35:46Z")

</div>

Thanks I will try that !

---

<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:** [May 20, 2021, 2:36pm UTC](https://community.auth0.com/t/login-logout-events-with-auth0-auth0-react/62239/7 "2021-05-20T14:36:07Z")

</div>

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