Passport验证返回json而不是重定向角

时间:2017-10-03 22:02:31

标签: angular express passport.js

我正在尝试从成功登录或登录中返回json数据。 (这是如此有角度可以抓住它并在前端进行重定向,而不是在后端表示重定向)。

我已经尝试了我在这里看到的其他答案,这是有道理的,应该尽我所知。特别是这个答案:Can you authenticate with Passport without redirecting?说它有工作代码,但如果我尝试res.send,我会得到以下错误。

这是我的api:

app.post('/signup', passport.authenticate('local-signup', {
    successRedirect: '/loginSucceeded',
    failureRedirect: '/loginFailed'
  }
));

app.get('/loginSucceeded', (req, res) => {
  res.json({message:'/posts'});
});

app.get('/loginFailed', (req, res) => {
  res.json({message:'/login'});
});

在Promise._fulfill(C:\ micromotion \ node_modules \ bluebird \ js \ release \ promise.js:638:18)     在PromiseArray._resolve(C:\ micromotion \ node_modules \ bluebird \ js \ release \ promise_array.js:126:19)     在PromiseArray._promiseFulfilled(C:\ micromotion \ node_modules \ bluebird \ js \ release \ promise_array.js:144:14)     在Promise._settlePromise(C:\ micromotion \ node_modules \ bluebird \ js \ release \ promise.js:574:26)     在Promise._settlePromise0(C:\ micromotion \ node_modules \ bluebird \ js \ release \ promise.js:614:10)     在Promise._settlePromises(C:\ micromotion \ node_modules \ bluebird \ js \ release \ promise.js:693:18)     at Async._drainQueue(C:\ micromotion \ node_modules \ bluebird \ js \ release \ async.js:133:16)     在Async._drainQueues(C:\ micromotion \ node_modules \ bluebird \ js \ release \ async.js:143:10)     在Immediate.Async.drainQueues(C:\ micromotion \ node_modules \ bluebird \ js \ release \ async.js:17:14)     在runCallback(timers.js:672:20)     在tryOnImmediate(timers.js:645:5)     at processImmediate [as _immediateCallback](timers.js:617:5)

我尝试过其他一些事情,例如使用函数切换成功和失败重定向并获得相同的错误。这是否意味着错误在我的护照策略范围内?我很难理解它,但它是从教程中提取的:

 passport.use('local-signup', new LocalStrategy(

    {
      usernameField: 'email',
      passwordField: 'password',
      passReqToCallback: true // allows us to pass back the entire request to the callback
    },


    function(req, email, password, done) {
      var generateHash = function(password) {
        return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null);
      };

      User.findOne({
        where: {
          email: email
        }
      }).then(function(user) {
        if (user)
        {
          return done(null, false, {
            message: 'That email is already taken'
          });

        } else

        {
          var userPassword = generateHash(password);
          var data =
          {
            email: email,
            password: userPassword,
            firstname: req.body.firstname,
            lastname: req.body.lastname
          };

          User.create(data).then(function(newUser, created) {

            if (!newUser) {
              return done(null, false);
            }

            if (newUser) {
              return done(null, newUser);
            }
          });
        }
      });
    }
  ));

我的任何角色代码都可以调用它,但实际上并不重要:

  signup(data){
    return this.http.post('/api/signup', data)
      .toPromise()
      .then(response => {
        console.log(response);
       // this.router.navigate([response.url])
      });
  }

1 个答案:

答案 0 :(得分:0)

我能够使用他们的文档中的自定义回调来解决它。猜猜我只需更好地理解req / res / next。

app.post('/signup', function(req, res, next){
  passport.authenticate('local-signup', function(err, user, info){
    if(err){return next(err);}
    if(!user){return res.send({redirect: '/signup'});}
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.send({redirect: '/posts'});
    });
  }) (req, res, next);
});

and angular:

  signup(data){
    return this.http.post('/api/signup', data)
      .toPromise()
      .then(response => {
        var r = response.json();
       this.router.navigate([r.redirect])
      });
  }