PassportJS:serializeUser()发送后不能设置标头

时间:2018-01-29 11:24:59

标签: javascript node.js passport.js

我只是想在" normal"之后尝试在passportjs中序列化用户。流。但是我一直收到以下错误:

Error: Can't set headers after they are sent.

我不知道在哪里可以发送res,因为我只是使用中间件发送它。以下是我如何调用身份验证路径:

routes.post('/signup', function(req, res, next) {
    passport.authenticate('local-signup-sme', {
        successRedirect : req.get('origin') + '/dashboard', // redirect to the secure profile section
        failureRedirect : req.get('origin') + '/signupSME', // redirect back to the signup page if there is an error
        failureFlash    : true,
        successFlash    : 'User created !'
    })(req, res, next)
});

这是我的local-signup-sme,我只调用done()而我只调用一次,或者在回调中,我检查过是否有其他但是我找不到但是我找不到任何...

我做错了什么?

 // used to serialize the user for the session
    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });

 passport.use('local-signup-sme', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
    },
    function(req, _email, _password, done) {
        if (typeof _password === 'undefined' ){
            return done(null, false, {message: 'Password field is missing'})
        } else if( typeof _email === 'undefined'){
            return done(null, false, {message: 'Email field is missing'})
        } else {
            var password = bcrypt.hashSync(_password, bcrypt.genSaltSync(10)); //encrypt password and store the hash...
            var companySelected = JSON.parse(req.body.companySelected);
            if (!req.user) {
                checkCompanyRegistrationStatus(companySelected.company_number,
                    function(){
                        checkUserRegistrationStatus(_email,
                            function(){
                                checkPasswordStrength(_password,
                                    function(){
                                        maindb.MainDBInsert("INSERT INTO Uncomplete_Users_Registrations (first_name, last_name, password, email, company_registrat_number, company_name) OUTPUT Inserted.id VALUES (?, ?, ?, ?, ?, ?);",
                                                    [req.body.firstName, req.body.lastName, password, _email, companySelected.company_number, companySelected.title],
                                                    function(user, rowsCount){
                                                        return done(null, user[0]);
                                                    },
                                                    function(code, err){
                                                        return done(null, false, {message:  err});
                                                    }
                                                );
                                            }, function (err) {
                                                return done(null, false, {message: err});
                                            })
                                    }, function(err){
                                        return done(null, false, {message: err})
                                    }
                                )

                            }, function(err){
                                return done(null, false, {message: err})
                            }
                        )
                    }, function(err){
                        return done(null, false, {message: err})
                    }
                )
            } else {
                // user is logged in and already has a local account. Ignore signup. (You should log out before trying to create a new account, user!)
                return done(null, req.user);
            }
        }
    }));

0 个答案:

没有答案