Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Update 0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Aug 2, 2014
1 parent f4b62ca commit f6e1909
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 156 deletions.
10 changes: 5 additions & 5 deletions app/controllers/articles.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports.create = function(req, res) {

article.save(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
Expand All @@ -43,7 +43,7 @@ exports.update = function(req, res) {

article.save(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
Expand All @@ -60,7 +60,7 @@ exports.delete = function(req, res) {

article.remove(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
Expand All @@ -75,7 +75,7 @@ exports.delete = function(req, res) {
exports.list = function(req, res) {
Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
Expand All @@ -101,7 +101,7 @@ exports.articleByID = function(req, res, next, id) {
*/
exports.hasAuthorization = function(req, res, next) {
if (req.article.user.id !== req.user.id) {
return res.send(403, {
return res.status(403).send({
message: 'User is not authorized'
});
}
Expand Down
18 changes: 14 additions & 4 deletions app/controllers/users.server.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
'use strict';

module.exports.authentication = require('./users/users.authentication');
module.exports.authorization = require('./users/users.authorization');
module.exports.password = require('./users/users.password');
module.exports.profile = require('./users/users.profile');
/**
* Module dependencies.
*/
var _ = require('lodash');

/**
* Extend user's controller
*/
module.exports = _.extend(
require('./users/users.authentication'),
require('./users/users.authorization'),
require('./users/users.password'),
require('./users/users.profile')
);
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports.signup = function(req, res) {
// Then save the user
user.save(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
Expand All @@ -37,7 +37,7 @@ exports.signup = function(req, res) {

req.login(user, function(err) {
if (err) {
res.send(400, err);
res.status(400).send(err);
} else {
res.jsonp(user);
}
Expand All @@ -52,15 +52,15 @@ exports.signup = function(req, res) {
exports.signin = function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err || !user) {
res.send(400, info);
res.status(400).send(info);
} else {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;

req.login(user, function(err) {
if (err) {
res.send(400, err);
res.status(400).send(err);
} else {
res.jsonp(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports.userByID = function(req, res, next, id) {
*/
exports.requiresLogin = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.send(401, {
return res.status(401).send({
message: 'User is not logged in'
});
}
Expand All @@ -45,7 +45,7 @@ exports.hasAuthorization = function(roles) {
if (_.intersection(req.user.roles, roles).length) {
return next();
} else {
return res.send(403, {
return res.status(403).send({
message: 'User is not authorized'
});
}
Expand Down
34 changes: 17 additions & 17 deletions app/controllers/users/users.password.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ exports.forgot = function(req, res, next) {
if (req.body.username) {
User.findOne({
username: req.body.username
}, function(err, user) {
}, '-salt -password', function(err, user) {
if (!user) {
return res.send(400, {
return res.status(400).send({
message: 'No account with that username has been found'
});
} else if (user.provider !== 'local') {
return res.send(400, {
return res.status(400).send({
message: 'It seems like you signed up using your ' + user.provider + ' account'
});
} else {
Expand All @@ -51,7 +51,7 @@ exports.forgot = function(req, res, next) {
}
});
} else {
return res.send(400, {
return res.status(400).send({
message: 'Username field must not be blank'
});
}
Expand All @@ -70,7 +70,7 @@ exports.forgot = function(req, res, next) {
var smtpTransport = nodemailer.createTransport(config.mailer.options);
var mailOptions = {
to: user.email,
from: config.mailer.fromEmail,
from: config.mailer.from,
subject: 'Password Reset',
html: emailHTML
};
Expand Down Expand Up @@ -129,13 +129,13 @@ exports.reset = function(req, res, next) {

user.save(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
res.status(400).send(err);
} else {
// Return authenticated user
res.jsonp(user);
Expand All @@ -146,12 +146,12 @@ exports.reset = function(req, res, next) {
}
});
} else {
return res.send(400, {
return res.status(400).send({
message: 'Passwords do not match'
});
}
} else {
return res.send(400, {
return res.status(400).send({
message: 'Password reset token is invalid or has expired.'
});
}
Expand All @@ -169,7 +169,7 @@ exports.reset = function(req, res, next) {
var smtpTransport = nodemailer.createTransport(config.mailer.options);
var mailOptions = {
to: user.email,
from: config.mailer.fromEmail,
from: config.mailer.from,
subject: 'Your password has been changed',
html: emailHTML
};
Expand Down Expand Up @@ -200,13 +200,13 @@ exports.changePassword = function(req, res, next) {

user.save(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
res.status(400).send(err);
} else {
res.send({
message: 'Password changed successfully'
Expand All @@ -216,28 +216,28 @@ exports.changePassword = function(req, res, next) {
}
});
} else {
res.send(400, {
res.status(400).send({
message: 'Passwords do not match'
});
}
} else {
res.send(400, {
res.status(400).send({
message: 'Current password is incorrect'
});
}
} else {
res.send(400, {
res.status(400).send({
message: 'User is not found'
});
}
});
} else {
res.send(400, {
res.status(400).send({
message: 'Please provide a new password'
});
}
} else {
res.send(400, {
res.status(400).send({
message: 'User is not signed in'
});
}
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/users/users.profile.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ exports.update = function(req, res) {

user.save(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
res.status(400).send(err);
} else {
res.jsonp(user);
}
});
}
});
} else {
res.send(400, {
res.status(400).send({
message: 'User is not signed in'
});
}
Expand Down Expand Up @@ -147,13 +147,13 @@ exports.removeOAuthProvider = function(req, res, next) {

user.save(function(err) {
if (err) {
return res.send(400, {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
req.login(user, function(err) {
if (err) {
res.send(400, err);
res.status(400).send(err);
} else {
res.jsonp(user);
}
Expand Down
6 changes: 3 additions & 3 deletions app/routes/articles.server.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module.exports = function(app) {
// Article Routes
app.route('/articles')
.get(articles.list)
.post(users.authorization.requiresLogin, articles.create);
.post(users.requiresLogin, articles.create);

app.route('/articles/:articleId')
.get(articles.read)
.put(users.authorization.requiresLogin, articles.hasAuthorization, articles.update)
.delete(users.authorization.requiresLogin, articles.hasAuthorization, articles.delete);
.put(users.requiresLogin, articles.hasAuthorization, articles.update)
.delete(users.requiresLogin, articles.hasAuthorization, articles.delete);

// Finish by binding the article middleware
app.param('articleId', articles.articleByID);
Expand Down
30 changes: 15 additions & 15 deletions app/routes/users.server.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ module.exports = function(app) {
var users = require('../../app/controllers/users');

// Setting up the users profile api
app.route('/users/me').get(users.profile.me);
app.route('/users').put(users.profile.update);
app.route('/users/accounts').delete(users.profile.removeOAuthProvider);
app.route('/users/me').get(users.me);
app.route('/users').put(users.update);
app.route('/users/accounts').delete(users.removeOAuthProvider);

// Setting up the users password api
app.route('/users/password').post(users.password.changePassword);
app.route('/auth/forgot').post(users.password.forgot);
app.route('/auth/reset/:token').get(users.password.validateResetToken);
app.route('/auth/reset/:token').post(users.password.reset);
app.route('/users/password').post(users.changePassword);
app.route('/auth/forgot').post(users.forgot);
app.route('/auth/reset/:token').get(users.validateResetToken);
app.route('/auth/reset/:token').post(users.reset);

// Setting up the users authentication api
app.route('/auth/signup').post(users.authentication.signup);
app.route('/auth/signin').post(users.authentication.signin);
app.route('/auth/signout').get(users.authentication.signout);
app.route('/auth/signup').post(users.signup);
app.route('/auth/signin').post(users.signin);
app.route('/auth/signout').get(users.signout);

// Setting the facebook oauth routes
app.route('/auth/facebook').get(passport.authenticate('facebook', {
scope: ['email']
}));
app.route('/auth/facebook/callback').get(users.authentication.oauthCallback('facebook'));
app.route('/auth/facebook/callback').get(users.oauthCallback('facebook'));

// Setting the twitter oauth routes
app.route('/auth/twitter').get(passport.authenticate('twitter'));
app.route('/auth/twitter/callback').get(users.authentication.oauthCallback('twitter'));
app.route('/auth/twitter/callback').get(users.oauthCallback('twitter'));

// Setting the google oauth routes
app.route('/auth/google').get(passport.authenticate('google', {
Expand All @@ -42,12 +42,12 @@ module.exports = function(app) {
'https://www.googleapis.com/auth/userinfo.email'
]
}));
app.route('/auth/google/callback').get(users.authentication.oauthCallback('google'));
app.route('/auth/google/callback').get(users.oauthCallback('google'));

// Setting the linkedin oauth routes
app.route('/auth/linkedin').get(passport.authenticate('linkedin'));
app.route('/auth/linkedin/callback').get(users.authentication.oauthCallback('linkedin'));
app.route('/auth/linkedin/callback').get(users.oauthCallback('linkedin'));

// Finish by binding the user middleware
app.param('userId', users.authorization.userByID);
app.param('userId', users.userByID);
};
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meanjs",
"version": "0.4.0",
"version": "0.3.2",
"description": "Fullstack JavaScript with MongoDB, Express, AngularJS, and Node.js.",
"dependencies": {
"bootstrap": "~3",
Expand Down
4 changes: 2 additions & 2 deletions config/env/development.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = {
db: 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean-dev',
db: 'mongodb://localhost/mean-dev',
app: {
title: 'MEAN.JS - Development Environment'
},
Expand All @@ -26,7 +26,7 @@ module.exports = {
callbackURL: 'http://localhost:3000/auth/linkedin/callback'
},
mailer: {
fromEmail: process.env.MAILER_FROM_EMAIL || 'MAILER_FROM_EMAIL',
from: process.env.MAILER_FROM || 'MAILER_FROM',
options: {
service: process.env.MAILER_SERVICE_PROVIDER || 'MAILER_SERVICE_PROVIDER',
auth: {
Expand Down
Loading

0 comments on commit f6e1909

Please sign in to comment.