I use Passport-Google-OAuth to implement registration/login on my demo site. It works very well. Last several days I try to find solution how to access session variables inside "GoogleStrategy" implementation.
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);
});
}));
I don't know is this possible. I need to update existing user in database, not to create new one, but can't "get" _id of existing user in this function.
From the docs: http://passportjs.org/guide/authorize/
Association in Verify Callback
Set the strategy's passReqToCallback option to true. With this option enabled, req will be passed as the first argument to the verify callback.
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);
}
}
));
With req passed as an argument, the verify callback can use the state of the request to tailor the authentication process, handling both authentication and authorization using a single strategy instance and set of routes. For example, if a user is already logged in, the newly "connected" account can be associated. Any additional application-specific properties set on req, including req.session, can be used as well.
尝试添加passReqToCallback:true,它应该将req对象传递到您的回调中,然后将req对象作为回调中的第一个参数添加,例如,passport.use(new GoogleStrategy({passReqToCallback:true,ConsumerKey:…},function(req,令牌...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.