# Programmatically logout the user

**URL:** https://discourse.nodered.org/t/programmatically-logout-the-user/9171
**Category:** General
**Created:** [19 March 2019 09:07 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171 "2019-03-19T09:07:31Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![smrtfn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smrtfn/32/6954_2.png) [@smrtfn](https://discourse.nodered.org/u/smrtfn)
#### Post date: [19 March 2019 09:07 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/1 "2019-03-19T09:07:31Z")

</div>

Hi,

Is it possible to logout the user programmatically in a node's code? Otherwords, is it possible to call RED.user.logout() inside a node's .js file?

Thank you

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [19 March 2019 09:21 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/2 "2019-03-19T09:21:10Z")

</div>

I'm sure you can try it just as quickly as I can...

---

<div class="post-metadata">

### Author: ![smrtfn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smrtfn/32/6954_2.png) [@smrtfn](https://discourse.nodered.org/u/smrtfn)
#### Post date: [19 March 2019 09:31 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/3 "2019-03-19T09:31:08Z")

</div>

Nice. I already had tried it. But I get:

TypeError: Cannot read property 'logout' of undefined

What is missing? Must I define something before calling RED.user.logout()?

Regards

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [19 March 2019 09:45 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/4 "2019-03-19T09:45:57Z")

</div>

I didn't say it would work...

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [19 March 2019 10:34 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/5 "2019-03-19T10:34:00Z")

</div>

Hi @smrtfn

The js code runs in the runtime part of Node-RED. It doesn't have any access to the editor and doesn't know about users.

The html code of a node does run in the editor, so would have access to the logout function. But that is at edit time, not as a result of a flow being run.

Why do you want to log a user out?

---

<div class="post-metadata">

### Author: ![smrtfn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smrtfn/32/6954_2.png) [@smrtfn](https://discourse.nodered.org/u/smrtfn)
#### Post date: [19 March 2019 10:43 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/6 "2019-03-19T10:43:26Z")

</div>

Thank you for the answer. I have written a node that changes admin/user passwords and writes new passwords to settings.js file. It works properly. But I want to logout actual user if the password has changed.

Regards

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [19 March 2019 11:24 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/7 "2019-03-19T11:24:58Z")

</div>

There is no API in the runtime to do that.

Node-RED needs to be restarted to get it to reread the settings file. Or you could create a custom Auth plugin that uses a separate file that you manage yourself.

But fact remains, there is no runtime api to invalidate a user's token available to a node.

This is an area we want to look at - making it easier to manage users in the runtime. But it isn't a priority at the moment.

---

<div class="post-metadata">

### Author: ![smrtfn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smrtfn/32/6954_2.png) [@smrtfn](https://discourse.nodered.org/u/smrtfn)
#### Post date: [19 March 2019 11:51 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/8 "2019-03-19T11:51:50Z")

</div>

Thank you for the information. As you specified, I can do it at html side such as a node's button's onclick, oneditsave etc. If it would be possible to trig/call one of these html functions from .js code side, it would be a solution for me.

Regards

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [19 March 2019 13:34 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/9 "2019-03-19T13:34:45Z")

</div>

Hi, there are several examples here in how you can send messages from a flow to a html page as well as to an ui template node in the dashboard. Once you receive the message, you could call a javascript function and do the thing

---

<div class="post-metadata">

### Author: ![smrtfn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smrtfn/32/6954_2.png) [@smrtfn](https://discourse.nodered.org/u/smrtfn)
#### Post date: [20 March 2019 08:22 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/10 "2019-03-20T08:22:26Z")

</div>

Hi krambriw,

Thank you for the suggestion. I have searched, found a few examples but could not find how to continuously listen and catch message object in my node's html code. Can you please specify me the example that close to my need?

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [20 March 2019 08:59 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/11 "2019-03-20T08:59:58Z")

</div>

In the following threads are examples

> [@Button in TopBar](https://discourse.nodered.org/t/button-in-topbar/6377/27):
>
> You wanna send a payload to the ui\_template and change something for the button? Or do you want the ui\_template to send out a payload (this is already working I assume)?

> [@Access msg object in dashboard ui\_template](https://discourse.nodered.org/t/access-msg-object-in-dashboard-ui-template/5502/12):
>
> Hi, not sure if you have a question or if you already solved the issue. Anyway, based on my previous example, this is how you can access the payload and assign values to other variables in the script Lets assume, you send a javascript object {"name":"jmacle"} as payload to the ui\_template node In the template node, the script captures the payload, sends it further to the debug node then extracts the "name", assigns it to you new variable bleh, and sends the value of bleh to the debug node As …

I suppose you wanna do something like

_some node in Node-RED -\> msg -\> ui template -\> extract msg.payload -\> call a javascript function to do some stuff_  
or  
_some node in Node-RED -\> msg -\> template node with your html/javascript code -\> extract msg.payload -\> call a javascript function to do some stuff_

It's unclear how your architecture looks like so best is you provide your flow "as is" and we can look at it and give further advice.

---

<div class="post-metadata">

### Author: ![smrtfn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smrtfn/32/6954_2.png) [@smrtfn](https://discourse.nodered.org/u/smrtfn)
#### Post date: [20 March 2019 09:17 UTC](https://discourse.nodered.org/t/programmatically-logout-the-user/9171/12 "2019-03-20T09:17:21Z")

</div>

I just want to call RED.user.logout() function programmatically to logout actual user. I can not call this in my node's .js code. I can call it in my node's .html code. For example, if I put it in my node's button's onclick, works perfect.

According to your suggestion now I am trying to add a function in my node's .html that waits for a message and if it get's a proper message, then calls RED.user.logout(). I will not use ui\_template. I think if I can catch the message object in my node's .html, I can do it. But it seems this will not be a simple code.

Thanks
