This commit is contained in:
Sahat Yalkabov
2013-11-13 15:49:58 -05:00
parent d8c0505420
commit 06b777cce9
32 changed files with 569 additions and 1077 deletions

8
config/env/all.js vendored
View File

@@ -1,8 +0,0 @@
var path = require('path'),
rootPath = path.normalize(__dirname + '/../..');
module.exports = {
root: rootPath,
port: process.env.PORT || 3000,
db: process.env.MONGOHQ_URL
}

View File

@@ -1,26 +0,0 @@
{
"db": "mongodb://localhost/mean-dev",
"app": {
"name": "MEAN - A Modern Stack - Development"
},
"facebook": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/facebook/callback"
},
"twitter": {
"clientID": "CONSUMER_KEY",
"clientSecret": "CONSUMER_SECRET",
"callbackURL": "http://localhost:3000/auth/twitter/callback"
},
"github": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/github/callback"
},
"google": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/google/callback"
}
}

View File

@@ -1,26 +0,0 @@
{
"db": "mongodb://localhost/mean",
"app": {
"name": "MEAN - A Modern Stack - Production"
},
"facebook": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/facebook/callback"
},
"twitter": {
"clientID": "CONSUMER_KEY",
"clientSecret": "CONSUMER_SECRET",
"callbackURL": "http://localhost:3000/auth/twitter/callback"
},
"github": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/github/callback"
},
"google": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/google/callback"
}
}

27
config/env/test.json vendored
View File

@@ -1,27 +0,0 @@
{
"db": "mongodb://localhost/mean-test",
"port": 3001,
"app": {
"name": "MEAN - A Modern Stack - Test"
},
"facebook": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/facebook/callback"
},
"twitter": {
"clientID": "CONSUMER_KEY",
"clientSecret": "CONSUMER_SECRET",
"callbackURL": "http://localhost:3000/auth/twitter/callback"
},
"github": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/github/callback"
},
"google": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/google/callback"
}
}

View File

@@ -1,27 +0,0 @@
{
"db": "mongodb://localhost/mean-travis",
"port": 3001,
"app": {
"name": "MEAN - A Modern Stack - Test on travis"
},
"facebook": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/facebook/callback"
},
"twitter": {
"clientID": "CONSUMER_KEY",
"clientSecret": "CONSUMER_SECRET",
"callbackURL": "http://localhost:3000/auth/twitter/callback"
},
"github": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/github/callback"
},
"google": {
"clientID": "APP_ID",
"clientSecret": "APP_SECRET",
"callbackURL": "http://localhost:3000/auth/google/callback"
}
}

View File

View File

@@ -1,33 +0,0 @@
/**
* Generic require login routing middleware
*/
exports.requiresLogin = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.send(401, 'User is not authorized');
}
next();
};
/**
* User authorizations routing middleware
*/
exports.user = {
hasAuthorization: function(req, res, next) {
if (req.profile.id != req.user.id) {
return res.send(401, 'User is not authorized');
}
next();
}
};
/**
* Article authorizations routing middleware
*/
exports.article = {
hasAuthorization: function(req, res, next) {
if (req.article.user.id != req.user.id) {
return res.send(401, 'User is not authorized');
}
next();
}
};

View File

@@ -1,172 +0,0 @@
var mongoose = require('mongoose'),
LocalStrategy = require('passport-local').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
FacebookStrategy = require('passport-facebook').Strategy,
GitHubStrategy = require('passport-github').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
User = mongoose.model('User'),
config = require('./config');
module.exports = function(passport) {
//Serialize sessions
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findOne({
_id: id
}, function(err, user) {
done(err, user);
});
});
//Use local strategy
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(email, password, done) {
User.findOne({
email: email
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {
message: 'Unknown user'
});
}
if (!user.authenticate(password)) {
return done(null, false, {
message: 'Invalid password'
});
}
return done(null, user);
});
}
));
//Use twitter strategy
passport.use(new TwitterStrategy({
consumerKey: config.twitter.clientID,
consumerSecret: config.twitter.clientSecret,
callbackURL: config.twitter.callbackURL
},
function(token, tokenSecret, profile, done) {
User.findOne({
'twitter.id_str': profile.id
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
user = new User({
name: profile.displayName,
username: profile.username,
provider: 'twitter',
twitter: profile._json
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
return done(err, user);
}
});
}
));
//Use facebook strategy
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callbackURL
},
function(accessToken, refreshToken, profile, done) {
User.findOne({
'facebook.id': profile.id
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
user = new User({
name: profile.displayName,
email: profile.emails[0].value,
username: profile.username,
provider: 'facebook',
facebook: profile._json
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
return done(err, user);
}
});
}
));
//Use github strategy
passport.use(new GitHubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.github.callbackURL
},
function(accessToken, refreshToken, profile, done) {
User.findOne({
'github.id': profile.id
}, function(err, user) {
if (!user) {
user = new User({
name: profile.displayName,
email: profile.emails[0].value,
username: profile.username,
provider: 'github',
github: profile._json
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
return done(err, user);
}
});
}
));
//Use google strategy
passport.use(new GoogleStrategy({
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.google.callbackURL
},
function(accessToken, refreshToken, profile, done) {
User.findOne({
'google.id': profile.id
}, function(err, user) {
if (!user) {
user = new User({
name: profile.displayName,
email: profile.emails[0].value,
username: profile.username,
provider: 'google',
google: profile._json
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
return done(err, user);
}
});
}
));
};

View File

@@ -1,78 +0,0 @@
module.exports = function(app, passport, auth) {
//User Routes
var users = require('../app/controllers/users');
app.get('/signin', users.signin);
app.get('/signup', users.signup);
app.get('/signout', users.signout);
//Setting up the users api
app.post('/users', users.create);
app.post('/users/session', passport.authenticate('local', {
failureRedirect: '/signin',
failureFlash: 'Invalid email or password.'
}), users.session);
app.get('/users/me', users.me);
app.get('/users/:userId', users.show);
//Setting the facebook oauth routes
app.get('/auth/facebook', passport.authenticate('facebook', {
scope: ['email', 'user_about_me'],
failureRedirect: '/signin'
}), users.signin);
app.get('/auth/facebook/callback', passport.authenticate('facebook', {
failureRedirect: '/signin'
}), users.authCallback);
//Setting the github oauth routes
app.get('/auth/github', passport.authenticate('github', {
failureRedirect: '/signin'
}), users.signin);
app.get('/auth/github/callback', passport.authenticate('github', {
failureRedirect: '/signin'
}), users.authCallback);
//Setting the twitter oauth routes
app.get('/auth/twitter', passport.authenticate('twitter', {
failureRedirect: '/signin'
}), users.signin);
app.get('/auth/twitter/callback', passport.authenticate('twitter', {
failureRedirect: '/signin'
}), users.authCallback);
//Setting the google oauth routes
app.get('/auth/google', passport.authenticate('google', {
failureRedirect: '/signin',
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
]
}), users.signin);
app.get('/auth/google/callback', passport.authenticate('google', {
failureRedirect: '/signin'
}), users.authCallback);
//Finish with setting up the userId param
app.param('userId', users.user);
//Article Routes
var articles = require('../app/controllers/articles');
app.get('/articles', articles.all);
app.post('/articles', auth.requiresLogin, articles.create);
app.get('/articles/:articleId', articles.show);
app.put('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.update);
app.del('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.destroy);
//Finish with setting up the articleId param
app.param('articleId', articles.article);
//Home route
var index = require('../app/controllers/index');
app.get('/', index.render);
};