# How to find out if user logged in

**URL:** <https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099>\
**Category:** Product Support\
**Created:** [October 3, 2020, 11:08am UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099 "2020-10-03T11:08:12Z")\
**Posts on this page:** 7\
**Page:** 1

## AI-generated summary

[MrDarkside](/t/-/166099/1) seeks a method to detect if a user is logged in to conditionally insert a script, noting that `Discourse.User.current()` is deprecated. [Canapin](/t/-/166099/2) suggests using the `api.getCurrentUser()` method within a script tag of type `text/discourse-plugin`. Although [MrDarkside](/t/-/166099/4) initially encountered a `ReferenceError` because `api` was not defined, [loginerror](/t/-/166099/5) clarified that the script type declaration is required to access the API object. After applying the correct script type, [MrDarkside](/t/-/166099/6) confirmed that while the code runs without errors, it produces no output, but acknowledges the direction for further investigation.

<div class="post-metadata">

**Author:** ![MrDarkside](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrdarkside/32/196020_2.png) [@MrDarkside](https://meta.discourse.org/u/MrDarkside)\
**Post date:** [October 3, 2020, 11:08am UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099/1 "2020-10-03T11:08:13Z")

</div>

I need to insert a script into Admin \> Customize \> CSS/HTML. It has to run only if a user not logged in

I tried Discourse.User.current() but got a deprecation warning:

> Deprecation notice: Import the User class instead of using User (deprecated since Discourse 2.4.0) (removal in Discourse 2.6.0)

Are there other ways to find it out?

---

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [October 3, 2020, 11:32am UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099/2 "2020-10-03T11:32:35Z")

</div>

```js
<script type="text/discourse-plugin" version="0.8">
const user = api.getCurrentUser();
if(user) {
    // do stuff
}
</script>

```

---

<div class="post-metadata">

**Author:** ![MrDarkside](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrdarkside/32/196020_2.png) [@MrDarkside](https://meta.discourse.org/u/MrDarkside)\
**Post date:** [October 3, 2020, 2:41pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099/3 "2020-10-03T14:41:16Z")

</div>

Oh so I have access to api object there, that’s nice. Thanks!

---

<div class="post-metadata">

**Author:** ![MrDarkside](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrdarkside/32/196020_2.png) [@MrDarkside](https://meta.discourse.org/u/MrDarkside)\
**Post date:** [October 3, 2020, 3:02pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099/4 "2020-10-03T15:02:15Z")

</div>

```
<script>
    const user = api.getCurrentUser();
    console.log(user)
</script>

```

> Uncaught ReferenceError: api is not defined

Oopsie

---

<div class="post-metadata">

**Author:** ![loginerror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/loginerror/32/156605_2.png) [@loginerror](https://meta.discourse.org/u/loginerror)\
**Post date:** [October 3, 2020, 3:11pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099/5 "2020-10-03T15:11:44Z")

</div>

You need to specify the type of your script as:  
`<script type="text/discourse-plugin" version="0.8">`

---

<div class="post-metadata">

**Author:** ![MrDarkside](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mrdarkside/32/196020_2.png) [@MrDarkside](https://meta.discourse.org/u/MrDarkside)\
**Post date:** [October 3, 2020, 3:45pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099/6 "2020-10-03T15:45:00Z")

</div>

No mistakes, but nothing happens either. Anyway, thanks, at least now I know which way to dig

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [November 2, 2020, 3:56pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-logged-in/166099/7 "2020-11-02T15:56:56Z")

</div>

Closed on November 2, 2020, 3:56pm UTC

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