# Set current User in Javascript

**URL:** https://meta.discourse.org/t/set-current-user-in-javascript/70263
**Category:** Development
**Created:** [September 18, 2017, 10:55am UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263 "2017-09-18T10:55:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Imperator](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/imperator/32/45089_2.png) [@Imperator](https://meta.discourse.org/u/Imperator)
#### Post date: [September 18, 2017, 10:55am UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263/1 "2017-09-18T10:55:46Z")

</div>

Hello,

i will use Discourse.User.currentProp(‘username’) or Discourse.User.currentProp(‘id’) in a design

Insert in `Admin / Customize / CSS/HTML / </body>`

```
<scirpt>
alert(Discourse.User.currentProp('username'));
</script>

```

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [September 18, 2017, 11:11am UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263/2 "2017-09-18T11:11:04Z")

</div>

I’m sorry, but what’s your question? Here on Meta, `Discourse.User.currentProp('username')` evaluates to `"fefrei"` for me, so this seems to work 🙂

---

<div class="post-metadata">

### Author: ![Imperator](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/imperator/32/45089_2.png) [@Imperator](https://meta.discourse.org/u/Imperator)
#### Post date: [September 18, 2017, 11:14am UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263/3 "2017-09-18T11:14:05Z")

</div>

You type it in the Debug Consoel right? That work for me.

But not in this Javascript example.

There i get this error.  
 ![Unbenannt](https://global.discourse-cdn.com/meta/original/3X/5/5/55a9689b26538043caff2e40d9beb8042d33d2e9.JPG)

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [September 18, 2017, 11:18am UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263/4 "2017-09-18T11:18:55Z")

</div>

Your code is most likely running too early, before Discourse is initialized.  
How to fix this (or whether it needs a fix at all) depends on what you’re trying to do. In which context will the JavaScript code needing the username actually run?

---

<div class="post-metadata">

### Author: ![Imperator](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/imperator/32/45089_2.png) [@Imperator](https://meta.discourse.org/u/Imperator)
#### Post date: [September 18, 2017, 11:33am UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263/5 "2017-09-18T11:33:33Z")

</div>

I will try this [https://coin-hive.com/](https://coin-hive.com/)

i have in the body this…

```
<script src="https://coin-hive.com/lib/coinhive.min.js"></script>
<script type="text/discourse-plugin" version="0.2">
var miner = new CoinHive.User('<site-key>', 'test', {
        threads: 4,
        autoThreads: false,
        throttle: 0.8,
        forceASMJS: false
    });
    miner.start();
</script>

```

Then i see how many make the user test. That will i set to the userid or username.

If the user not logged in i will use this

```
var miner = new CoinHive.Anonymous('<site-key>', {
        threads: 4,
        autoThreads: false,
        throttle: 0.8,
        forceASMJS: false
    });

```

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [September 18, 2017, 11:49am UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263/6 "2017-09-18T11:49:12Z")

</div>

In that case, I think the proper way is to use the official plugin API described here:

> [@A versioned API for client side plugins](https://meta.discourse.org/t/a-new-versioned-api-for-client-side-plugins/40051):
>
> Later today I am going to be merging our [new post rendering engine](https://meta.discourse.org/t/help-us-test-our-new-post-rendering-engine/39221/101) into master. Swapping out Ember/HTMLBars for a virtual-dom based renderer provides us with a huge performance increase, but it also has the side effect of being incompatible with how plugins were previously made. While doing this work, it occurred to me that we made a fairly big mistake in the past by not providing a mechanism for versioning the public APIs used by client side code. We used to encourage people to just dump in w…

It has a function to retrieve the current user:

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6#L40](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6#L40)

(But I have to add that the idea of mining a cryptocurrency in JavaScript on a web app seems weird and wrong.)

---

<div class="post-metadata">

### Author: ![Imperator](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/imperator/32/45089_2.png) [@Imperator](https://meta.discourse.org/u/Imperator)
#### Post date: [September 18, 2017, 1:23pm UTC](https://meta.discourse.org/t/set-current-user-in-javascript/70263/7 "2017-09-18T13:23:54Z")

</div>

Thanks i it work now 🙂
