Created
October 28, 2018 02:08
-
-
Save chatman-media/69e0a9a89eeeb8b2cafb0c668d6fa2e4 to your computer and use it in GitHub Desktop.
final-fb-passport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const passport = require('passport'); | |
const router = express.Router(); | |
const { fbAuth, fbCb } = require('../lib/facebook'); | |
router.get('/oauth/:id/facebook', fbAuth); | |
router.get('/oauth/:id/facebook', fbAuth()); | |
module.exports = router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const passport = require('passport'); | |
const apiRouter = require('./routes/api'); | |
const app = express(); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use('/api', apiRouter); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const models = require('../models'); | |
const email = require('../utils/email'); | |
const passportFacebook = require('../auth/facebook'); | |
const fbAuth = (req, res) => { | |
req.session.isRegisterUser = req.query.isRegisterUser || false; | |
req.session.regUserId = req.params.id > 0 ? req.params.id : null; | |
req.session.redirectUrlAfterSignIn = req.query.redirectUrl ? req.query.redirectUrl : null; | |
return passportFacebook.authenticate('facebook', {})(res, req); | |
}; | |
const fbCb = (req, res) => { | |
return passportFacebook.authenticate('facebook', { | |
successRedirect: '/step4', | |
failureRedirect: '/login' | |
}, (err, profile) => { | |
console.log(err); | |
console.log(profile); | |
})(req, res); | |
}; | |
module.exports = { | |
fbCb, | |
fbAuth | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const passport = require('passport'); | |
const Strategy = require('@passport-next/passport-facebook').Strategy; | |
passport.use(new Strategy({ | |
clientID: process.env.FACEBOOK_APP_ID, | |
clientSecret: process.env.FACEBOOK_SECRET, | |
callbackURL: 'http://localhost:3001/api/oauth/facebook/callback', | |
graphApiVersion: 'v3.1', | |
// scope: ['email'] | |
}, | |
function(accessToken, refreshToken, profile, cb) { | |
// NEVER called | |
console.log(`accessToken`, accessToken); | |
console.log(`profile`, profile); | |
return cb(null, profile); | |
} | |
)); | |
passport.serializeUser(function(user, done) { | |
done(null, user.id); | |
}); | |
passport.deserializeUser(function(id, done) { | |
return models.User.findById(id).then(user=>{ | |
done(null, user); | |
}).catch(function (err) { | |
done(null, null); | |
}); | |
}); | |
module.exports = passport; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment