Quesurfin Quesurfin - 1 year ago 59
Node.js Question

Express redirects to wrong view after passport authentication

I'm having quite the time figuring out why the URL is updating but it goes to the wrong view.

When a user is authenticated with passport, the URL updates to my code (/clients) but the view goes back to the homepage view.

After the user is authenticated, i'd like them to be redirected to my angular app.

folder structure

server
--config
-auth
-express
-mongoose
-passport
-routes
--controllers
--data
--utils
--views
-client (ng app)
-css
-js
-views
index.jade (angular app index)
-shared
layout.jade
-users
login.jade
index.jade (express index)


express.js

app.set('view engine', 'jade');
app.set('views', config.rootPath + '/server/views/');


auth.js

login: function(req, res, next) {
var auth = passport.authenticate('local', function(err, user) {
if (err) return next(err);
if (!user) {
req.session.error = 'Invalid Username or Password!';
res.redirect('/login');
}

req.logIn(user, function(err) {
if (err) return next(err);
res.redirect('/client');
console.log();
})
});
auth(req, res, next);
},...


Routes.js

var auth = require('./auth'),
controllers = require('../controllers');

module.exports = function(app) {
app.get('/register', controllers.users.getRegister);
app.post('/register', controllers.users.createUser);

app.post('/login', auth.login);
app.get('/logout', auth.logout);
app.get('/login', controllers.users.getLogin);

app.get('/', function (req, res) {
res.render('index', {currentUser: req.user});
});

app.get('*', function (req, res) {
res.render('index', {currentUser: req.user});
});
};


If anyone can point me in the right direction, i'd greatly appreciate it.

Answer Source

Add a route

   app.get('/client', function (req, res) {
     res.render('client/index', {currentUser: req.session.user});
   });