# The session validation should be checked before every page is loaded and the login page should be redirected

**URL:** <https://discourse.nodered.org/t/the-session-validation-should-be-checked-before-every-page-is-loaded-and-the-login-page-should-be-redirected/93380>\
**Category:** General\
**Tags:** http-request, security\
**Created:** [23 November 2024 06:54 UTC](https://discourse.nodered.org/t/the-session-validation-should-be-checked-before-every-page-is-loaded-and-the-login-page-should-be-redirected/93380 "2024-11-23T06:54:27Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [24 November 2024 12:28 UTC](https://discourse.nodered.org/t/the-session-validation-should-be-checked-before-every-page-is-loaded-and-the-login-page-should-be-redirected/93380/4 "2024-11-24T12:28:36Z")

</div>

OK I think I can see what is going on.

try this...

Your Middleware

```auto
function sessionValidation(req, res, next) {
    const sessionId = req.cookies['session_id'];
    if (sessionId) {
        next()
    } else {
        res.redirect('/api/login');
    }
}

```

Settings.JS

```auto
    /** The following property can be used to add a custom middleware function
     * in front of all http in nodes. This allows custom authentication to be
     * applied to all http in nodes, or any other sort of common request processing.
     * It can be a single function or an array of middleware functions.
     */
    httpNodeMiddleware: sessionValidation,

    /** When httpAdminRoot is used to move the UI to a different root path, the
     * following property can be used to identify a directory of static content
     * that should be served at http://localhost:1880/.
     * When httpStaticRoot is set differently to httpAdminRoot, there is no need
     * to move httpAdminRoot
     */

```

Just tested this - and it works

---

_[View the full topic](https://discourse.nodered.org/t/the-session-validation-should-be-checked-before-every-page-is-loaded-and-the-login-page-should-be-redirected/93380)._
