【问题标题】:facing issue with passport js面临护照js的问题
【发布时间】:2019-11-17 23:20:26
【问题描述】:

我是节点 js 的新手,我正在尝试使用 google passport 做一个授权示例,下面是我的代码:

index.js

const express = require('express');
const app = express();
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth20').Strategy;

passport.use(new GoogleStrategy({
      clientID        : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      clientSecret    : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      callbackURL     : "http://localhost/google/login",
      passReqToCallback   : true
  },
  function(accessToken, refreshToken, profile, done) {
    return done(); //this is the issue, I am confused with it's use
  }
));

app.get('/failed', function (req, res) {
  res.send('failed login')
});

app.get('/final', function (req, res) {
  res.send('finally google auth has done')
});

app.get('/auth/google',
  passport.authenticate('google', { scope: ['profile'] }));

app.get('/google/login',
  passport.authenticate('google', { failureRedirect: '/failed' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/final');
  });

app.listen('80', () => {
  console.log('server is running')
})

最后,我的目标是在不检查数据库的值的情况下成功登录谷歌,因为我只是在学习它。

节点索引.js

然后我打开网址:http://localhost/auth/google

我的程序应该在使用 google 凭据登录后运行 get /final,但出现 TypeError: done is not a function 的错误

我无法使用done(),我该如何解决。

【问题讨论】:

    标签: node.js passport.js passport-google-oauth passport-google-oauth2


    【解决方案1】:

    使用passReqToCallback : true时,需要更改回调函数的参数。 req 也需要作为回调函数的第一个参数传递。

    你的回调函数参数应该是(req, accessToken, refreshToken, profile, done)

    这就是你得到错误的原因:

    done 不是函数

    试试这个:

    passport.use(new GoogleStrategy({
          clientID        : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          clientSecret    : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          callbackURL     : "http://localhost/google/login",
          passReqToCallback   : true
      },
      function(req, accessToken, refreshToken, profile, done) {
        return done(); // it will work now
      }
    ));
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2021-09-30
      • 2019-05-23
      • 2017-05-29
      • 2017-08-18
      • 2017-02-20
      相关资源
      最近更新 更多