# How to check whether the user exists while his logged in?

**URL:** <https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [September 8, 2023, 9:34am UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749 "2023-09-08T09:34:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtester](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@mtester](https://community.auth0.com/u/mtester)\
**Post date:** [September 8, 2023, 9:34am UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/1 "2023-09-08T09:34:27Z")

</div>

We use a third party website for comments into which we send the token.\_\_raw from auth0. But there is a problem if user deletes his account from auth0, while he’s logged in and then opens the comments section. In this case we still send correct token to our comments provider. Is there a way to check if the user actually still exists (not just in cache) in frontend Auth0 Spa js?

---

<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:** [September 8, 2023, 11:24pm UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/3 "2023-09-08T23:24:36Z")

</div>

Hi @mtester,

Welcome to the Auth0 Community!

You can check whether the user exists by calling the Management API’s [Get a user endpoint](https://auth0.com/docs/api/management/v2/users/get-users-by-id). You can find the `user_id` in the `sub` claim of the access token you store.

Let me also share our [Revoke Tokens](https://auth0.com/docs/secure/tokens/revoke-tokens) documentation for your reference.

In the doc, it states that once an access token is issued, it cannot be revoked. Therefore, you should try to have tokens be as short-lived as possible and refresh the user periodically if the user stays active.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mtester](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@mtester](https://community.auth0.com/u/mtester)\
**Post date:** [September 11, 2023, 8:10am UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/4 "2023-09-11T08:10:19Z")

</div>

But that has to be done on the backend then, as its not public and needs some authentication. I saw there is also the ManagementClient, but that one also needs some token and its suggested to not to use it on frontend.

---

<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:** [September 11, 2023, 5:55pm UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/5 "2023-09-11T17:55:01Z")

</div>

Hi @mtester,

Thanks for following up.

With the auth0-spa-js SDK, there is actually a method called [`isAuthenticated`](https://auth0.github.io/auth0-spa-js/classes/Auth0Client.html#isAuthenticated) that you could use to determine if the user is logged in.

Could you please give it a try and let me know how it goes?

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mtester](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@mtester](https://community.auth0.com/u/mtester)\
**Post date:** [September 11, 2023, 7:04pm UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/6 "2023-09-11T19:04:31Z")

</div>

That will do nothing, as the user is already logged in when we need to check if it still exists.

---

<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:** [September 12, 2023, 4:24pm UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/7 "2023-09-12T16:24:05Z")

</div>

Hi @mtester,

If the user exists and is logged in, the `isAuthenticated` method will return **True**.

If the user is removed while logged in, the `isAuthenticated` method will return **False**.

In this way, you can determine if the user exists or not while logged in.

You can test this by yourself by creating a new user, logging them in, and then deleting them. You will see that their session is inactive.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mtester](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@mtester](https://community.auth0.com/u/mtester)\
**Post date:** [September 12, 2023, 6:24pm UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/8 "2023-09-12T18:24:10Z")

</div>

No it doesn’t. I just tested it. And it only works with the cached info as per its code here:

```auto
public async isAuthenticated() {
    const user = await this.getUser();
    return !!user;
  }

public async getUser<TUser extends User>(): Promise<TUser | undefined> {
    const cache = await this._getIdTokenFromCache();

    return cache?.decodedToken?.user as TUser;
  }

```

As you can see it only calls the getUser function which only decodes the user part of the cached token.

---

<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:** [September 13, 2023, 7:02pm UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/9 "2023-09-13T19:02:11Z")

</div>

Hi @mtester,

Thank you for your reply and for testing this on your end.

I have just collaborated with my colleagues on this one, and it looks like the best course of action would be to make your tokens as short-lived as possible.

This is because you are sending the token to your comments provider, which will grant the user access for the lifetime of the token.

Once you have made the access token as short-lived as possible, it will force the user to re-authenticate to get a fresh token for usage. For this, you could either implement silent authentication or refresh token rotation.

Here are some helpful resources for your reference:

- [Token Best Practices](https://auth0.com/docs/secure/tokens/token-best-practices)
- [Configure Silent Authentication](https://auth0.com/docs/authenticate/login/configure-silent-authentication)
- [Refresh Token Rotation](https://auth0.com/docs/secure/tokens/refresh-tokens/refresh-token-rotation)

Please let me know if you have any 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:** [October 9, 2023, 10:56pm UTC](https://community.auth0.com/t/how-to-check-whether-the-user-exists-while-his-logged-in/115749/10 "2023-10-09T22:56:02Z")

</div>

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