2015-06-21 33 views
0

我正在使用passport.js,passport-google-oauth和nodjes加载用户配置文件(本地)。但登录后重定向不起作用。信息已被加载(我可以在登录后进入/ google-profile)。 这是我的代码重定向passport.js

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

任何人可以帮助我吗?

+0

你有一个回调后,与谷歌护照认证? – alexsc

+0

是的,我设置userPorifile的功能被称为 – SaschaDeWaal

+0

但是我没有在控制台中看到“完成”。所以这个功能不叫 – SaschaDeWaal

回答

0

你应该是这样的:

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

请注意,我不不理解你的回拨?*,你没有使用快递?

+0

非常感谢! – SaschaDeWaal