Skip to content

Commit

Permalink
OAuth and User Authentication
Browse files Browse the repository at this point in the history
- Use an OAuth providers for authenticating users within your server.
- Set up your server using Passport OAuth modules to enable user
authentication using OAuth providers.
  • Loading branch information
yogykwan committed May 29, 2017
1 parent 9128813 commit 4108c9f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
7 changes: 1 addition & 6 deletions conFusion-Express/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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')));

Expand Down
40 changes: 40 additions & 0 deletions conFusion-Express/authenticate.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
});
}
));
7 changes: 6 additions & 1 deletion conFusion-Express/config.js
Original file line number Diff line number Diff line change
@@ -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'
}
};
2 changes: 2 additions & 0 deletions conFusion-Express/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down
1 change: 1 addition & 0 deletions conFusion-Express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions conFusion-Express/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 4108c9f

Please sign in to comment.