Programmatically logout the user



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



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

1 Like


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()?




I didn't say it would work...



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?



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.




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.



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.




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



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?



In the following threads are examples

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
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.



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.