Get the username connected to the dashboard

Hi,

I have secured the acces of Node RED by setting up nginx. Then, i wanted to know the username of the person connected to the dashboard. So i added a middleware to the settings.js file. The code is below

	dashboard: {
		middleware: (request, response, next) => {
		const Username = request.headers['x-auth-user'];		
		console.log(Username);
		next()
		
		} 

},

When i connect to the dashboard, i get the username write in the console, as you can see below.

Now i want to get the username to the node red flows, but i don't know how to do it. I tried to use functionGlobalContext, but i figured that the list just update at the launch of node red. So when another user connect to the dashboard, the variable doesn't get refresh, and the variable in node red says "undefined".

Is there a way to store the current username in a global (or flow) variable, and have it automatically update whenever a new user connects to the dashboard?

Thanks already for your help !

I find how you can do it. You need to set the username as variable of the settings.js file like this

let Username = 'bonjour';

and the you need to modify the middleware of the dashboard, like this

	dashboard: {
		middleware: (request, response, next) => {
		Username = request.headers['x-auth-user'];		
		console.log(Username);
		next()
		
		} 
},

and finally you need to create a function that return the Username like this

	functionGlobalContext: {
	getUsername: () =>{
	return Username;
	},
},

Then in node red you just need to call the function in a function node like this to get the username.

const globalFunction = global.get('getUsername');
const username = globalFunction();

Hope it can helps !

I'm trying to get a guest in a slightly different way, but I don't know if it's good. Maybe the code from the description will give you some hints. Note: it completely blocks the dashboard if there is no user, this may be undesirable.

@aaqu/node-red-dashboard-2-portal-auth - npm

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