Created
February 17, 2017 14:35
-
-
Save WORMSS/c6584b2d43aadc4e2c9a826d3471b170 to your computer and use it in GitHub Desktop.
A snippet of my Express.Route for OAuth
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
/* | |
Some extra stuff for setting up the server | |
*/ | |
// Setting up bare passport. No Auth Strategy. | |
const passport = require("./middlewear/my-passport-init"); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
// Setting up passport Strategies and Routes | |
app.use(require("./middlewear/my-passport-routes")); | |
/* | |
Below is where I setup all my extra little routes and staticly public files | |
*/ |
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 Router = require("express").Router; | |
const app = new Router(); | |
// Setting Google Auth Strategy on passport and required auth routes. | |
const passport_google = require("./passport-google"); | |
passport.use(passport_google.getStrategy()); | |
app.use(passport_google.getRoutes()); | |
// Setting Facebook Auth Strategy on passport and required auth routes. | |
const passport_facebook = require("./passport-facebook"); | |
passport.use(passport_facebook.getStrategy()); | |
app.use(passport_facebook.getRoutes()); | |
// Setting Twitter Auth Strategy on passport and required auth routes. | |
const passport_twitter = require("./passport-twitter"); | |
passport.use(passport_twitter.getStrategy()); | |
app.use(passport_twitter.getRoutes()); | |
// Setting Twitter Auth Strategy on passport and required auth routes. | |
const passport_github = require("./passport-github"); | |
passport.use(passport_github.getStrategy()); | |
app.use(passport_github.getRoutes()); | |
module.exports = app; |
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 Router = require("express").Router; | |
const passport = require("passport"); | |
const Strategy = require("passport-github").Strategy; | |
const User = require("../lib/mongoUtil").collection("User"); | |
const ObjectID = require("mongodb").ObjectID; | |
const oauth_options = { | |
"clientID": process.env.GITHUB_AUTH_CLIENT_ID, | |
"clientSecret": process.env.GITHUB_AUTH_CLIENT_SECRET, | |
"callbackURL": process.env.GITHUB_AUTH_CALLBACK_URL, | |
"passReqToCallback": true | |
}; | |
const strategy = new Strategy(oauth_options, function (req, accessToken, refreshToken, profile, cb) { | |
delete profile._raw; | |
let user = req.user; | |
if ( user ) { | |
User.findOneAndUpdate( | |
{ "_id": user._id }, | |
{ "$set": { "github_profile": profile, "github_token": accessToken } }, | |
{ "returnOriginal": false } | |
) | |
.then(doc => doc.value) | |
.then(user => cb(null, user)) | |
.catch(err => { | |
console.error("Error:", err); | |
cb(err); | |
}); | |
} else { | |
User.findOneAndUpdate( | |
{ "github_profile.id" : profile.id, }, // Query | |
{ "$set" : { "github_profile": profile, "github_token": accessToken } }, // Update | |
{ "upsert": true, "returnOriginal": false } // Options | |
) | |
.then(doc => doc.value) // TODO: Test for errors here. | |
.then(user => cb(null, user)) | |
.catch(err => { | |
console.error("Error:", err); | |
cb(err); | |
}); | |
} | |
}); | |
const app = new Router(); | |
app.get('/auth/github', passport.authenticate('github')); | |
app.get('/auth/github/callback', passport.authenticate('github', { "successReturnToOrRedirect": "/", "failureRedirect": '/login' })); | |
module.exports.getStrategy = function () { | |
return strategy | |
}; | |
module.exports.getRoutes = function (options) { | |
return app; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment