![](/img/trans.png)
[英]Node.js How to maintain the desired data in the data session through the base Passport.js
[英]Node.js Passport GoogleStrategy how to access session data
我使用Passport-Google-OAuth在演示站點上實現注冊/登錄。 效果很好。 最近幾天,我嘗試找到一種解決方案,該解決方案如何在“ GoogleStrategy”實現中訪問會話變量。
var GoogleStrategy = require('passport-google-oauth').OAuthStrategy;
passport.use(new GoogleStrategy({
consumerKey: GOOGLE_CONSUMER_KEY,
consumerSecret: GOOGLE_CONSUMER_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/google/callback"
},
function(token, tokenSecret, profile, done) {
// Is it possible to access to session variables here, eg.:
// var tmp = req.session.tmp;
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}));
我不知道這可能嗎。 我需要更新數據庫中的現有用戶,而不是創建新用戶,但是無法在此函數中“獲取”現有用戶的_id。
從文檔中: http : //passportjs.org/guide/authorize/
驗證回調中的關聯
將策略的passReqToCallback選項設置為true。 啟用此選項后,req將作為第一個參數傳遞給verify回調。
passport.use(new TwitterStrategy({
consumerKey: TWITTER_CONSUMER_KEY,
consumerSecret: TWITTER_CONSUMER_SECRET,
callbackURL: "http://www.example.com/auth/twitter/callback",
passReqToCallback: true
},
function(req, token, tokenSecret, profile, done) {
if (!req.user) {
// Not logged-in. Authenticate based on Twitter account.
} else {
// Logged in. Associate Twitter account with user. Preserve the login
// state by supplying the existing user after association.
// return done(null, req.user);
}
}
));
將req作為參數傳遞后,verify回調可以使用請求的狀態來定制身份驗證過程,使用單個策略實例和一組路由來處理身份驗證和授權。 例如,如果用戶已經登錄,則可以關聯新的“已連接”帳戶。 也可以使用在req上設置的任何其他特定於應用程序的屬性,包括req.session。
嘗試添加passReqToCallback:true,它應該將req對象傳遞到您的回調中,然后將req對象作為回調中的第一個參數添加,例如,passport.use(new GoogleStrategy({passReqToCallback:true,ConsumerKey:…},function(req,令牌...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.