1

On my express server I'am tring to implement the following logic:

  1. If the path is not /Login or /Register

  2. And the request.session.authenticated is not true.

  3. Redirect all request to /Login.

In other words , pseudocode :

function()
    {
        if (req.session.authenticated !== true && path !== '/Login')
            res.redirect('/Login')
    }


How can i write this function in Express with app.get() or similar ?

app.get( path , (req, res) => 
{
  // ??
})

------------------------------------------- After edited --------------------------

So , I tried this :

app.use(redirect())

function redirect(req, res, next) 
{
  if (req.session.authenticated !== true && req.path !== '/Login/Login.html' || req.session.authenticated !== true && req.path !== '/Register/Register.html') 
    {
      res.redirect('/Login/Login.html');
      return next()
    }
}

But , I got the following error :

  if (req.session.authenticated !== true && req.path !== '/Login/Login.html' || req.session.authenticated !== true && req.path !== '/Register/Register.html')
          ^

TypeError: Cannot read property 'session' of undefined

I guess that for some reason this function does not have acess to the request object which is weird. How can I make it acess the request ? I thought all middleware functions would have acess to the request.

------------------------------------2nd Edit------------------------------------

In my case the middleware function above only works if you save it to a variable and then use app.use() calling the variable the function was saved in.

So that would be :


app.use(redirectMe)

let redirectMe = function redirect(req, res, next) 
{
  if (req.session.authenticated !== true && req.path !== '/Login/Login.html' || req.session.authenticated !== true && req.path !== '/Register/Register.html') 
    {
      res.redirect('/Login/Login.html');
      return next()
    }
}
izzypt
  • 170
  • 2
  • 16

2 Answers2

1

You can write something like an authentication middleware for this.

function redirect(req, res, next) {
        if (req.session.authenticated !== true && req.path !== '/Login' && req.path !== '/Register') {
            res.redirect('/Login');
            return
        }
    }

And the you just need to export that function and you can use it as a middleware in your routes. Something like this:

app.get('/profile', redirect(), (req, res) => {
  //Some code here
});
voxtool
  • 345
  • 4
  • 11
  • If I do something like app.use(redirect()) , would the server run that function for every HTTP request that comes in ? – izzypt Apr 16 '21 at 17:15
  • Yes it will work like this, I think it should be without () but you can try both ways. And if there is a problem with this usage instead of return you can write **next()** – voxtool Apr 16 '21 at 17:33
  • Take a look at my edit , for some reason I do not have acess to the request. – izzypt Apr 16 '21 at 17:48
  • Have you tried using it this way: app.use(redirect) – voxtool Apr 16 '21 at 17:56
  • yes , but it does nto work because if I do that it doesn't call the fuction. – izzypt Apr 16 '21 at 18:08
0

Declare a function in a golobal variable as shown below

let verifyLogin = (req, res,next){ 
        if(req.session.authenticated)
        { 
          next()
        } else {
          res.redirect('/login')
        }
    }

The above function is called from app.get() as shown below

 app.get('path', verifyLogin, (req, res)=>{
             //
    })
Muneeb T
  • 1
  • 1