redirectFailure google auth

时间:2018-01-23 22:11:04

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

我在使用NodeJS,Passport.js和Google Oauth2策略实施GoogleOauth2时遇到了问题。

我已按照此处给出的所有说明操作: (我没有遵循mongoDB指令,只想在console.log中获取auth详细信息)。 http://www.passportjs.org/docs/google/ 根据Oauth2战略。

对于'范围'我定义'openid': PassportJS google scope variable

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

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

控制台会在控制台中显示用户openid详细信息,包括用户ID,名称等,以便实际发送auth详细信息。

但是'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();
        }
    ));

}

1 个答案:

答案 0 :(得分:0)

解决!

问题是必须由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);
    });

}

使用它作为护照功能可以完全解决问题。