-
-
Save camperbot/ff3a1166684c1b184709ac0bee30dee6 to your computer and use it in GitHub Desktop.
Advanced Node and Express - Implementation of Social Authentication II
This file contains 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 LocalStrategy = require('passport-local'); | |
const bcrypt = require('bcrypt'); | |
const ObjectID = require('mongodb').ObjectID; | |
const GitHubStrategy = require('passport-github').Strategy; | |
module.exports = function (app, myDataBase) { | |
passport.serializeUser((user, done) => { | |
done(null, user._id); | |
}); | |
passport.deserializeUser((id, done) => { | |
myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => { | |
if (err) return console.error(err); | |
done(null, doc); | |
}); | |
}); | |
passport.use(new LocalStrategy( | |
function (username, password, done) { | |
myDataBase.findOne({ username: username }, function (err, user) { | |
console.log('User ' + username + ' attempted to log in.'); | |
if (err) { return done(err); } | |
if (!user) { return done(null, false); } | |
if (!bcrypt.compareSync(password, user.password)) { return done(null, false); } | |
return done(null, user); | |
}); | |
} | |
)); | |
passport.use(new GitHubStrategy({ | |
clientID: process.env.GITHUB_CLIENT_ID, | |
clientSecret: process.env.GITHUB_CLIENT_SECRET, | |
callbackURL: 'https://boilerplate-advancednode.your-username.repl.co/auth/github/callback' | |
}, | |
function (accessToken, refreshToken, profile, cb) { | |
console.log(profile); | |
// Database logic here with callback containing our user object | |
} | |
)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment