简体   繁体   English

使用passport.js重定向

[英]Redirect with passport.js

I am using passport.js, passport-google-oauth with nodjes to load a users profile (local). 我正在使用nodjes使用passport.js,passport-google-oauth来加载用户个人资料(本地)。 But the redirect after logging in doesn't work. 但是登录后的重定向不起作用。 The information has been loaded (I can go to /google-profile after logged in). 信息已加载(登录后我可以进入/ google-profile)。 This is my code 这是我的代码

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);

Can anybody help me with this? 有人可以帮我吗?

you should have something like : 你应该有这样的东西:

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

in your config file, in your passport file : 在您的配置文件中,在您的护照文件中:

  passport.use(new GoogleStrategy({

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

    },

then in routes: 然后在路线上:

 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 : '/'
            }));

Please note that I don't understand your callback with ?* , you're not using express ? 请注意,我不理解您使用?*进行的回调,您未使用express吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM