# Get current user info in Jupyterlab extension

**URL:** <https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115>\
**Category:** The Littlest JupyterHub\
**Tags:** jupyterlab\
**Created:** [February 22, 2022, 12:11am UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115 "2022-02-22T00:11:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aadil](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aadil](https://discourse.jupyter.org/u/aadil)\
**Post date:** [February 22, 2022, 12:11am UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/1 "2022-02-22T00:11:44Z")

</div>

Hi,

I am wondering is it possible to get the current logged in user info in Jupyterlab extension by making a call to Jupyterhub API, the only problem I am facing is that the tokens are assigned to the users. In the extension, I need to check if the user is an admin or not and then show the menu items accordingly?

Thanks.

---

<div class="post-metadata">

**Author:** ![manics](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/manics/32/85_2.png) [@manics](https://discourse.jupyter.org/u/manics)\
**Post date:** [February 22, 2022, 11:06am UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/2 "2022-02-22T11:06:33Z")

</div>

The JupyterHub `/user` API endpoint should include the user’s scopes. Based on the scopes you can decide what menu items to show.  
[https://jupyterhub.readthedocs.io/en/stable/reference/rest-api.html#/default/get\_user](https://jupyterhub.readthedocs.io/en/stable/reference/rest-api.html#/default/get_user)

---

<div class="post-metadata">

**Author:** ![aadil](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aadil](https://discourse.jupyter.org/u/aadil)\
**Post date:** [February 22, 2022, 10:34pm UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/3 "2022-02-22T22:34:02Z")

</div>

You are right, but the /user API endpoint expects a token for that specific user and returns the model based on the user token so the question is how do I get a user-specific token in the jupyterlab extension.

---

<div class="post-metadata">

**Author:** ![aadil](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aadil](https://discourse.jupyter.org/u/aadil)\
**Post date:** [February 23, 2022, 2:33am UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/4 "2022-02-23T02:33:26Z")

</div>

I figured out how to get the user-specific token in the extension by using PageConfig.getToken() function to get the token but this function returns an empty string instead of returning the token for the user.

---

<div class="post-metadata">

**Author:** ![manics](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/manics/32/85_2.png) [@manics](https://discourse.jupyter.org/u/manics)\
**Post date:** [February 23, 2022, 10:13pm UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/5 "2022-02-23T22:13:00Z")

</div>

There’s already an API token on the server side. For example, if you start a JupyterLab terminal:

```auto
$ curl -H "Authorization: token $JUPYTERHUB_API_TOKEN" $JUPYTERHUB_API_URL/user

{"groups": [], "last_activity": "2022-02-23T21:49:51.553599Z", "kind": "user", "name": "asd", "admin": false, "session_id":null, "scopes": ["access:servers!user=asd", "read:users:activity!user=asd", "users:activity!user=asd"]}

```

Note you shouldn’t rely on the `admin` property with JupyterHub 2.0 since permissions are now much more fine grained- use the `scopes` field instead.

`PageConfig.getToken()` will be empty as there was a bug: [Forward-port fixes from 1.5.0 security release by minrk · Pull Request #3679 · jupyterhub/jupyterhub · GitHub](https://github.com/jupyterhub/jupyterhub/pull/3679/commits/857ee2885fd1ec2eb121fa198e4fd4ed13b05cd4)

---

<div class="post-metadata">

**Author:** ![aadil](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aadil](https://discourse.jupyter.org/u/aadil)\
**Post date:** [February 23, 2022, 11:29pm UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/6 "2022-02-23T23:29:47Z")

</div>

Yes I can access the API token in Jupyterlab terminal but how do I access the same Token in my front end extension, is there any way to do this?

---

<div class="post-metadata">

**Author:** ![minrk](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/minrk/32/13_2.png) [@minrk](https://discourse.jupyter.org/u/minrk)\
**Post date:** [March 1, 2022, 2:56pm UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/7 "2022-03-01T14:56:42Z")

</div>

Now that jupyterlab-server has `page_config_hook`, `PageConfig.getToken()` will be able start returning the _user_’s token soon: [Add user token to JupyterLab PageConfig by minrk · Pull Request #3809 · jupyterhub/jupyterhub · GitHub](https://github.com/jupyterhub/jupyterhub/pull/3809) .

You could probably patch-in a similar page\_config\_hook via `jupyter_server_config` to work with current JupyterHub (note: you’d need to use the `get_secure_cookie` bit to make sure you still get the token for cookie-authenticated requests).

Something like:

```python
# /etc/jupyter/jupyter_server_config.py
def page_config_hook(handler, page_config):
    hub_auth = handler.settings["hub_auth"]
    token = hub_auth.get_token(handler)
    if not token:
        token = handler.get_secure_cookie(self.cookie_name)
        if token:
            token = token.decode("ascii", "replace")
    page_config["token"] = token
    return page_config

c.ServerApp.tornado_settings.update({"page_config_hook": page_config_hook})

```

---

<div class="post-metadata">

**Author:** ![aadil](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aadil](https://discourse.jupyter.org/u/aadil)\
**Post date:** [March 4, 2022, 5:08am UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/8 "2022-03-04T05:08:34Z")

</div>

![image](https://canada1.discourse-cdn.com/flex031/uploads/jupyter/original/2X/7/7c22403ebcd5b92acf9c3e12957812c51dbf1a3f.png)

Hi @minrk, sorry I am new to JupyterHub configuration and here is what my configuration file looks like for tljh but still I am unable to get token in my Jupyterlab extension.

---

<div class="post-metadata">

**Author:** ![minrk](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/minrk/32/13_2.png) [@minrk](https://discourse.jupyter.org/u/minrk)\
**Post date:** [March 4, 2022, 9:15am UTC](https://discourse.jupyter.org/t/get-current-user-info-in-jupyterlab-extension/13115/9 "2022-03-04T09:15:52Z")

</div>

This needs to be configuration for the single-user server (a different config file), not JupyterHub. You can put it in e.g. `/etc/jupyter/jupyter_server_config.py`
