2014-02-27 27 views
0

我是新來的node.js環境,我使用passport.js進行身份驗證。 我知道如何使用passport-google進行身份驗證,但我不知道如何將電子郵件ID,姓名,照片等數據從經過身份驗證的Google帳戶獲取到HTML表單。 下面一個是server.js如何使用passport.js獲取電子郵件ID,Google名稱等數據?

.. 
var passport = require('passport') 
.. 
app.get('/auth/google', passport.authenticate('google')); 
app.get('/auth/google/return', passport.authenticate('google', { successRedirect: '/', 
           failureRedirect: '/login' })); 

而且request.js文件是

var passport = require('passport'); 
var GoogleStrategy = require('passport-google').Strategy; 

passport.use(new GoogleStrategy({ 
returnURL: 'http://localhost:9000/profilepage.html', 
realm: 'http://localhost:9000' 
}, 
function(identifier, profile, done) { 
User.findOrCreate({ openId: identifier }, function(err, user) { 
    done(err, user); 
}); 
} 
)); 

回答

1

輪廓數據將被填充到的GoogleStrategy回調函數的第二個參數(名爲profile)。看看這個代碼示例:https://github.com/jaredhanson/passport-google/blob/master/examples/signon/app.js

你可以這樣訪問用戶配置文件信息:

function(identifier, profile, done) { 
    var userData = {identifier: identifier}; 

    // append more data if needed 
    userData.email = profile.emails[0]; 
    userData.displayName = profile.displayName; 
    userData.fullName = profile.familyName + " " + profile.givenName; 
    // id is optional 
    if (profile.id) { 
    userData.id = profile.id; 
    } 

    return done(null, userData); 
}); 
+0

如何獲得的生日? – Foreever

+0

當console.log(userData.fullName) –

相關問題