Skip to content

Commit

Permalink
Merge pull request #40 from herpiko/githubauth3
Browse files Browse the repository at this point in the history
Github OAuth
  • Loading branch information
anak10thn authored Jul 17, 2017
2 parents dd72c99 + de7e23e commit 50848a2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 30 deletions.
37 changes: 34 additions & 3 deletions apis/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ var jwt = require('jsonwebtoken');
var jwtDecode = require('jwt-decode');
var utils = require('../lib/utils.js');
var userModel = mongoose.model('Users');
var passport;

/* Google OAuth */
if (
process.env.GOOGLE_OAUTH_CLIENT_ID &&
process.env.GOOGLE_OAUTH_CLIENT_SECRET &&
process.env.GOOGLE_OAUTH_CALLBACK_URL
) {
var passport = require('passport');
passport = require('passport');
require(__dirname + '/passport.js')(passport);
/* Google OAuth */

router.route("/google")
.get(passport.authenticate('google', { scope : ['profile', 'email'] } ));

Expand All @@ -33,8 +35,37 @@ if (
});
res.send(`<script type="text/javascript">sessionStorage.setItem("orcinus","${token}"); window.location = "/";</script>`);
});
}

/* Github OAuth */
if (
process.env.GITHUB_OAUTH_CLIENT_ID &&
process.env.GITHUB_OAUTH_CLIENT_SECRET &&
process.env.GITHUB_OAUTH_CALLBACK_URL
) {
if (!passport) {
passport = require('passport');
require(__dirname + '/passport.js')(passport);
}

router.route("/github")
.get(passport.authenticate('github', { scope : ['profile', 'email'] } ));

/* TODO FacebooktOAuth */
router.route("/github-callback")
.get(passport.authenticate('github'), (req, res) => {
if (!req.user || (req.user && !req.user.username)) {
return res.redirect('/');
}
var userJWT = {
username : req.user.username,
email : req.user.email,
id : req.user._id
};
var token = jwt.sign(userJWT, req.app.locals.secret, {
expiresIn: 60*60 // expires in 1 hours
});
res.send(`<script type="text/javascript">sessionStorage.setItem("orcinus","${token}"); window.location = "/";</script>`);
});
}

/* GET home page. */
Expand Down
88 changes: 63 additions & 25 deletions apis/passport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var mongoose = require('mongoose');
var LocalStrategy = require('passport-local').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var GithubStrategy = require('passport-github').Strategy;
var userModel = mongoose.model('Users');
var urandom = require('urandom');
var md5 = require('md5');
Expand All @@ -14,31 +15,68 @@ module.exports = function(passport) {
done(err, user);
});
});
passport.use(new GoogleStrategy({
clientID : process.env.GOOGLE_OAUTH_CLIENT_ID,
clientSecret : process.env.GOOGLE_OAUTH_CLIENT_SECRET,
callbackURL : process.env.GOOGLE_OAUTH_CALLBACK_URL,
},
function(token, refreshToken, profile, done){
process.nextTick(function(){
userModel.findOne({ $or : [{'googleId' : profile.id }, { email : profile.emails[0].value }, { username : profile.emails[0].value } ] }, (err, user) => {
if (err) return done(err);
if (user) {
return done(null, user);
}
var user = new userModel();
user.googleId = profile.id;
user.googleToken = token;
user.email = profile.emails[0].value;
user.username = profile.emails[0].value;
user.password = md5(urandom.randomIt());
user.verify = true;
user.admin = false;
user.save((err) => {
if (
process.env.GOOGLE_OAUTH_CLIENT_ID &&
process.env.GOOGLE_OAUTH_CLIENT_SECRET &&
process.env.GOOGLE_OAUTH_CALLBACK_URL
) {
passport.use(new GoogleStrategy({
clientID : process.env.GOOGLE_OAUTH_CLIENT_ID,
clientSecret : process.env.GOOGLE_OAUTH_CLIENT_SECRET,
callbackURL : process.env.GOOGLE_OAUTH_CALLBACK_URL,
},
function(token, refreshToken, profile, done){
process.nextTick(function(){
userModel.findOne({ $or : [{'googleId' : profile.id }, { email : profile.emails[0].value }, { username : profile.emails[0].value } ] }, (err, user) => {
if (err) return done(err);
return done(null, user);
});
if (user) {
return done(null, user);
}
var user = new userModel();
user.googleId = profile.id;
user.googleToken = token;
user.email = profile.emails[0].value;
user.username = profile.emails[0].value;
user.password = md5(urandom.randomIt());
user.verify = true;
user.admin = false;
user.save((err) => {
if (err) return done(err);
return done(null, user);
});
});
});
});
}));
}));
}
if (
process.env.GITHUB_OAUTH_CLIENT_ID &&
process.env.GITHUB_OAUTH_CLIENT_SECRET &&
process.env.GITHUB_OAUTH_CALLBACK_URL
) {
passport.use(new GithubStrategy({
clientID : process.env.GITHUB_OAUTH_CLIENT_ID,
clientSecret : process.env.GITHUB_OAUTH_CLIENT_SECRET,
callbackURL : process.env.GITHUB_OAUTH_CALLBACK_URL,
}, function(token, refreshToken, profile, done){
process.nextTick(function(){
userModel.findOne({ $or : [{'githubId' : profile.id }, { username : profile.username } ] }, (err, user) => {
if (err) return done(err);
if (user) {
return done(null, user);
}
var user = new userModel();
user.githubId = profile.id;
user.githubToken = token;
user.username = profile.username;
user.password = md5(urandom.randomIt());
user.verify = true;
user.admin = false;
user.save((err) => {
if (err) return done(err);
return done(null, user);
});
});
});
}));
}
}
3 changes: 1 addition & 2 deletions db/model/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var mongoose = require('mongoose')
UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
firstname: { type: String },
Expand Down Expand Up @@ -57,4 +56,4 @@ UserSchema.methods.comparePass = function(candidatePassword, cb) {
});
};

module.exports = mongoose.model('Users', UserSchema);
module.exports = mongoose.model('Users', UserSchema);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"morgan": "~1.7.0",
"orcinusd": "^0.3.3",
"passport": "^0.3.2",
"passport-github": "^1.1.0",
"passport-google-oauth": "^1.0.0",
"passport-local": "^1.0.0",
"path": "^0.12.7",
Expand Down

0 comments on commit 50848a2

Please sign in to comment.