Reputation: 854
I am in the process of moving from PHP to NodeJS, I'm trying to understand why a middleware redirect is spinning out of control.
// Check to see if the request includes a session Username
app.use(function (req, res, next) {
sessionmanager = req.session;
if(sessionmanager.Username) {
console.log('Session Details detected');
next();
} else {
console.log('No session details detected, pushing user to login page');
res.redirect('/login');
}
});
// User Landing Page
app.get('/', function (req, res) {
console.log('Request for landing page');
res.send('Welcome Page!');
});
// User Login Page
app.get('/login', function (req, res) {
console.log('Request for User login page');
res.sendFile(path.join(__dirname, './public', 'login.html'));
});
When I visit localhost:3000 it redirects me to localhost:3000/login but the page fails to load because it looks like its been redirected too many times. When I look at the console log, I can count; 20+ rows of message 'No session details detected, pushing user to login page'.
From what I can guess, the redirect loops back to app.use function, fails to pass the session manager.Username statement and gets stuck in a loop. Whats the best way to redirect traffic to the /login page? Can I put an exclude clause in app.use?
Upvotes: 0
Views: 579
Reputation: 320
You have your login route below the middleware. It should be to top of your route list something like this
// User Login Page
app.get('/login', function (req, res) {
console.log('Request for User login page');
res.sendFile(path.join(__dirname, './public', 'login.html'));
});
// Check to see if the request includes a session Username
app.use(function (req, res, next) {
sessionmanager = req.session;
if(sessionmanager.Username) {
console.log('Session Details detected');
next();
} else {
console.log('No session details detected, pushing user to login page');
res.redirect('/login');
}
});
// User Landing Page
app.get('/', function (req, res) {
console.log('Request for landing page');
res.send('Welcome Page!');
});
Hope you understand
Upvotes: 0
Reputation: 854
From reading the docs at: https://expressjs.com/en/guide/using-middleware.html You can set the requests so that /login comes before app.use this way its not part of the redirect loop. Example:
// User Login Page
app.get('/login', function (req, res) {
console.log('Request for User login page');
res.sendFile(path.join(__dirname, './public', 'login.html'));
});
// Check to see if the request includes a session Username
app.use(function (req, res, next) {
sessionmanager = req.session;
if(sessionmanager.Username) {
console.log('Session Details detected');
next();
} else {
console.log('No session details detected, pushing user to login page');
res.redirect('/login');
//res.sendFile(path.join(__dirname, './public', 'login.html'));
}
});
// User Landing Page
app.get('/', function (req, res) {
console.log('Request for landing page');
res.send('Welcome Page!');
});
Now you can make requests to the /login url and any pages past app.use will push you back to the login page if you have not got a session logged in.
Upvotes: 1