2015-06-21 75 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