Express Passport.js Successful redirect does not load the page, the request remains pending

I am trying to perform basic authentication of a username and password using a .js passport and a local passport.

While failRedirect does exactly what it should do (redirect to the specified page), successRedirect continues to wait for the request for the specified page, and after a while an "empty response" is returned.

http://www.deviantpics.com/VdG

As you can see in this picture, when it asks for the control panel, it says that its size is 0B, but when I go to this panel without redirection, it says it has 1.6B.

I looked through Stackoverflow and did not find an answer that would help me.

Could you check my code and suggest something before I go crazy.

This is the passport load code.

//set expression var expressSession = require('express-session'); app.use(expressSession({ secret: credentials.session.secret })); //set passport var passport = require('passport'); var localStrategy = require('./strategies/auth/local.js'); passport.use('local', localStrategy); passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findById(id, function(err, user) { done(err, user); }); }); app.use(passport.initialize()); app.use(passport.session()); 

routes.js

module.exports = function (app) {

 //main app.get('/', main.home); app.get('/login', main.login); app.get('/signup', main.signup); app.post('/login', auth.loginLocal); app.post('/signup', main.checkSignup); //user app.get('/user/dashboard', user.dashboard); app.get('/user/addmemory', user.addMemory); app.get('/user/memory', user.memory); 

login function

 exports.loginLocal = passport.authenticate('local', { successRedirect: '/user/dashboard', failureRedirect: '/login' }); 

local strategy

 var localAuthStrategy = new LocalStrategy(function(username, password, done) { User.findOne({ username: username }, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'Incorrect username' }); } if (!user.validPassword(password)) { return done(null, false, { message: 'Incorrect password' }); } return done(null, user); }); }); 

dashboard function

 exports.dashboard = function(req, res) { res.render('user/dashboard', { layout: 'user' }); }; 
+5
source share
2 answers

I found the answer to my question, the problem was that the User model was not defined in the script where serializeUser and deserializeUser were defined. I could not understand what was happening because I did not define any actions in the catch all handler, so remember, be sure to catch the entire handler to know what is happening.

 app.use(function(err, req, res, next) { console.log(err); }); 
+5
source

A few days ago I ran into the same problem as you and found that I forgot to put the brackets at the end of the serializeUser and deserializeUser . In fact, I used the passport-local-mongoose package for the corresponding functions. But it should be noted that in app.use() functions are called and executed for all template files, so we use parentheses with function names.

0
source

All Articles