# Session Management and UI Redirection in Node-RED Dashboard Widgets

**URL:** <https://discourse.nodered.org/t/session-management-and-ui-redirection-in-node-red-dashboard-widgets/93539>\
**Category:** Dashboard\
**Tags:** dashboard-2\
**Created:** [28 November 2024 09:51 UTC](https://discourse.nodered.org/t/session-management-and-ui-redirection-in-node-red-dashboard-widgets/93539 "2024-11-28T09:51:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DhamodharanGopal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhamodharangopal/32/95512_2.png) [@DhamodharanGopal](https://discourse.nodered.org/u/DhamodharanGopal)\
**Post date:** [28 November 2024 09:51 UTC](https://discourse.nodered.org/t/session-management-and-ui-redirection-in-node-red-dashboard-widgets/93539/1 "2024-11-28T09:51:15Z")

</div>

I'm implementing session management in my Node-RED `ui_base.js` file and handling user sessions using WebSocket cookies. Here are the challenges and requirements I'm facing:

1. **Redirect to Login on Session Expiry:**

- I've implemented session handling in the `onConnection` function.
- When no cookies are found or the session ID is invalid/expired, I disconnect the socket.
- On page reload, users are redirected to the login page, but I also want them to be redirected dynamically when their session expires or is destroyed, without requiring a page reload.

1. **Prevent Widget Interaction Without Session:**

- When a user's session expires, I need to prevent interaction with UI widgets like sliders, gauges, or other inputs.
- Currently, the widget interactions send input payloads to the server, which I want to avoid for expired sessions.

1. **Dynamic Redirection on UI Events:**

- When users perform actions like changing pages or interacting with widgets, if their session is invalid, I need to:
  - Prevent the action or input payload from being processed.
  - Redirect them to the login page dynamically.

**Questions:**

- How can I implement dynamic redirection to the login page during user interaction (e.g., widget input, page change) without requiring a browser reload?
- What is the best approach to control or block widget interactions when the session is invalid or expired?
- Are there existing best practices or middleware in Node-RED for session-based user interaction control?

```auto
/**
         * on connection handler for SocketIO
         * @param {Socket} socket socket.io socket connecting to the server
         */
        function onConnection(socket) {
            // Record mapping from connection to the ui-base node
            socket._baseId = node.id;
        
            // Handle cookies
            let cookies = socket.request.headers.cookie;
            console.log(" **************** cookies in socket: ", cookies);
        
            if (!cookies) {
                console.log("No cookies found, closing socket connection.");
                //socket.emit('redirect', '/login.html'); // Emit the redirect event with the login page URL
                socket.disconnect(true); // Disconnect the WebSocket
                return;
            }
            

             // node.connections[socket.id] = socket // store the connection for later use
                    uiShared.connections[socket.id] = socket // store the connection for later use
        
                    emitConfig(socket)
        
                    // clean up then re-register listeners
                    // cleanupEventHandlers(socket)
                    // setup connections, and fire any 'on('connection')' events
                    setupEventHandlers(socket, true)
        }

```

Any guidance or code samples to address these issues would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 December 2024 09:51 UTC](https://discourse.nodered.org/t/session-management-and-ui-redirection-in-node-red-dashboard-widgets/93539/2 "2024-12-28T09:51:41Z")

</div>

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