0

I am trying to restrict all pages if the user is not authenticated.

The strange thing is that my middleware is being called twice when I call the login page.

THe following are my code

app.use((req, res, next) => {
    if (req.session.user) {
        res.session.user = req.session.user;
    }
    if (req.isAuthenticated() || req.path === '/' || req.path === '/login'){
        console.log('inside next');
        next()
    }else{
        console.log('inside redirect');
        res.redirect('/')
    }
});
app.get('/', (req, res) => {

    console.log('inside /')

    res.render('./login', {
        css: ['login.css'],
        js: ['login.js']
    })
});

When I go to localhost:8000/ which is my root page, it prints out following in the console

inside next
inside /
inside redirect
inside next
inside /

As you can see after get('/') is called, the middleware is called once again to redirect the page. Why is this? If I remove the middleware, / gets called only once.

forJ
  • 4,309
  • 6
  • 34
  • 60
  • How are you calling the service. Might be something to do with the client code. – Samuel Toh Aug 09 '17 at 01:41
  • @SamuelToh what do you mean? which part of the code would you like to see – forJ Aug 09 '17 at 01:47
  • @SamuelToh as I have said, / gets rendered twice only when I add the middleware so I am guessing it's to do with that. – forJ Aug 09 '17 at 01:51
  • The client side javascript which calls `/` and `/login`. Maybe your `login` page is making a call to the `/login` endpoint upon page load? – Samuel Toh Aug 09 '17 at 01:59

1 Answers1

1

I found out why...

Apparently Chrome recently added a support for source map ESRI : Failed to parse source map

since the source map was not included in my vendor folder, it could not find the source map and refreshed the page.

So from now gotta take caution when copying bootstrap files - copy the source maps with the css file so that it doesn't refresh twice

forJ
  • 4,309
  • 6
  • 34
  • 60