Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github OAuth #40

Merged
merged 2 commits into from
Jul 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anak10thn Github Oauth profile data does not provide email so there is nothing could be value of this field. This has an impact on frontend/dashboard side. Any strings with email field will not be appear.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signin with Github,

orcinusbug

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