diff --git a/conFusion-Express/app.js b/conFusion-Express/app.js index 2d1495f..6a3d28d 100644 --- a/conFusion-Express/app.js +++ b/conFusion-Express/app.js @@ -6,8 +6,7 @@ var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var mongoose = require('mongoose'); var passport = require('passport'); -var LocalStrategy = require('passport-local').Strategy; - +var authenticate = require('./authenticate'); var config = require('./config'); mongoose.connect(config.mongoUrl); @@ -47,11 +46,7 @@ app.use(bodyParser.urlencoded({extended: false})); app.use(cookieParser()); // passport config -var User = require('./models/user'); app.use(passport.initialize()); -passport.use(new LocalStrategy(User.authenticate())); -passport.serializeUser(User.serializeUser()); -passport.deserializeUser(User.deserializeUser()); app.use(express.static(path.join(__dirname, 'public'))); diff --git a/conFusion-Express/authenticate.js b/conFusion-Express/authenticate.js new file mode 100644 index 0000000..63c3c8d --- /dev/null +++ b/conFusion-Express/authenticate.js @@ -0,0 +1,40 @@ +var passport = require('passport'); +var User = require('./models/user'); +var config = require('./config'); +var LocalStrategy = require('passport-local').Strategy; +var GithubStrategy = require('passport-github').Strategy; + +exports.local = passport.use(new LocalStrategy(User.authenticate())); +passport.serializeUser(User.serializeUser()); +passport.deserializeUser(User.deserializeUser()); + +exports.github = passport.use(new GithubStrategy({ + clientID: config.github.clientID, + clientSecret: config.github.clientSecret, + callbackURL: config.github.callbackURL + }, + function (accessToken, refreshToken, profile, done) { + User.findOne({OauthId: profile.id}, function (err, user) { + if (err) { + console.log(err); // handle errors! + } + if (!err && user !== null) { + done(null, user); + } else { + user = new User({ + username: profile.displayName + }); + user.OauthId = profile.id; + user.OauthToken = accessToken; + user.save(function (err) { + if (err) { + console.log(err); // handle errors! + } else { + console.log("saving user ..."); + done(null, user); + } + }); + } + }); + } +)); \ No newline at end of file diff --git a/conFusion-Express/config.js b/conFusion-Express/config.js index f309107..0c17cac 100644 --- a/conFusion-Express/config.js +++ b/conFusion-Express/config.js @@ -1,4 +1,9 @@ module.exports = { 'secretKey': 'http://jennica.space', - 'mongoUrl': 'mongodb://localhost:27017/conFusion' + 'mongoUrl': 'mongodb://localhost:27017/conFusion', + 'github': { + clientID: '7cde94e0406e8531304e', + clientSecret: 'af4814dbfcee082e0d62e2ceb5f0ddef9bd5b5c0', + callbackURL: 'https://localhost:3443/users/github/callback' + } }; \ No newline at end of file diff --git a/conFusion-Express/models/user.js b/conFusion-Express/models/user.js index 110d1e5..f345e52 100644 --- a/conFusion-Express/models/user.js +++ b/conFusion-Express/models/user.js @@ -5,6 +5,8 @@ var passportLocalMongoose = require('passport-local-mongoose'); var User = new Schema({ username: String, password: String, + OauthId: String, + OauthToken: String, firstname: { type: String, default: '' diff --git a/conFusion-Express/package.json b/conFusion-Express/package.json index 55abae3..812120a 100644 --- a/conFusion-Express/package.json +++ b/conFusion-Express/package.json @@ -19,6 +19,7 @@ "mongoose-currency": "~0.2.0", "morgan": "~1.8.1", "passport": "~0.3.2", + "passport-github": "~1.1.0", "passport-local": "~1.0.0", "passport-local-mongoose": "~4.0.0", "serve-favicon": "~2.4.2", diff --git a/conFusion-Express/routes/users.js b/conFusion-Express/routes/users.js index 8b5b2f0..b9db03d 100644 --- a/conFusion-Express/routes/users.js +++ b/conFusion-Express/routes/users.js @@ -68,4 +68,34 @@ router.get('/logout', function (req, res) { }); }); +router.get('/github', passport.authenticate('github'), function (req, res) { + +}); + +router.get('/github/callback', function (req, res, next) { + passport.authenticate('github', function (err, user, info) { + if (err) { + return next(err); + } + if (!user) { + return res.status(401).json({ + err: info + }); + } + req.logIn(user, function (err) { + if (err) { + return res.status(500).json({ + err: 'Could not log in user' + }); + } + var token = Verify.getToken(user); + res.status(200).json({ + status: 'Login successful!', + success: true, + token: token + }); + }); + })(req, res, next); +}); + module.exports = router; \ No newline at end of file