在前端访问护照消息

时间:2019-01-23 01:21:04

标签: jquery node.js passport.js

passport s的done函数有第三个参数,我在前端无法访问它。我知道在sessionflash上设置了其他参数,但是如何在常规的jquery post请求中访问它呢?这是代码:

passport.use('local-signup', 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 back the entire request to the callback
},
function(req, email, password, done) {
  console.log('local-signup function');
    // User.findOne wont fire unless data is sent back
    process.nextTick(function() {

    User.findOne({'local.email': email}).then(function(foundUser){
      // If the user was never registered, create new user
      if (foundUser === null) {
        console.log('Incorrect email');
        done(null, false, { error: 'email', message: 'Incorrect email.' });
      } else {
        console.log(foundUser);
        // The user was already registered locally
        if (!foundUser.validPassword(password)) {
          console.log('Incorrect password');
          return done(null, false, { error: 'pwd', message: 'Incorrect password.' });
        } else {
          return done(null, foundUser);
        }
      }  
    })

    });
}));

我正在向ajax请求,我想获得在message passport中设置的LocalStrategy

$.ajax({
            url : $(this).attr('action') || window.location.pathname,
            type: "POST",
            data: $(this).serialize(),
            success: function (data) {
                var baseURL = getBaseUrl();
                var redirectURL = baseURL;
                window.location.replace(redirectURL);
            },
            error: function (jXHR, textStatus, errorThrown) {
                var error = jXHR.responseJSON['error'];
                var errorMsg = jXHR.responseJSON['message'];
                var statusCode = jXHR.status;
                console.log(statusCode);
                console.log(textStatus);
                console.log(errorThrown);
                $("#form_output").text(errorMsg);
                if (statusCode === 401) {
                    if (error === 'email') {
                        $("#form_email").addClass('is-invalid');
                    } else if (error === 'pwd') {
                        $("#form_password").addClass('is-invalid');
                    }
                } 
            }
        });

我不确定,也许我应该使用自定义回调,但是我有很多策略可能需要不同的处理方式:

app.post('/login', passport.authenticate(['facebook-token', 'local-signup']), function(req, res) {

})

0 个答案:

没有答案