【问题标题】:Redirect with passport.js使用 passport.js 重定向
【发布时间】:2015-09-07 00:23:28
【问题描述】:

我正在使用 passport.js、passport-google-oauth 和 nodjes 来加载用户个人资料(本地)。但是登录后重定向不起作用。信息已加载(登录后我可以转到/google-profile)。 这是我的代码

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

var GOOGLE_CLIENT_ID = "bla";
var GOOGLE_CLIENT_SECRET = "bla";

var userPorifile = {};

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://localhost:8000/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    userPorifile = profile;
  }
));


var app = express();

app.get('/google-profile', function (req, res) {
    res.json(userPorifile);
});

app.get('/login',
  passport.authenticate('google', { scope:                 'https://www.googleapis.com/auth/plus.login' }));

app.get('/auth/google/callback?*', passport.authenticate('google', {     successRedirect : '/google-profile', failureRedirect: '/login' }), function(req,     res) {
    console.log("done");
    res.redirect('/google-profile');
});

app.use(function (req, res, next) {
    if (req.query.something) {
        next();
    } else {
        next();
    }
});

app.listen(8000);

谁能帮我解决这个问题?

【问题讨论】:

  • 护照通过google认证后有回调吗?
  • 是的,我设置userPorifile的函数被调用
  • 但我在控制台中没有看到“完成”。这样就不会调用该函数

标签: javascript node.js passport.js


【解决方案1】:

你应该有类似的东西:

 'googleAuth' : {
        'clientID'      : 'your-secret-clientID-here',
        'clientSecret'  : 'your-client-secret-here',
        'callbackURL'   : 'http://localhost:8080/auth/google/callback'
    }

在您的配置文件中,在您的护照文件中:

  passport.use(new GoogleStrategy({

        clientID        : configAuth.googleAuth.clientID,
        clientSecret    : configAuth.googleAuth.clientSecret,
        callbackURL     : configAuth.googleAuth.callbackURL,

    },

然后在路线中:

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

    // the callback after google has authenticated the user
    app.get('/auth/google/callback',
            passport.authenticate('google', {
                    successRedirect : '/profile',
                    failureRedirect : '/'
            }));

请注意,我不明白您使用 ?* 的回调,您没有使用 express 吗?

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2018-06-02
  • 2016-06-13
  • 2014-12-09
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
相关资源
最近更新 更多