Skip to content

Instantly share code, notes, and snippets.

@rkpattnaik780
Created August 6, 2019 04:04
Show Gist options
  • Save rkpattnaik780/a67a283ca0d65f737bd9dd707a67f207 to your computer and use it in GitHub Desktop.
Save rkpattnaik780/a67a283ca0d65f737bd9dd707a67f207 to your computer and use it in GitHub Desktop.
server/config/passport-setup.js
const passport = require("passport");
var GitHubStrategy = require("passport-github2").Strategy;
const User = require("../models/user-model");
// Passport takes that user id and stores it internally on
// req.session.passport which is passport’s internal
// mechanism to keep track of things.
passport.serializeUser((user, done) => {
done(null, user.id);
});
// makes a request to our DB to find the full profile information
// for the user and then calls done(null, user). This is where
// the user profile is attached to the request handler at req.user.
passport.deserializeUser((id, done) => {
User.findById(id).then(user => {
// This takes the profile info and attaches it on the request
// object so its available on your callback url as req.user.
done(null, user);
});
});
// Implementing the passport github strategy
passport.use(
new GitHubStrategy(
{
clientID: "XXXXXXXXXX",
clientSecret: "caXXXXX4c35ce089XXXXXXXXXX4d1bae",
callbackURL: "/auth/github/redirect"
},
(accessToken, refreshToken, profile, done) => {
// Callback method triggered upon signing in.
User.findOne({ githubId: profile.id }).then(currentUser => {
if (currentUser) {
// already have this user
done(null, currentUser);
} else {
// if not, create user in our db
new User({
githubId: profile.id,
username: profile.username,
name: profile.displayName,
image: profile._json.avatar_url
})
.save()
.then(newUser => {
done(null, newUser);
});
}
});
}
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment