【问题标题】:redirectFailure google auth重定向失败谷歌身份验证
【发布时间】:2018-07-02 20:38:30
【问题描述】:

我在使用 NodeJS、Passport.js 和 google Oauth2 策略实现 GoogleOauth2 时遇到问题。

我已按照此处给出的所有说明进行操作: (我没有遵循 mongoDB 的说明,只是想 console.log 现在的身份验证详细信息)。 http://www.passportjs.org/docs/google/ 在 Oauth2 策略下。

对于“范围”,我定义了“openid”: PassportJS google scope variable

该应用程序正常工作,它会将我重定向到谷歌的登录页面,我可以在那里登录,它“似乎”可以工作。

我进行了设置,以便在身份验证完成时回调触发一个函数来回显谷歌提供的内容: PassportJS boilerplate callback

控制台确实会在控制台中显示用户的 openid 详细信息,包括他们的用户 ID、姓名等,因此实际上会发回身份验证详细信息。

但是...“redirectFailure”也会触发。 PassportJS googleauth callbackEndpoint

我不知道如何调试 redirectFailure 触发的原因。

我的凭据正确,客户端密码正确(我通过输入假数字并尝试进行检查,结果出现未经授权的错误,但使用我的详细信息可以正常运行)。

有人可以帮忙吗? 谢谢。

完整代码:(用打字稿编写)。

    declare function require(name: string);
declare var __dirname;
declare var process;
declare var module;


const express = require('express');
const hbs = require('hbs');
var bodyParser = require('body-parser')
var flash = require('connect-flash');

var passport = require('passport');
require('./ouath1')(passport);

export function startServer(PORT, passport) {
    var app = express();

    app.use(passport.initialize());
    app.use(passport.session());
    app.use(flash());

    app.use(express.static(__dirname + '/public'));
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());

    app.set('view engine', 'hbs');
    app.set('views', __dirname + '/views')

    app.get('/healthz', (req, res) => {
        res.send("TestSuccess");
    })

    app.get('/', (req, res) => {
        res.render('login.hbs');
    })

    // GET /auth/google
    //   Use passport.authenticate() as route middleware to authenticate the
    //   request.  The first step in Google authentication will involve
    //   redirecting the user to google.com.  After authorization, Google
    //   will redirect the user back to this application at /auth/google/callback
    app.get('/auth/google',
        passport.authenticate('google', { scope: 'openid' }));

    // GET /auth/google/callback
    //   Use passport.authenticate() as route middleware to authenticate the
    //   request.  If authentication fails, the user will be redirected back to the
    //   login page.  Otherwise, the primary route function function will be called,
    //   which, in this example, will redirect the user to the home page.
    app.get('/readback',
        passport.authenticate('google', { failureRedirect: '/login' }),
        function (req, res) {
            res.redirect('/');
        });

    app.listen(PORT || 3000), () => {
        console.log("listening");
    };

    return app;
}

this.startServer(process.env.PORT,passport);

验证码:

    declare function require(name: string);
declare var process;
declare var module;

var configAuth = require('./config/auth.js');

var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

// Use the GoogleStrategy within Passport.
//   Strategies in Passport require a `verify` function, which accept
//   credentials (in this case, an accessToken, refreshToken, and Google
//   profile), and invoke a callback with a user object.

module.exports = function (passport) {
    passport.use(new GoogleStrategy({
        clientID: configAuth.googleAuth.clientID,
        clientSecret: configAuth.googleAuth.clientSecret,
        callbackURL: configAuth.googleAuth.callbackURL
    },
        function (accessToken, refreshToken, profile, done) {
            console.log("Access Token:", accessToken);
            console.log("Refresh Token:", refreshToken);
            console.log("Profile:", profile);
            done();
        }
    ));

}

【问题讨论】:

    标签: oauth-2.0 passport.js google-authentication


    【解决方案1】:

    解决了!

    问题是 serialise 函数必须由 passport.use 函数定义和使用。

    即:

    module.exports = function (passport) {
        passport.use(new GoogleStrategy({
            clientID: configAuth.googleAuth.clientID,
            clientSecret: configAuth.googleAuth.clientSecret,
            callbackURL: configAuth.googleAuth.callbackURL
        },
            function (accessToken, refreshToken, profile, cb) {
                // console.log("Access Token:", accessToken);
                // console.log("Refresh Token:", refreshToken);
                // console.log("Profile:", profile);
                return cb(null, profile);
            }
        ));
    
        passport.serializeUser(function (user, cb) {
            cb(null, user);
        });
    
        passport.deserializeUser(function (obj, cb) {
            cb(null, obj);
        });
    
    }
    

    使用它作为护照功能完全解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2016-06-28
      • 2016-01-26
      • 2016-04-03
      • 1970-01-01
      • 2021-12-06
      • 2019-03-17
      • 1970-01-01
      • 2016-11-14
      • 2019-06-16
      相关资源
      最近更新 更多