Passport + Node.js / Automatic login after adding user

后端 未结 3 2342
一生所求
一生所求 2020-12-24 11:56

I am using passport for authentication and session handling. Everything works fine so far. I implemented a \"Sign in\" form to add new users to the app. After a user is adde

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 12:08

    Please use code from the @Weston answer bellow, because it's more universal and straightforward

    Should look something like this

    app.post('/sign', function(req, res){
        authProvider.saveUser(...do stuff), function(error, user){
            if(error){
                res.redirect('/sign');
            } else {
                passport.authenticate('local')(req, res, function () {
                    res.redirect('/account');
                })
            }
        });
    });         
    

    I don't sure about name of strategy, but by default LocalStrategy should provide 'local' name

    http://passportjs.org/guide/authenticate/

提交回复
热议问题