social login in node js and express js

Solutions on MaxInterview for social login in node js and express js by the best coders in the world

showing results for - "social login in node js and express js"
Pedro
10 Oct 2020
1passport.use('twitter', new TwitterStrategy({
2    consumerKey     : twitterConfig.apikey,
3    consumerSecret  : twitterConfig.apisecret,
4    callbackURL     : twitterConfig.callbackURL
5  },
6  function(token, tokenSecret, profile, done) {
7    // make the code asynchronous
8    // User.findOne won't fire until we have all our data back from Twitter
9    process.nextTick(function() { 
10 
11      User.findOne({ 'twitter.id' : profile.id }, 
12        function(err, user) {
13          // if there is an error, stop everything and return that
14          // ie an error connecting to the database
15          if (err)
16            return done(err);
17 
18            // if the user is found then log them in
19            if (user) {
20               return done(null, user); // user found, return that user
21            } else {
22               // if there is no user, create them
23               var newUser                 = new User();
24 
25               // set all of the user data that we need
26               newUser.twitter.id          = profile.id;
27               newUser.twitter.token       = token;
28               newUser.twitter.username = profile.username;
29               newUser.twitter.displayName = profile.displayName;
30               newUser.twitter.lastStatus = profile._json.status.text;
31 
32               // save our user into the database
33               newUser.save(function(err) {
34                 if (err)
35                   throw err;
36                 return done(null, newUser);
37               });
38            }
39         });
40      });
41    })
42);
43