Skip to content

Instantly share code, notes, and snippets.

@mateusvahl
Created January 18, 2015 21:08
Show Gist options
  • Save mateusvahl/80d0fda929a1bf8e22dd to your computer and use it in GitHub Desktop.
Save mateusvahl/80d0fda929a1bf8e22dd to your computer and use it in GitHub Desktop.
var twitterStrategy = require('passport-twitter').Strategy;
var facebookStrategy = require('passport-facebook').Strategy;
// Tem que criar um APP no twitter/facebook pra ter a chave publica e a secreta
//
var config = require('../config/passport');
var twConfig = {
consumerKey: config.twitter.key,
consumerSecret: config.twitter.secret,
callbackURL: '/auth/twitter/callback',
passReqToCallback : true
};
var fbConfig = {
clientID: config.facebook.id,
clientSecret: config.facebook.secret,
callbackURL: '/auth/facebook/callback',
profileFields: ['id'],
passReqToCallback : true
};
function serializeUser(user, done) {
done(null, user);
}
function deserializeUser(obj, done) {
done(null, obj);
}
function afterAuthFacebook(req, accessToken, tokenSecret, profile, done) {
// Salvando a chave na sessão
req.session.facebookToken = accessToken;
done(null, profile);
}
function afterAuthTwitter(req, accessToken, tokenSecret, profile, done) {
// Salvando as chaves na sessão
req.session.twitterToken = accessToken;
req.session.twitterTokenSecret = tokenSecret;
// Twitter responde com nome do usr e oturas coisas se quiser salvar na sessão
req.session.userName = profile.displayName;
req.session.userPhoto = JSON.parse(profile._raw).profile_image_url;
done(null, profile);
}
/**
* exports
*/
module.exports = function(passport) {
passport.serializeUser(serializeUser);
passport.deserializeUser(deserializeUser);
passport.use('twitter', new twitterStrategy(twConfig, afterAuthTwitter));
passport.use('facebook', new facebookStrategy(fbConfig, afterAuthFacebook));
};
@mateusvahl
Copy link
Author

var express = require('express');
var router = express.Router();

   /**
   * Rotas
   */
module.exports = function(passport) {
  router.get('/auth/twitter', passport.authenticate('twitter'));
  router.get('/auth/facebook', passport.authenticate('facebook'));
  router.get('/auth/twitter/callback', passport.authenticate('twitter', {successRedirect: '/', failureRedirect: '/' }));
  router.get('/auth/facebook/callback', passport.authenticate('facebook', {successRedirect: '/', failureRedirect: '/' }));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment