Vote count:
0
Any call to http://mydomain.com/ results into 404 as soon as I add ensureAuthenticated to app.use('/', ensureAuthenticated, routes); and user is authenticated successfully. Note, when user is not authenticated it works correct and user is redirected to login page.
What could be wrong when user is auth successfully and trying to access home page?
Any call to /api/* works without any issues in all cases. I am using express 4.x, node, passport auth module with local strategy.
Below is my code: app.js:
var routes = require('./routes/index');
var auth = require('./routes/auth');
app.use('/', auth);
app.get('/api/*', ensureAuthenticated, apiHandler.handleApis);
app.post('/api/*', ensureAuthenticated, apiHandler.handleApis);
app.use('/', ensureAuthenticated, routes); //results into 404 for http://host/
//app.use('/', routes); //Everything works great if I replace above line with it
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
console.log('user is authenticated for the request, process id:' + process.pid);
return next();
}
res.redirect('/login12');
}
index.js
router.get('/', function(req, res) {
res.render('index', { title: 'home', name: 'home' });
});
router.get('/home', function(req, res) {
res.render('index', { title: 'home', name: 'home' });
});
module.exports = router;
auth.js
router.post('/api/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
return res.send(info);
}
req.logIn(user
, function(err) {
if (err) {
return next(err);
}
return res.send({ output : { result : 'Ok'} });
});
})(req, res, next);
});
router.post('/api/register', function(req, res, next) {
//code to register
});
router.get('/login12', function(req, res) {
console.log('GOING TO login PAGE')
res.render('login', {});
});
module.exports = router;
asked 16 secs ago
Aucun commentaire:
Enregistrer un commentaire