nodejs api social media login code example
Example 1: facebook integration in node.js
const request = require('request-promise');
module.exports = (app) => {
const userFieldSet = 'name, link, is_verified, picture';
const pageFieldSet = 'name, category, link, picture, is_verified';
app.post('/facebook-search', (req, res) => {
const { queryTerm, searchType } = req.body;
const options = {
method: 'GET',
uri: 'https://graph.facebook.com/search',
qs: {
access_token: config.user_access_token,
q: queryTerm,
type: searchType,
fields: searchType === 'page' ? pageFieldSet : userFieldSet
}
};
request(options)
.then(fbRes => {
const parsedRes = JSON.parse(fbRes).data;
res.json(parsedRes);
})
});
}
Example 2: social login in node js and express js
passport.use('twitter', new TwitterStrategy({
consumerKey : twitterConfig.apikey,
consumerSecret : twitterConfig.apisecret,
callbackURL : twitterConfig.callbackURL
},
function(token, tokenSecret, profile, done) {
process.nextTick(function() {
User.findOne({ 'twitter.id' : profile.id },
function(err, user) {
if (err)
return done(err);
if (user) {
return done(null, user);
} else {
var newUser = new User();
newUser.twitter.id = profile.id;
newUser.twitter.token = token;
newUser.twitter.username = profile.username;
newUser.twitter.displayName = profile.displayName;
newUser.twitter.lastStatus = profile._json.status.text;
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
})
);