# Display username when login in banner

**URL:** https://meta.discourse.org/t/display-username-when-login-in-banner/83024
**Category:** Development
**Created:** [March 14, 2018, 10:57pm UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024 "2018-03-14T22:57:11Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [March 14, 2018, 10:57pm UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/1 "2018-03-14T22:57:11Z")

</div>

I would like to say “hey Username” in my banner, how can I do this? And not display a name when logout… Thanks!

 ![username](https://global.discourse-cdn.com/meta/original/3X/c/b/cb245ce311c4c39ccffd059c20879a7e636086ea.png)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [March 15, 2018, 12:09am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/2 "2018-03-15T00:09:04Z")

</div>

I think this would have to be done in client JavaScript, any ideas @dax?

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 15, 2018, 12:47am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/3 "2018-03-15T00:47:21Z")

</div>

I think it’s as simple as adding this to the \</ head\> tab of the theme:

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
    if(api.getCurrentUser() != null) {
        $("#logged-in-user").text(" " + api.getCurrentUser().username);
    }
</script>

```

And then adding a `<span>` to the \< header\> tab where `*USERNAME*` is:

```plaintext
<h2 class="x-title">Hey<span id="logged-in-user"></span>! Bienvenue dans ta nouvelle communauté</h2>

```

---

<div class="post-metadata">

### Author: ![Biscuit](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/biscuit/32/94482_2.png) [@Biscuit](https://meta.discourse.org/u/Biscuit)
#### Post date: [March 15, 2018, 1:17am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/4 "2018-03-15T01:17:49Z")

</div>

Thanks for sharing.

Now you’ve got me interested in checking out what other API calls are available!

If anyone else is interested: -

> [@Discourse REST API Documentation](https://meta.discourse.org/t/discourse-api-documentation/22706):
>
> Discourse API Please view the Discourse API Documentation site for detailed info: [https://docs.discourse.org](https://docs.discourse.org)information_source Authentication API requests must use HTTP header based authentication. Pass your Api-Key and Api-Username as HTTP headers. Authentication via query parameters or request body is not supported (this was removed in April 2020). Please see the example cURL request below. The only API endpoints that continue to support credentials in query parameters are requests to…

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [March 15, 2018, 2:17am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/5 "2018-03-15T02:17:23Z")

</div>

Thanks! But, I tried it and my name doesn’t display.  
I copied exactly what you wrote.

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 15, 2018, 5:05am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/6 "2018-03-15T05:05:26Z")

</div>

Is this where you are putting the code (see highlighted lines)?

 ![banner](https://global.discourse-cdn.com/meta/original/3X/f/8/f8c925123e0c16725edc2eebfdd397ea5db3f925.gif)

Also did you make sure you saved the changes? Refreshed your browser after you saved?

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [March 15, 2018, 11:42am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/7 "2018-03-15T11:42:13Z")

</div>

When I put the html in the header like you do, I see my name. But I don’t want the banner above the header. I want it after like this tutorial [https://meta.discourse.org/t/banner-themes-and-instructions-for-customizing-them/82368](https://meta.discourse.org/t/banner-themes-and-instructions-for-customizing-them/82368) . When I put the code in the “Afther header” I don’t see my name.

And is it possible to see only the first name?

Thanks

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 15, 2018, 6:40pm UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/8 "2018-03-15T18:40:45Z")

</div>

Sure, no problem. You can put the HTML code with the `<span>` in the After Header now, and the code below in the `</head>`. It displays the first name only. To show their full name, you would get rid of the `.split(' ')[0]`.

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
    api.onPageChange(() => {
        if(api.getCurrentUser() != null) {
            $("#logged-in-user").text(" " + api.getCurrentUser().name.split(' ')[0]);
        }
    });
</script>

```

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [March 15, 2018, 8:59pm UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/9 "2018-03-15T20:59:03Z")

</div>

This is what I got with `After header`. I don’t see my name.

 ![after%20header](https://global.discourse-cdn.com/meta/original/3X/f/e/fed630b803c97b3cd32a749d0c547dcd94d6b0f6.png)

This is what I got with `header` . I see my full name but it is not where I want to place this section.

 ![header](https://global.discourse-cdn.com/meta/original/3X/7/9/793bbd8cd947215212f081581d6fbde2969e685d.png)

I put this code :

```
<script type="text/discourse-plugin" version="0.8.18">
if(api.getCurrentUser() != null) {
    $("#logged-in-user").text(" " + api.getCurrentUser().username);
}</script>

```

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [March 15, 2018, 9:15pm UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/10 "2018-03-15T21:15:59Z")

</div>

It looks like you copied the wrong code. This is what you want to add to `</head>` tab:

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
    api.onPageChange(() => {
        if(api.getCurrentUser() != null) {
            $("#logged-in-user").text(" " + api.getCurrentUser().name.split(' ')[0]);
        }
    });
</script>

```

And place this in the After Header tab where appropriate:

```plaintext
<h2 class="x-title">Hey<span id="logged-in-user"></span>! Bienvenue dans ta nouvelle communauté</h2>

```

---

<div class="post-metadata">

### Author: ![Francois\_Douville](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/francois_douville/32/94221_2.png) [@Francois\_Douville](https://meta.discourse.org/u/Francois_Douville)
#### Post date: [March 15, 2018, 9:43pm UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/11 "2018-03-15T21:43:16Z")

</div>

Thanks so much! I didn’t see you modify the code!

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [March 16, 2018, 9:31am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/12 "2018-03-16T09:31:34Z")

</div>

Thanks for this, love it!

Have slightly modified the script to include the ‘Hey!’ in the script, thereby getting around the issue of showing the ‘Hey!’ if name is blank:

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
    api.onPageChange(() => {
        if(api.getCurrentUser() != null) {
            if(api.getCurrentUser().name.split(' ')[0] != "") {
                $("#logged-in-user").text("Hey " + api.getCurrentUser().name.split(' ')[0]+"!");
            }
        }
    });
</script>

```

I’ve used this shorter greeting in a more discrete spot at the side of my site banners, just above the avatar.

---

<div class="post-metadata">

### Author: ![Osama](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/osama/32/98013_2.png) [@Osama](https://meta.discourse.org/u/Osama)
#### Post date: [March 16, 2018, 10:47am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/13 "2018-03-16T10:47:53Z")

</div>

Just a quick heads up: did you verify that this piece of code doesn’t raise any errors for non-logged in users? A JS error in a theme could make your site completely unusable.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [March 16, 2018, 10:49am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/14 "2018-03-16T10:49:27Z")

</div>

You are right … thanks for the warning … was getting this on console when logged out:

```plaintext
null is not an object (evaluating 'api.getCurrentUser().name')

```

Fixed my code above, clearing the error when logged out. Apologies, believe the original script worked fine.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [May 6, 2018, 8:19am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/15 "2018-05-06T08:19:03Z")

</div>

Improved this slightly, to use the Username in single quotes if the Name is not available:

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
    api.onPageChange(() => {
        if(api.getCurrentUser() != null) {
            if(api.getCurrentUser().name != "") {
                if(api.getCurrentUser().name.split(' ')[0] != "") {
                    $("#logged-in-user").text("Hey " + api.getCurrentUser().name.split(' ')[0]+"!");
                }
            }
            else
            {
                $("#logged-in-user").text("Hey '" + api.getCurrentUser().username + "'!");
            }
        }
    });
</script>

```

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [November 5, 2023, 8:00am UTC](https://meta.discourse.org/t/display-username-when-login-in-banner/83024/18 "2023-11-05T08:00:25Z")

</div>


