Hey guys, I've been stumped here for quite a while and I've looked around without any leads. I'm quite new to the node environment and I wanted to play around with authentication -- which is also a new topic for me.Anyways, regarding the question, I'm using express sessions and a mongo store to save session state (as well as storing it in a db) and wanted to use the passport-spotify strategy to connect and save that data to my local session and db as well. However, their strategy doesn't allow a req param on their local strategy so I can't save my user data to my current session. Here's their example for their strategy off the github: passport.use( new SpotifyStrategy( { clientID: appKey, clientSecret: appSecret, callbackURL: 'http://localhost:8888/callback' }, function(accessToken, refreshToken, expires_in, profile, done) { // asynchronous verification, for effect... process.nextTick(function() { // To keep the example simple, the user's spotify profile is returned to // represent the logged-in user. In a typical application, you would want // to associate the spotify account with a user record in your database, // and return that user instead. return done(null, profile); }); } ) ); I want to use User.findOne to check if my current session user has a spotifyid as well as saving the data to the current session user and back onto my db, but again, their function doesn't accept a request parameter so I can't check if a user even exists. I've found a great tutorial and an example of the facebook strategy aiming at exactly what I'm trying to accomplish:passport.use(new FacebookStrategy({ // pull in our app id and secret from our auth.js file clientID : configAuth.facebookAuth.clientID, clientSecret : configAuth.facebookAuth.clientSecret, callbackURL : configAuth.facebookAuth.callbackURL, passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not) }, function(req, token, refreshToken, profile, done) { process.nextTick(function() { if (!req.user) { User.findOne({ 'facebook.id' : profile.id }, function(err, user) { if (err) return done(err); if (user) { if (!user.facebook.token) { user.facebook.token = token; user.facebook.name = profile.name.givenName + ' ' + profile.name.familyName; user.facebook.email = profile.emails[0].value; user.save(function(err) { if (err) throw err; return done(null, user); }); } return done(null, user); // user found, return that user } else { var newUser = new User(); newUser.facebook.id = profile.id; // set the users facebook id newUser.facebook.token = token; // we will save the token that facebook provides to the user newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName; // look at the passport user profile to see how names are returned newUser.facebook.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first newUser.save(function(err) { if (err) throw err; // if successful, return the new user return done(null, newUser); }); } }); } else { var user = req.user; // pull the user out of the session user.facebook.id = profile.id; user.facebook.token = token; user.facebook.name = profile.name.givenName + ' ' + profile.name.familyName; user.facebook.email = profile.emails[0].value; // save the user user.save(function(err) { if (err) throw err; return done(null, user); }); } }); })); Furthermore, this session is 'saving', but it's over-riding my current user session. Here is what I current have:passport.use( new SpotifyStrategy({ clientID: spotifyConf.client_id, clientSecret: spotifyConf.client_secret, callbackURL: spotifyConf.redirect_uri }, function (accessToken, refreshToken, expires_in, profile, done ) { // Need this req.session but no req in params? process.nextTick(function () { Person.findOne({ spotify_id: profile.id }, function (err, user) { if (err) { return done(err); } if (!user) { var person = Person; user = new Spotify({ Spotify_id: profile.id, display_name: profile.displayName, access_token: accessToken, refresh_token: refreshToken, profile_pic: profile.photos[0], person: person._id }); user.save(function (err) { if (err) console.log(err); return done(err, user); }); } else { //found user. Return return done(null, user); } }); }); } ) ); Am I overlooking something completely obvious? I've been walled on this for a couple days. Any suggestions would be incredibly helpful.
Submitted November 15, 2018 at 09:01PM by BroXplode
No comments:
Post a Comment