重定向 passport.js

Redirect with passport.js

我正在使用 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);

谁能帮我解决这个问题?

你应该有这样的东西:

 '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 ?