# 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:** 6

<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 13:44 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/6 "2024-11-24T13:44:39Z")

</div>

Is this an API you're building or a Web interface?

if an API, I wouldn't use redirects

```auto
function sessionValidation(req, res, next) {
    if (req.originalUrl === '/api/login') {
        next()
    } else {
        const sessionId = req.cookies['session_id'];
        if (sessionId) {
            next()
        } else {
             res.json({error:'Access Denied',reason:'NO_TOKEN_PRESENT'})
        }
    }
}

```

if a web interface

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

```

Without knowing your setup fully - its a little hard to understand what is the right answer.

Also be carful about redirects and HTTP Methods - I _think_ the redirect needs to handle the original method - else you may get 404 for the redirect target, I'm not entirely sure

---

_[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)._
