【问题标题】:Passport authenticate return json instead of redirect for angular护照身份验证返回 json 而不是 Angular 重定向
【发布时间】:2018-03-15 05:06:24
【问题描述】:

我正在尝试从我成功的登录或登录中返回 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) 在 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) 在 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);
            }
          });
        }
      });
    }
  ));

我调用它的任何 Angular 代码,但实际上并不重要:

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

【问题讨论】:

    标签: angular express passport.js


    【解决方案1】:

    我能够使用他们文档中的自定义回调来解决它。我想我只需要更好地理解 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);
    });
    

    和角度:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 2020-01-30
      • 2018-11-13
      • 2019-01-19
      • 2016-11-10
      • 1970-01-01
      • 2018-11-06
      相关资源
      最近更新 更多