diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000..1b84277 --- /dev/null +++ b/.bowerrc @@ -0,0 +1,3 @@ +{ + "directory": "public/lib" +} diff --git a/.csslintrc b/.csslintrc new file mode 100644 index 0000000..0dab227 --- /dev/null +++ b/.csslintrc @@ -0,0 +1,15 @@ +{ + "adjoining-classes": false, + "box-model": false, + "box-sizing": false, + "floats": false, + "font-sizes": false, + "important": false, + "known-properties": false, + "overqualified-elements": false, + "qualified-headings": false, + "regex-selectors": false, + "unique-headings": false, + "universal-selector": false, + "unqualified-attributes": false +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..636ceea --- /dev/null +++ b/.editorconfig @@ -0,0 +1,36 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# Howto with your editor: +# Sublime: https://github.com/sindresorhus/editorconfig-sublime + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[**] +end_of_line = lf +insert_final_newline = true + +# Standard at: https://github.com/felixge/node-style-guide +[**.js, **.json] +trim_trailing_whitespace = true +indent_style = tab +quote_type = single +curly_bracket_next_line = false +spaces_around_operators = true +space_after_control_statements = true +space_after_anonymous_functions = false +spaces_in_brackets = false + +# No Standard. Please document a standard if different from .js +[**.yml, **.html, **.css] +trim_trailing_whitespace = true +indent_style = tab + +# No standard. Please document a standard if different from .js +[**.md] +indent_style = tab + +# Standard at: +[Makefile] +indent_style = tab \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bca129d --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# iOS / Apple +# =========== +.DS_Store +ehthumbs.db +Icon? +Thumbs.db + +# Node and related ecosystem +# ========================== +.nodemonignore +.sass-cache/ +npm-debug.log +node_modules/ +public/lib/ +app/tests/coverage/ +.bower-*/ +.idea/ + +# MEAN.js app and assets +# ====================== +config/sslcerts/*.pem +access.log +public/dist/ + +# Sublime editor +# ============== +.sublime-project +*.sublime-project +*.sublime-workspace + +# Eclipse project files +# ===================== +.project +.settings/ +.*.md.html +.metadata +*~.nib +local.properties + +# IntelliJ +# ======== +*.iml + +# Cloud9 IDE +# ========= +.c9/ +data/ +mongod + +# General +# ======= +*.log +*.csv +*.dat +*.out +*.pid +*.gz +*.tmp +*.bak +*.swp +logs/ +build/ diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..4cd07cd --- /dev/null +++ b/.jshintrc @@ -0,0 +1,42 @@ +{ + "node": true, // Enable globals available when code is running inside of the NodeJS runtime environment. + "browser": true, // Standard browser globals e.g. `window`, `document`. + "esnext": true, // Allow ES.next specific features such as `const` and `let`. + "bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.). + "camelcase": false, // Permit only camelcase for `var` and `object indexes`. + "curly": false, // Require {} for every new block or scope. + "eqeqeq": true, // Require triple equals i.e. `===`. + "immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` + "latedef": true, // Prohibit variable use before definition. + "newcap": true, // Require capitalization of all constructor functions e.g. `new F()`. + "noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`. + "quotmark": "single", // Define quotes to string values. + "regexp": true, // Prohibit `.` and `[^...]` in regular expressions. + "undef": true, // Require all non-global variables be declared before they are used. + "unused": false, // Warn unused variables. + "strict": true, // Require `use strict` pragma in every file. + "trailing": true, // Prohibit trailing whitespaces. + "smarttabs": false, // Suppresses warnings about mixed tabs and spaces + "globals": { // Globals variables. + "jasmine": true, + "angular": true, + "ApplicationConfiguration": true + }, + "predef": [ // Extra globals. + "define", + "require", + "exports", + "module", + "describe", + "before", + "beforeEach", + "after", + "afterEach", + "it", + "inject", + "expect" + ], + "indent": 4, // Specify indentation spacing + "devel": true, // Allow development statements e.g. `console.log();`. + "noempty": true // Prohibit use of empty blocks. +} \ No newline at end of file diff --git a/.slugignore b/.slugignore new file mode 100644 index 0000000..e4e50ba --- /dev/null +++ b/.slugignore @@ -0,0 +1 @@ +/app/tests \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..708607e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: node_js +node_js: + - "0.10" + - "0.11" +env: + - NODE_ENV=travis +services: + - mongodb diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..96dc98b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM dockerfile/nodejs + +MAINTAINER Matthias Luebken, matthias@catalyst-zero.com + +WORKDIR /home/mean + +# Install Mean.JS Prerequisites +RUN npm install -g grunt-cli +RUN npm install -g bower + +# Install Mean.JS packages +ADD package.json /home/mean/package.json +RUN npm install + +# Manually trigger bower. Why doesnt this work via npm install? +ADD .bowerrc /home/mean/.bowerrc +ADD bower.json /home/mean/bower.json +RUN bower install --config.interactive=false --allow-root + +# Make everything available for start +ADD . /home/mean + +# currently only works for development +ENV NODE_ENV development + +# Port 3000 for server +# Port 35729 for livereload +EXPOSE 3000 35729 +CMD ["grunt"] \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..b366c5d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +## License +(The MIT License) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Procfile b/Procfile new file mode 100755 index 0000000..3360097 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: ./node_modules/.bin/forever -m 5 server.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..4a1500e --- /dev/null +++ b/README.md @@ -0,0 +1,226 @@ +# Sample MeanJS application on Scalingo + +https://sample-node-meanjs.scalingo.io + +## Modifications with standard distribution: + +config/env/production.js: Read `MONGO_URL` environment variable + +```js +module.exports = { + db: { + uri: process.env.MONGO_URL || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean', + options: { + user: '', + pass: '' + } + }, + // ... +} +``` + +config/env/all.js: Read `PORT` environment variable. + +```js +module.exports = { + app: { + // ... + }, + port: process.env.PORT || 3000, + // ... +} +``` + +## Scalingo configuration + +You can use our CLI: http://cli.scalingo.com +Or the web dashboard to achieve these operations: https://my.scalingo.com + +### Set environment to production + +``` +scalingo -a your-app-name env-set NODE_ENV=production +``` + +### Allocating a MongoDB Addon. + +``` +scalingo -a your-app-name addons-add scalingo-mongodb free +-----> Addon scalingo-mongodb has been provisionned + ID: your-app-name + Modified variables: [MONGO_URL SCALINGO_MONGO_URL] + Message from addon provider: Database successfully created +``` + +# MeanJS Readme + +[![MEAN.JS Logo](http://meanjs.org/img/logo-small.png)](http://meanjs.org/) + +[![Build Status](https://travis-ci.org/meanjs/mean.svg?branch=master)](https://travis-ci.org/meanjs/mean) +[![Dependencies Status](https://david-dm.org/meanjs/mean.svg)](https://david-dm.org/meanjs/mean) +[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/meanjs/mean?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +MEAN.JS is a full-stack JavaScript open-source solution, which provides a solid starting point for [MongoDB](http://www.mongodb.org/), [Node.js](http://www.nodejs.org/), [Express](http://expressjs.com/), and [AngularJS](http://angularjs.org/) based applications. The idea is to solve the common issues with connecting those frameworks, build a robust framework to support daily development needs, and help developers use better practices while working with popular JavaScript components. + +## Before You Begin +Before you begin we recommend you read about the basic building blocks that assemble a MEAN.JS application: +* MongoDB - Go through [MongoDB Official Website](http://mongodb.org/) and proceed to their [Official Manual](http://docs.mongodb.org/manual/), which should help you understand NoSQL and MongoDB better. +* Express - The best way to understand express is through its [Official Website](http://expressjs.com/), which has a [Getting Started](http://expressjs.com/starter/installing.html) guide, as well as an [ExpressJS Guide](http://expressjs.com/guide/error-handling.html) guide for general express topics. You can also go through this [StackOverflow Thread](http://stackoverflow.com/questions/8144214/learning-express-for-node-js) for more resources. +* AngularJS - Angular's [Official Website](http://angularjs.org/) is a great starting point. You can also use [Thinkster Popular Guide](http://www.thinkster.io/), and the [Egghead Videos](https://egghead.io/). +* Node.js - Start by going through [Node.js Official Website](http://nodejs.org/) and this [StackOverflow Thread](http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js), which should get you going with the Node.js platform in no time. + + +## Prerequisites +Make sure you have installed all of the following prerequisites on your development machine: +* Node.js - [Download & Install Node.js](http://www.nodejs.org/download/) and the npm package manager. If you encounter any problems, you can also use this [GitHub Gist](https://gist.github.com/isaacs/579814) to install Node.js. +* MongoDB - [Download & Install MongoDB](http://www.mongodb.org/downloads), and make sure it's running on the default port (27017). +* Bower - You're going to use the [Bower Package Manager](http://bower.io/) to manage your front-end packages. Make sure you've installed Node.js and npm first, then install bower globally using npm: + +```bash +$ npm install -g bower +``` + +* Grunt - You're going to use the [Grunt Task Runner](http://gruntjs.com/) to automate your development process. Make sure you've installed Node.js and npm first, then install grunt globally using npm: + +```bash +$ npm install -g grunt-cli +``` + +## Downloading MEAN.JS +There are several ways you can get the MEAN.JS boilerplate: + +### Yo Generator +The recommended way would be to use the [Official Yo Generator](http://meanjs.org/generator.html), which generates the latest stable copy of the MEAN.JS boilerplate and supplies multiple sub-generators to ease your daily development cycles. + +### Cloning The GitHub Repository +You can also use Git to directly clone the MEAN.JS repository: +```bash +$ git clone https://github.com/meanjs/mean.git meanjs +``` +This will clone the latest version of the MEAN.JS repository to a **meanjs** folder. + +### Downloading The Repository Zip File +Another way to use the MEAN.JS boilerplate is to download a zip copy from the [master branch on GitHub](https://github.com/meanjs/mean/archive/master.zip). You can also do this using `wget` command: +```bash +$ wget https://github.com/meanjs/mean/archive/master.zip -O meanjs.zip; unzip meanjs.zip; rm meanjs.zip +``` +Don't forget to rename **mean-master** after your project name. + +## Quick Install +Once you've downloaded the boilerplate and installed all the prerequisites, you're just a few steps away from starting to develop your MEAN application. + +The first thing you should do is install the Node.js dependencies. The boilerplate comes pre-bundled with a package.json file that contains the list of modules you need to start your application. To learn more about the modules installed visit the NPM & Package.json section. + +To install Node.js dependencies you're going to use npm again. In the application folder run this in the command-line: + +```bash +$ npm install +``` + +This command does a few things: +* First it will install the dependencies needed for the application to run. +* If you're running in a development environment, it will then also install development dependencies needed for testing and running your application. +* Finally, when the install process is over, npm will initiate a bower install command to install all the front-end modules needed for the application. + +## Running Your Application +After the install process is over, you'll be able to run your application using Grunt. Just run grunt default task: + +```bash +$ grunt +``` + +Your application should run on port 3000, so in your browser just go to [http://localhost:3000](http://localhost:3000) + +That's it! Your application should be running. To proceed with your development, check the other sections in this documentation. +If you encounter any problems, try the Troubleshooting section. + +## Testing Your Application +You can run the full test suite included with MEAN.JS with the test task: + +``` +$ grunt test +``` + +This will run both the server-side tests (located in the app/tests/ directory) and the client-side tests (located in the public/modules/*/tests/). + +To execute only the server tests, run the test:server task: + +``` +$ grunt test:server +``` + +And to run only the client tests, run the test:client task: + +``` +$ grunt test:client +``` + +## Development and deployment With Docker + +* Install [Docker](http://www.docker.com/) +* Install [Fig](https://github.com/orchardup/fig) + +* Local development and testing with fig: +```bash +$ fig up +``` + +* Local development and testing with just Docker: +```bash +$ docker build -t mean . +$ docker run -p 27017:27017 -d --name db mongo +$ docker run -p 3000:3000 --link db:db_1 mean +$ +``` + +* To enable live reload, forward port 35729 and mount /app and /public as volumes: +```bash +$ docker run -p 3000:3000 -p 35729:35729 -v /Users/mdl/workspace/mean-stack/mean/public:/home/mean/public -v /Users/mdl/workspace/mean-stack/mean/app:/home/mean/app --link db:db_1 mean +``` + +## Running in a secure environment +To run your application in a secure manner you'll need to use OpenSSL and generate a set of self-signed certificates. Unix-based users can use the following command: +```bash +$ sh ./scripts/generate-ssl-certs.sh +``` +Windows users can follow instructions found [here](http://www.websense.com/support/article/kbarticle/How-to-use-OpenSSL-and-Microsoft-Certification-Authority). +After you've generated the key and certificate, place them in the *config/sslcerts* folder. + +## Getting Started With MEAN.JS +You have your application running, but there is a lot of stuff to understand. We recommend you go over the [Official Documentation](http://meanjs.org/docs.html). +In the docs we'll try to explain both general concepts of MEAN components and give you some guidelines to help you improve your development process. We tried covering as many aspects as possible, and will keep it updated by your request. You can also help us develop and improve the documentation by checking out the *gh-pages* branch of this repository. + +## Community +* Use the [Official Website](http://meanjs.org) to learn about changes and the roadmap. +* Join #meanjs on freenode. +* Discuss it in the new [Google Group](https://groups.google.com/d/forum/meanjs) +* Ping us on [Twitter](http://twitter.com/meanjsorg) and [Facebook](http://facebook.com/meanjs) + +## Live Example +Browse the live MEAN.JS example on [http://meanjs.herokuapp.com](http://meanjs.herokuapp.com). + +## Credits +Inspired by the great work of [Madhusudhan Srinivasa](https://github.com/madhums/) +The MEAN name was coined by [Valeri Karpov](http://blog.mongodb.org/post/49262866911/the-mean-stack-mongodb-expressjs-angularjs-and) + +## License +(The MIT License) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/app/controllers/articles.server.controller.js b/app/controllers/articles.server.controller.js new file mode 100644 index 0000000..8eb757b --- /dev/null +++ b/app/controllers/articles.server.controller.js @@ -0,0 +1,120 @@ +'use strict'; + +/** + * Module dependencies. + */ +var mongoose = require('mongoose'), + errorHandler = require('./errors.server.controller'), + Article = mongoose.model('Article'), + _ = require('lodash'); + +/** + * Create a article + */ +exports.create = function(req, res) { + var article = new Article(req.body); + article.user = req.user; + + article.save(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + res.json(article); + } + }); +}; + +/** + * Show the current article + */ +exports.read = function(req, res) { + res.json(req.article); +}; + +/** + * Update a article + */ +exports.update = function(req, res) { + var article = req.article; + + article = _.extend(article, req.body); + + article.save(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + res.json(article); + } + }); +}; + +/** + * Delete an article + */ +exports.delete = function(req, res) { + var article = req.article; + + article.remove(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + res.json(article); + } + }); +}; + +/** + * List of Articles + */ +exports.list = function(req, res) { + Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + res.json(articles); + } + }); +}; + +/** + * Article middleware + */ +exports.articleByID = function(req, res, next, id) { + + if (!mongoose.Types.ObjectId.isValid(id)) { + return res.status(400).send({ + message: 'Article is invalid' + }); + } + + Article.findById(id).populate('user', 'displayName').exec(function(err, article) { + if (err) return next(err); + if (!article) { + return res.status(404).send({ + message: 'Article not found' + }); + } + req.article = article; + next(); + }); +}; + +/** + * Article authorization middleware + */ +exports.hasAuthorization = function(req, res, next) { + if (req.article.user.id !== req.user.id) { + return res.status(403).send({ + message: 'User is not authorized' + }); + } + next(); +}; \ No newline at end of file diff --git a/app/controllers/core.server.controller.js b/app/controllers/core.server.controller.js new file mode 100644 index 0000000..f2af8e1 --- /dev/null +++ b/app/controllers/core.server.controller.js @@ -0,0 +1,11 @@ +'use strict'; + +/** + * Module dependencies. + */ +exports.index = function(req, res) { + res.render('index', { + user: req.user || null, + request: req + }); +}; \ No newline at end of file diff --git a/app/controllers/errors.server.controller.js b/app/controllers/errors.server.controller.js new file mode 100644 index 0000000..41078b4 --- /dev/null +++ b/app/controllers/errors.server.controller.js @@ -0,0 +1,42 @@ +'use strict'; + +/** + * Get unique error field name + */ +var getUniqueErrorMessage = function(err) { + var output; + + try { + var fieldName = err.err.substring(err.err.lastIndexOf('.$') + 2, err.err.lastIndexOf('_1')); + output = fieldName.charAt(0).toUpperCase() + fieldName.slice(1) + ' already exists'; + + } catch (ex) { + output = 'Unique field already exists'; + } + + return output; +}; + +/** + * Get the error message from error object + */ +exports.getErrorMessage = function(err) { + var message = ''; + + if (err.code) { + switch (err.code) { + case 11000: + case 11001: + message = getUniqueErrorMessage(err); + break; + default: + message = 'Something went wrong'; + } + } else { + for (var errName in err.errors) { + if (err.errors[errName].message) message = err.errors[errName].message; + } + } + + return message; +}; \ No newline at end of file diff --git a/app/controllers/users.server.controller.js b/app/controllers/users.server.controller.js new file mode 100755 index 0000000..64e772e --- /dev/null +++ b/app/controllers/users.server.controller.js @@ -0,0 +1,16 @@ +'use strict'; + +/** + * Module dependencies. + */ +var _ = require('lodash'); + +/** + * Extend user's controller + */ +module.exports = _.extend( + require('./users/users.authentication.server.controller'), + require('./users/users.authorization.server.controller'), + require('./users/users.password.server.controller'), + require('./users/users.profile.server.controller') +); \ No newline at end of file diff --git a/app/controllers/users/users.authentication.server.controller.js b/app/controllers/users/users.authentication.server.controller.js new file mode 100644 index 0000000..c15c8a1 --- /dev/null +++ b/app/controllers/users/users.authentication.server.controller.js @@ -0,0 +1,206 @@ +'use strict'; + +/** + * Module dependencies. + */ +var _ = require('lodash'), + errorHandler = require('../errors.server.controller'), + mongoose = require('mongoose'), + passport = require('passport'), + User = mongoose.model('User'); + +/** + * Signup + */ +exports.signup = function(req, res) { + // For security measurement we remove the roles from the req.body object + delete req.body.roles; + + // Init Variables + var user = new User(req.body); + var message = null; + + // Add missing user fields + user.provider = 'local'; + user.displayName = user.firstName + ' ' + user.lastName; + + // Then save the user + user.save(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + // Remove sensitive data before login + user.password = undefined; + user.salt = undefined; + + req.login(user, function(err) { + if (err) { + res.status(400).send(err); + } else { + res.json(user); + } + }); + } + }); +}; + +/** + * Signin after passport authentication + */ +exports.signin = function(req, res, next) { + passport.authenticate('local', function(err, user, info) { + if (err || !user) { + 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.status(400).send(err); + } else { + res.json(user); + } + }); + } + })(req, res, next); +}; + +/** + * Signout + */ +exports.signout = function(req, res) { + req.logout(); + res.redirect('/'); +}; + +/** + * OAuth callback + */ +exports.oauthCallback = function(strategy) { + return function(req, res, next) { + passport.authenticate(strategy, function(err, user, redirectURL) { + if (err || !user) { + return res.redirect('/#!/signin'); + } + req.login(user, function(err) { + if (err) { + return res.redirect('/#!/signin'); + } + + return res.redirect(redirectURL || '/'); + }); + })(req, res, next); + }; +}; + +/** + * Helper function to save or update a OAuth user profile + */ +exports.saveOAuthUserProfile = function(req, providerUserProfile, done) { + if (!req.user) { + // Define a search query fields + var searchMainProviderIdentifierField = 'providerData.' + providerUserProfile.providerIdentifierField; + var searchAdditionalProviderIdentifierField = 'additionalProvidersData.' + providerUserProfile.provider + '.' + providerUserProfile.providerIdentifierField; + + // Define main provider search query + var mainProviderSearchQuery = {}; + mainProviderSearchQuery.provider = providerUserProfile.provider; + mainProviderSearchQuery[searchMainProviderIdentifierField] = providerUserProfile.providerData[providerUserProfile.providerIdentifierField]; + + // Define additional provider search query + var additionalProviderSearchQuery = {}; + additionalProviderSearchQuery[searchAdditionalProviderIdentifierField] = providerUserProfile.providerData[providerUserProfile.providerIdentifierField]; + + // Define a search query to find existing user with current provider profile + var searchQuery = { + $or: [mainProviderSearchQuery, additionalProviderSearchQuery] + }; + + User.findOne(searchQuery, function(err, user) { + if (err) { + return done(err); + } else { + if (!user) { + var possibleUsername = providerUserProfile.username || ((providerUserProfile.email) ? providerUserProfile.email.split('@')[0] : ''); + + User.findUniqueUsername(possibleUsername, null, function(availableUsername) { + user = new User({ + firstName: providerUserProfile.firstName, + lastName: providerUserProfile.lastName, + username: availableUsername, + displayName: providerUserProfile.displayName, + email: providerUserProfile.email, + provider: providerUserProfile.provider, + providerData: providerUserProfile.providerData + }); + + // And save the user + user.save(function(err) { + return done(err, user); + }); + }); + } else { + return done(err, user); + } + } + }); + } else { + // User is already logged in, join the provider data to the existing user + var user = req.user; + + // Check if user exists, is not signed in using this provider, and doesn't have that provider data already configured + if (user.provider !== providerUserProfile.provider && (!user.additionalProvidersData || !user.additionalProvidersData[providerUserProfile.provider])) { + // Add the provider data to the additional provider data field + if (!user.additionalProvidersData) user.additionalProvidersData = {}; + user.additionalProvidersData[providerUserProfile.provider] = providerUserProfile.providerData; + + // Then tell mongoose that we've updated the additionalProvidersData field + user.markModified('additionalProvidersData'); + + // And save the user + user.save(function(err) { + return done(err, user, '/#!/settings/accounts'); + }); + } else { + return done(new Error('User is already connected using this provider'), user); + } + } +}; + +/** + * Remove OAuth provider + */ +exports.removeOAuthProvider = function(req, res, next) { + var user = req.user; + var provider = req.param('provider'); + + if (user && provider) { + // Delete the additional provider + if (user.additionalProvidersData[provider]) { + delete user.additionalProvidersData[provider]; + + // Then tell mongoose that we've updated the additionalProvidersData field + user.markModified('additionalProvidersData'); + } + + user.save(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + req.login(user, function(err) { + if (err) { + res.status(400).send(err); + } else { + res.json(user); + } + }); + } + }); + } +}; \ No newline at end of file diff --git a/app/controllers/users/users.authorization.server.controller.js b/app/controllers/users/users.authorization.server.controller.js new file mode 100644 index 0000000..932e490 --- /dev/null +++ b/app/controllers/users/users.authorization.server.controller.js @@ -0,0 +1,52 @@ +'use strict'; + +/** + * Module dependencies. + */ +var _ = require('lodash'), + mongoose = require('mongoose'), + User = mongoose.model('User'); + +/** + * User middleware + */ +exports.userByID = function(req, res, next, id) { + User.findById(id).exec(function(err, user) { + if (err) return next(err); + if (!user) return next(new Error('Failed to load User ' + id)); + req.profile = user; + next(); + }); +}; + +/** + * Require login routing middleware + */ +exports.requiresLogin = function(req, res, next) { + if (!req.isAuthenticated()) { + return res.status(401).send({ + message: 'User is not logged in' + }); + } + + next(); +}; + +/** + * User authorizations routing middleware + */ +exports.hasAuthorization = function(roles) { + var _this = this; + + return function(req, res, next) { + _this.requiresLogin(req, res, function() { + if (_.intersection(req.user.roles, roles).length) { + return next(); + } else { + return res.status(403).send({ + message: 'User is not authorized' + }); + } + }); + }; +}; diff --git a/app/controllers/users/users.password.server.controller.js b/app/controllers/users/users.password.server.controller.js new file mode 100644 index 0000000..63c20b5 --- /dev/null +++ b/app/controllers/users/users.password.server.controller.js @@ -0,0 +1,249 @@ +'use strict'; + +/** + * Module dependencies. + */ +var _ = require('lodash'), + errorHandler = require('../errors.server.controller'), + mongoose = require('mongoose'), + passport = require('passport'), + User = mongoose.model('User'), + config = require('../../../config/config'), + nodemailer = require('nodemailer'), + async = require('async'), + crypto = require('crypto'); + +var smtpTransport = nodemailer.createTransport(config.mailer.options); + +/** + * Forgot for reset password (forgot POST) + */ +exports.forgot = function(req, res, next) { + async.waterfall([ + // Generate random token + function(done) { + crypto.randomBytes(20, function(err, buffer) { + var token = buffer.toString('hex'); + done(err, token); + }); + }, + // Lookup user by username + function(token, done) { + if (req.body.username) { + User.findOne({ + username: req.body.username + }, '-salt -password', function(err, user) { + if (!user) { + return res.status(400).send({ + message: 'No account with that username has been found' + }); + } else if (user.provider !== 'local') { + return res.status(400).send({ + message: 'It seems like you signed up using your ' + user.provider + ' account' + }); + } else { + user.resetPasswordToken = token; + user.resetPasswordExpires = Date.now() + 3600000; // 1 hour + + user.save(function(err) { + done(err, token, user); + }); + } + }); + } else { + return res.status(400).send({ + message: 'Username field must not be blank' + }); + } + }, + function(token, user, done) { + res.render('templates/reset-password-email', { + name: user.displayName, + appName: config.app.title, + url: 'http://' + req.headers.host + '/auth/reset/' + token + }, function(err, emailHTML) { + done(err, emailHTML, user); + }); + }, + // If valid email, send reset email using service + function(emailHTML, user, done) { + var mailOptions = { + to: user.email, + from: config.mailer.from, + subject: 'Password Reset', + html: emailHTML + }; + smtpTransport.sendMail(mailOptions, function(err) { + if (!err) { + res.send({ + message: 'An email has been sent to ' + user.email + ' with further instructions.' + }); + } else { + return res.status(400).send({ + message: 'Failure sending email' + }); + } + + done(err); + }); + } + ], function(err) { + if (err) return next(err); + }); +}; + +/** + * Reset password GET from email token + */ +exports.validateResetToken = function(req, res) { + User.findOne({ + resetPasswordToken: req.params.token, + resetPasswordExpires: { + $gt: Date.now() + } + }, function(err, user) { + if (!user) { + return res.redirect('/#!/password/reset/invalid'); + } + + res.redirect('/#!/password/reset/' + req.params.token); + }); +}; + +/** + * Reset password POST from email token + */ +exports.reset = function(req, res, next) { + // Init Variables + var passwordDetails = req.body; + + async.waterfall([ + + function(done) { + User.findOne({ + resetPasswordToken: req.params.token, + resetPasswordExpires: { + $gt: Date.now() + } + }, function(err, user) { + if (!err && user) { + if (passwordDetails.newPassword === passwordDetails.verifyPassword) { + user.password = passwordDetails.newPassword; + user.resetPasswordToken = undefined; + user.resetPasswordExpires = undefined; + + user.save(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + req.login(user, function(err) { + if (err) { + res.status(400).send(err); + } else { + // Return authenticated user + res.json(user); + + done(err, user); + } + }); + } + }); + } else { + return res.status(400).send({ + message: 'Passwords do not match' + }); + } + } else { + return res.status(400).send({ + message: 'Password reset token is invalid or has expired.' + }); + } + }); + }, + function(user, done) { + res.render('templates/reset-password-confirm-email', { + name: user.displayName, + appName: config.app.title + }, function(err, emailHTML) { + done(err, emailHTML, user); + }); + }, + // If valid email, send reset email using service + function(emailHTML, user, done) { + var mailOptions = { + to: user.email, + from: config.mailer.from, + subject: 'Your password has been changed', + html: emailHTML + }; + + smtpTransport.sendMail(mailOptions, function(err) { + done(err, 'done'); + }); + } + ], function(err) { + if (err) return next(err); + }); +}; + +/** + * Change Password + */ +exports.changePassword = function(req, res) { + // Init Variables + var passwordDetails = req.body; + + if (req.user) { + if (passwordDetails.newPassword) { + User.findById(req.user.id, function(err, user) { + if (!err && user) { + if (user.authenticate(passwordDetails.currentPassword)) { + if (passwordDetails.newPassword === passwordDetails.verifyPassword) { + user.password = passwordDetails.newPassword; + + user.save(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + req.login(user, function(err) { + if (err) { + res.status(400).send(err); + } else { + res.send({ + message: 'Password changed successfully' + }); + } + }); + } + }); + } else { + res.status(400).send({ + message: 'Passwords do not match' + }); + } + } else { + res.status(400).send({ + message: 'Current password is incorrect' + }); + } + } else { + res.status(400).send({ + message: 'User is not found' + }); + } + }); + } else { + res.status(400).send({ + message: 'Please provide a new password' + }); + } + } else { + res.status(400).send({ + message: 'User is not signed in' + }); + } +}; diff --git a/app/controllers/users/users.profile.server.controller.js b/app/controllers/users/users.profile.server.controller.js new file mode 100644 index 0000000..dd38936 --- /dev/null +++ b/app/controllers/users/users.profile.server.controller.js @@ -0,0 +1,56 @@ +'use strict'; + +/** + * Module dependencies. + */ +var _ = require('lodash'), + errorHandler = require('../errors.server.controller.js'), + mongoose = require('mongoose'), + passport = require('passport'), + User = mongoose.model('User'); + +/** + * Update user details + */ +exports.update = function(req, res) { + // Init Variables + var user = req.user; + var message = null; + + // For security measurement we remove the roles from the req.body object + delete req.body.roles; + + if (user) { + // Merge existing user + user = _.extend(user, req.body); + user.updated = Date.now(); + user.displayName = user.firstName + ' ' + user.lastName; + + user.save(function(err) { + if (err) { + return res.status(400).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + req.login(user, function(err) { + if (err) { + res.status(400).send(err); + } else { + res.json(user); + } + }); + } + }); + } else { + res.status(400).send({ + message: 'User is not signed in' + }); + } +}; + +/** + * Send User + */ +exports.me = function(req, res) { + res.json(req.user || null); +}; \ No newline at end of file diff --git a/app/models/article.server.model.js b/app/models/article.server.model.js new file mode 100644 index 0000000..3f6fd0d --- /dev/null +++ b/app/models/article.server.model.js @@ -0,0 +1,34 @@ +'use strict'; + +/** + * Module dependencies. + */ +var mongoose = require('mongoose'), + Schema = mongoose.Schema; + +/** + * Article Schema + */ +var ArticleSchema = new Schema({ + created: { + type: Date, + default: Date.now + }, + title: { + type: String, + default: '', + trim: true, + required: 'Title cannot be blank' + }, + content: { + type: String, + default: '', + trim: true + }, + user: { + type: Schema.ObjectId, + ref: 'User' + } +}); + +mongoose.model('Article', ArticleSchema); \ No newline at end of file diff --git a/app/models/user.server.model.js b/app/models/user.server.model.js new file mode 100755 index 0000000..840464a --- /dev/null +++ b/app/models/user.server.model.js @@ -0,0 +1,146 @@ +'use strict'; + +/** + * Module dependencies. + */ +var mongoose = require('mongoose'), + Schema = mongoose.Schema, + crypto = require('crypto'); + +/** + * A Validation function for local strategy properties + */ +var validateLocalStrategyProperty = function(property) { + return ((this.provider !== 'local' && !this.updated) || property.length); +}; + +/** + * A Validation function for local strategy password + */ +var validateLocalStrategyPassword = function(password) { + return (this.provider !== 'local' || (password && password.length > 6)); +}; + +/** + * User Schema + */ +var UserSchema = new Schema({ + firstName: { + type: String, + trim: true, + default: '', + validate: [validateLocalStrategyProperty, 'Please fill in your first name'] + }, + lastName: { + type: String, + trim: true, + default: '', + validate: [validateLocalStrategyProperty, 'Please fill in your last name'] + }, + displayName: { + type: String, + trim: true + }, + email: { + type: String, + trim: true, + default: '', + validate: [validateLocalStrategyProperty, 'Please fill in your email'], + match: [/.+\@.+\..+/, 'Please fill a valid email address'] + }, + username: { + type: String, + unique: 'Username already exists', + required: 'Please fill in a username', + trim: true + }, + password: { + type: String, + default: '', + validate: [validateLocalStrategyPassword, 'Password should be longer'] + }, + salt: { + type: String + }, + provider: { + type: String, + required: 'Provider is required' + }, + providerData: {}, + additionalProvidersData: {}, + roles: { + type: [{ + type: String, + enum: ['user', 'admin'] + }], + default: ['user'] + }, + updated: { + type: Date + }, + created: { + type: Date, + default: Date.now + }, + /* For reset password */ + resetPasswordToken: { + type: String + }, + resetPasswordExpires: { + type: Date + } +}); + +/** + * Hook a pre save method to hash the password + */ +UserSchema.pre('save', function(next) { + if (this.password && this.password.length > 6) { + this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64'); + this.password = this.hashPassword(this.password); + } + + next(); +}); + +/** + * Create instance method for hashing a password + */ +UserSchema.methods.hashPassword = function(password) { + if (this.salt && password) { + return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64'); + } else { + return password; + } +}; + +/** + * Create instance method for authenticating user + */ +UserSchema.methods.authenticate = function(password) { + return this.password === this.hashPassword(password); +}; + +/** + * Find possible not used username + */ +UserSchema.statics.findUniqueUsername = function(username, suffix, callback) { + var _this = this; + var possibleUsername = username + (suffix || ''); + + _this.findOne({ + username: possibleUsername + }, function(err, user) { + if (!err) { + if (!user) { + callback(possibleUsername); + } else { + return _this.findUniqueUsername(username, (suffix || 0) + 1, callback); + } + } else { + callback(null); + } + }); +}; + +mongoose.model('User', UserSchema); \ No newline at end of file diff --git a/app/routes/articles.server.routes.js b/app/routes/articles.server.routes.js new file mode 100644 index 0000000..7d840e6 --- /dev/null +++ b/app/routes/articles.server.routes.js @@ -0,0 +1,22 @@ +'use strict'; + +/** + * Module dependencies. + */ +var users = require('../../app/controllers/users.server.controller'), + articles = require('../../app/controllers/articles.server.controller'); + +module.exports = function(app) { + // Article Routes + app.route('/articles') + .get(articles.list) + .post(users.requiresLogin, articles.create); + + app.route('/articles/:articleId') + .get(articles.read) + .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); +}; \ No newline at end of file diff --git a/app/routes/core.server.routes.js b/app/routes/core.server.routes.js new file mode 100644 index 0000000..1db9d40 --- /dev/null +++ b/app/routes/core.server.routes.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = function(app) { + // Root routing + var core = require('../../app/controllers/core.server.controller'); + app.route('/').get(core.index); +}; \ No newline at end of file diff --git a/app/routes/users.server.routes.js b/app/routes/users.server.routes.js new file mode 100644 index 0000000..3120e9a --- /dev/null +++ b/app/routes/users.server.routes.js @@ -0,0 +1,57 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'); + +module.exports = function(app) { + // User Routes + var users = require('../../app/controllers/users.server.controller'); + + // Setting up the users profile api + 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.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.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.oauthCallback('facebook')); + + // Setting the twitter oauth routes + app.route('/auth/twitter').get(passport.authenticate('twitter')); + app.route('/auth/twitter/callback').get(users.oauthCallback('twitter')); + + // Setting the google oauth routes + app.route('/auth/google').get(passport.authenticate('google', { + scope: [ + 'https://www.googleapis.com/auth/userinfo.profile', + 'https://www.googleapis.com/auth/userinfo.email' + ] + })); + 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.oauthCallback('linkedin')); + + // Setting the github oauth routes + app.route('/auth/github').get(passport.authenticate('github')); + app.route('/auth/github/callback').get(users.oauthCallback('github')); + + // Finish by binding the user middleware + app.param('userId', users.userByID); +}; \ No newline at end of file diff --git a/app/tests/article.server.model.test.js b/app/tests/article.server.model.test.js new file mode 100644 index 0000000..9ced1d4 --- /dev/null +++ b/app/tests/article.server.model.test.js @@ -0,0 +1,64 @@ +'use strict'; + +/** + * Module dependencies. + */ +var should = require('should'), + mongoose = require('mongoose'), + User = mongoose.model('User'), + Article = mongoose.model('Article'); + +/** + * Globals + */ +var user, article; + +/** + * Unit tests + */ +describe('Article Model Unit Tests:', function() { + beforeEach(function(done) { + user = new User({ + firstName: 'Full', + lastName: 'Name', + displayName: 'Full Name', + email: 'test@test.com', + username: 'username', + password: 'password' + }); + + user.save(function() { + article = new Article({ + title: 'Article Title', + content: 'Article Content', + user: user + }); + + done(); + }); + }); + + describe('Method Save', function() { + it('should be able to save without problems', function(done) { + return article.save(function(err) { + should.not.exist(err); + done(); + }); + }); + + it('should be able to show an error when try to save without title', function(done) { + article.title = ''; + + return article.save(function(err) { + should.exist(err); + done(); + }); + }); + }); + + afterEach(function(done) { + Article.remove().exec(function() { + User.remove().exec(done); + }); + }); +}); diff --git a/app/tests/article.server.routes.test.js b/app/tests/article.server.routes.test.js new file mode 100644 index 0000000..55716a1 --- /dev/null +++ b/app/tests/article.server.routes.test.js @@ -0,0 +1,280 @@ +'use strict'; + +var should = require('should'), + request = require('supertest'), + app = require('../../server'), + mongoose = require('mongoose'), + User = mongoose.model('User'), + Article = mongoose.model('Article'), + agent = request.agent(app); + +/** + * Globals + */ +var credentials, user, article; + +/** + * Article routes tests + */ +describe('Article CRUD tests', function() { + beforeEach(function(done) { + // Create user credentials + credentials = { + username: 'username', + password: 'password' + }; + + // Create a new user + user = new User({ + firstName: 'Full', + lastName: 'Name', + displayName: 'Full Name', + email: 'test@test.com', + username: credentials.username, + password: credentials.password, + provider: 'local' + }); + + // Save a user to the test db and create new article + user.save(function() { + article = { + title: 'Article Title', + content: 'Article Content' + }; + + done(); + }); + }); + + it('should be able to save an article if logged in', function(done) { + agent.post('/auth/signin') + .send(credentials) + .expect(200) + .end(function(signinErr, signinRes) { + // Handle signin error + if (signinErr) done(signinErr); + + // Get the userId + var userId = user.id; + + // Save a new article + agent.post('/articles') + .send(article) + .expect(200) + .end(function(articleSaveErr, articleSaveRes) { + // Handle article save error + if (articleSaveErr) done(articleSaveErr); + + // Get a list of articles + agent.get('/articles') + .end(function(articlesGetErr, articlesGetRes) { + // Handle article save error + if (articlesGetErr) done(articlesGetErr); + + // Get articles list + var articles = articlesGetRes.body; + + // Set assertions + (articles[0].user._id).should.equal(userId); + (articles[0].title).should.match('Article Title'); + + // Call the assertion callback + done(); + }); + }); + }); + }); + + it('should not be able to save an article if not logged in', function(done) { + agent.post('/articles') + .send(article) + .expect(401) + .end(function(articleSaveErr, articleSaveRes) { + // Call the assertion callback + done(articleSaveErr); + }); + }); + + it('should not be able to save an article if no title is provided', function(done) { + // Invalidate title field + article.title = ''; + + agent.post('/auth/signin') + .send(credentials) + .expect(200) + .end(function(signinErr, signinRes) { + // Handle signin error + if (signinErr) done(signinErr); + + // Get the userId + var userId = user.id; + + // Save a new article + agent.post('/articles') + .send(article) + .expect(400) + .end(function(articleSaveErr, articleSaveRes) { + // Set message assertion + (articleSaveRes.body.message).should.match('Title cannot be blank'); + + // Handle article save error + done(articleSaveErr); + }); + }); + }); + + it('should be able to update an article if signed in', function(done) { + agent.post('/auth/signin') + .send(credentials) + .expect(200) + .end(function(signinErr, signinRes) { + // Handle signin error + if (signinErr) done(signinErr); + + // Get the userId + var userId = user.id; + + // Save a new article + agent.post('/articles') + .send(article) + .expect(200) + .end(function(articleSaveErr, articleSaveRes) { + // Handle article save error + if (articleSaveErr) done(articleSaveErr); + + // Update article title + article.title = 'WHY YOU GOTTA BE SO MEAN?'; + + // Update an existing article + agent.put('/articles/' + articleSaveRes.body._id) + .send(article) + .expect(200) + .end(function(articleUpdateErr, articleUpdateRes) { + // Handle article update error + if (articleUpdateErr) done(articleUpdateErr); + + // Set assertions + (articleUpdateRes.body._id).should.equal(articleSaveRes.body._id); + (articleUpdateRes.body.title).should.match('WHY YOU GOTTA BE SO MEAN?'); + + // Call the assertion callback + done(); + }); + }); + }); + }); + + it('should be able to get a list of articles if not signed in', function(done) { + // Create new article model instance + var articleObj = new Article(article); + + // Save the article + articleObj.save(function() { + // Request articles + request(app).get('/articles') + .end(function(req, res) { + // Set assertion + res.body.should.be.an.Array.with.lengthOf(1); + + // Call the assertion callback + done(); + }); + + }); + }); + + + it('should be able to get a single article if not signed in', function(done) { + // Create new article model instance + var articleObj = new Article(article); + + // Save the article + articleObj.save(function() { + request(app).get('/articles/' + articleObj._id) + .end(function(req, res) { + // Set assertion + res.body.should.be.an.Object.with.property('title', article.title); + + // Call the assertion callback + done(); + }); + }); + }); + + it('should return proper error for single article which doesnt exist, if not signed in', function(done) { + request(app).get('/articles/test') + .end(function(req, res) { + // Set assertion + res.body.should.be.an.Object.with.property('message', 'Article is invalid'); + + // Call the assertion callback + done(); + }); + }); + + it('should be able to delete an article if signed in', function(done) { + agent.post('/auth/signin') + .send(credentials) + .expect(200) + .end(function(signinErr, signinRes) { + // Handle signin error + if (signinErr) done(signinErr); + + // Get the userId + var userId = user.id; + + // Save a new article + agent.post('/articles') + .send(article) + .expect(200) + .end(function(articleSaveErr, articleSaveRes) { + // Handle article save error + if (articleSaveErr) done(articleSaveErr); + + // Delete an existing article + agent.delete('/articles/' + articleSaveRes.body._id) + .send(article) + .expect(200) + .end(function(articleDeleteErr, articleDeleteRes) { + // Handle article error error + if (articleDeleteErr) done(articleDeleteErr); + + // Set assertions + (articleDeleteRes.body._id).should.equal(articleSaveRes.body._id); + + // Call the assertion callback + done(); + }); + }); + }); + }); + + it('should not be able to delete an article if not signed in', function(done) { + // Set article user + article.user = user; + + // Create new article model instance + var articleObj = new Article(article); + + // Save the article + articleObj.save(function() { + // Try deleting article + request(app).delete('/articles/' + articleObj._id) + .expect(401) + .end(function(articleDeleteErr, articleDeleteRes) { + // Set message assertion + (articleDeleteRes.body.message).should.match('User is not logged in'); + + // Handle article error error + done(articleDeleteErr); + }); + + }); + }); + + afterEach(function(done) { + User.remove().exec(function() { + Article.remove().exec(done); + }); + }); +}); diff --git a/app/tests/user.server.model.test.js b/app/tests/user.server.model.test.js new file mode 100644 index 0000000..e438487 --- /dev/null +++ b/app/tests/user.server.model.test.js @@ -0,0 +1,75 @@ +'use strict'; + +/** + * Module dependencies. + */ +var should = require('should'), + mongoose = require('mongoose'), + User = mongoose.model('User'); + +/** + * Globals + */ +var user, user2; + +/** + * Unit tests + */ +describe('User Model Unit Tests:', function() { + before(function(done) { + user = new User({ + firstName: 'Full', + lastName: 'Name', + displayName: 'Full Name', + email: 'test@test.com', + username: 'username', + password: 'password', + provider: 'local' + }); + user2 = new User({ + firstName: 'Full', + lastName: 'Name', + displayName: 'Full Name', + email: 'test@test.com', + username: 'username', + password: 'password', + provider: 'local' + }); + + done(); + }); + + describe('Method Save', function() { + it('should begin with no users', function(done) { + User.find({}, function(err, users) { + users.should.have.length(0); + done(); + }); + }); + + it('should be able to save without problems', function(done) { + user.save(done); + }); + + it('should fail to save an existing user again', function(done) { + user.save(function() { + user2.save(function(err) { + should.exist(err); + done(); + }); + }); + }); + + it('should be able to show an error when try to save without first name', function(done) { + user.firstName = ''; + return user.save(function(err) { + should.exist(err); + done(); + }); + }); + }); + + after(function(done) { + User.remove().exec(done); + }); +}); diff --git a/app/views/404.server.view.html b/app/views/404.server.view.html new file mode 100644 index 0000000..0074fa4 --- /dev/null +++ b/app/views/404.server.view.html @@ -0,0 +1,8 @@ +{% extends 'layout.server.view.html' %} + +{% block content %} +

Page Not Found

+
+	{{url}} is not a valid path.
+
+{% endblock %} \ No newline at end of file diff --git a/app/views/500.server.view.html b/app/views/500.server.view.html new file mode 100644 index 0000000..8e6711b --- /dev/null +++ b/app/views/500.server.view.html @@ -0,0 +1,8 @@ +{% extends 'layout.server.view.html' %} + +{% block content %} +

Server Error

+
+	{{error}}
+
+{% endblock %} \ No newline at end of file diff --git a/app/views/index.server.view.html b/app/views/index.server.view.html new file mode 100644 index 0000000..7e60893 --- /dev/null +++ b/app/views/index.server.view.html @@ -0,0 +1,5 @@ +{% extends 'layout.server.view.html' %} + +{% block content %} +
+{% endblock %} diff --git a/app/views/layout.server.view.html b/app/views/layout.server.view.html new file mode 100644 index 0000000..220a200 --- /dev/null +++ b/app/views/layout.server.view.html @@ -0,0 +1,79 @@ + + + + + {{title}} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {% for cssFile in cssFiles %} + {% endfor %} + + + + + + + +
+
+ {% block content %}{% endblock %} +
+
+ + + + + + {% for jsFile in jsFiles %} + {% endfor %} + + {% if process.env.NODE_ENV === 'development' %} + + + {% endif %} + + + + + \ No newline at end of file diff --git a/app/views/templates/reset-password-confirm-email.server.view.html b/app/views/templates/reset-password-confirm-email.server.view.html new file mode 100644 index 0000000..626ddc3 --- /dev/null +++ b/app/views/templates/reset-password-confirm-email.server.view.html @@ -0,0 +1,13 @@ + + + + + +

Dear {{name}},

+

+

This is a confirmation that the password for your account has just been changed

+
+
+

The {{appName}} Support Team

+ + \ No newline at end of file diff --git a/app/views/templates/reset-password-email.server.view.html b/app/views/templates/reset-password-email.server.view.html new file mode 100644 index 0000000..262edf0 --- /dev/null +++ b/app/views/templates/reset-password-email.server.view.html @@ -0,0 +1,18 @@ + + + + + +

Dear {{name}},

+
+

+ You have requested to have your password reset for your account at {{appName}} +

+

Please visit this url to reset your password:

+

{{url}}

+ If you didn't make this request, you can ignore this email. +
+
+

The {{appName}} Support Team

+ + \ No newline at end of file diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..a49e033 --- /dev/null +++ b/bower.json @@ -0,0 +1,16 @@ +{ + "name": "meanjs", + "version": "0.3.2", + "description": "Fullstack JavaScript with MongoDB, Express, AngularJS, and Node.js.", + "dependencies": { + "bootstrap": "~3", + "angular": "~1.2", + "angular-resource": "~1.2", + "angular-animate": "~1.2", + "angular-mocks": "~1.2", + "angular-bootstrap": "~0.11.2", + "angular-bootstrap": "~0.12.0", + "angular-ui-utils": "~0.1.1", + "angular-ui-router": "~0.2.11" + } +} diff --git a/config/config.js b/config/config.js new file mode 100644 index 0000000..3baa02e --- /dev/null +++ b/config/config.js @@ -0,0 +1,76 @@ +'use strict'; + +/** + * Module dependencies. + */ +var _ = require('lodash'), + glob = require('glob'); + +/** + * Load app configurations + */ +module.exports = _.extend( + require('./env/all'), + require('./env/' + process.env.NODE_ENV) || {} +); + +/** + * Get files by glob patterns + */ +module.exports.getGlobbedFiles = function(globPatterns, removeRoot) { + // For context switching + var _this = this; + + // URL paths regex + var urlRegex = new RegExp('^(?:[a-z]+:)?\/\/', 'i'); + + // The output array + var output = []; + + // If glob pattern is array so we use each pattern in a recursive way, otherwise we use glob + if (_.isArray(globPatterns)) { + globPatterns.forEach(function(globPattern) { + output = _.union(output, _this.getGlobbedFiles(globPattern, removeRoot)); + }); + } else if (_.isString(globPatterns)) { + if (urlRegex.test(globPatterns)) { + output.push(globPatterns); + } else { + glob(globPatterns, { + sync: true + }, function(err, files) { + if (removeRoot) { + files = files.map(function(file) { + return file.replace(removeRoot, ''); + }); + } + + output = _.union(output, files); + }); + } + } + + return output; +}; + +/** + * Get the modules JavaScript files + */ +module.exports.getJavaScriptAssets = function(includeTests) { + var output = this.getGlobbedFiles(this.assets.lib.js.concat(this.assets.js), 'public/'); + + // To include tests + if (includeTests) { + output = _.union(output, this.getGlobbedFiles(this.assets.tests)); + } + + return output; +}; + +/** + * Get the modules CSS files + */ +module.exports.getCSSAssets = function() { + var output = this.getGlobbedFiles(this.assets.lib.css.concat(this.assets.css), 'public/'); + return output; +}; \ No newline at end of file diff --git a/config/env/all.js b/config/env/all.js new file mode 100644 index 0000000..66fc208 --- /dev/null +++ b/config/env/all.js @@ -0,0 +1,72 @@ +'use strict'; + +module.exports = { + app: { + title: 'MEAN.JS', + description: 'Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js', + keywords: 'mongodb, express, angularjs, node.js, mongoose, passport' + }, + port: process.env.PORT || 3000, + templateEngine: 'swig', + // The secret should be set to a non-guessable string that + // is used to compute a session hash + sessionSecret: 'MEAN', + // The name of the MongoDB collection to store sessions in + sessionCollection: 'sessions', + // The session cookie settings + sessionCookie: { + path: '/', + httpOnly: true, + // If secure is set to true then it will cause the cookie to be set + // only when SSL-enabled (HTTPS) is used, and otherwise it won't + // set a cookie. 'true' is recommended yet it requires the above + // mentioned pre-requisite. + secure: false, + // Only set the maxAge to null if the cookie shouldn't be expired + // at all. The cookie will expunge when the browser is closed. + maxAge: null, + // To set the cookie in a specific domain uncomment the following + // setting: + // domain: 'yourdomain.com' + }, + // The session cookie name + sessionName: 'connect.sid', + log: { + // Can specify one of 'combined', 'common', 'dev', 'short', 'tiny' + format: 'combined', + // Stream defaults to process.stdout + // Uncomment to enable logging to a log on the file system + options: { + stream: 'access.log' + } + }, + assets: { + lib: { + css: [ + 'public/lib/bootstrap/dist/css/bootstrap.css', + 'public/lib/bootstrap/dist/css/bootstrap-theme.css', + ], + js: [ + 'public/lib/angular/angular.js', + 'public/lib/angular-resource/angular-resource.js', + 'public/lib/angular-animate/angular-animate.js', + 'public/lib/angular-ui-router/release/angular-ui-router.js', + 'public/lib/angular-ui-utils/ui-utils.js', + 'public/lib/angular-bootstrap/ui-bootstrap-tpls.js' + ] + }, + css: [ + 'public/modules/**/css/*.css' + ], + js: [ + 'public/config.js', + 'public/application.js', + 'public/modules/*/*.js', + 'public/modules/*/*[!tests]*/*.js' + ], + tests: [ + 'public/lib/angular-mocks/angular-mocks.js', + 'public/modules/*/tests/*.js' + ] + } +}; \ No newline at end of file diff --git a/config/env/development.js b/config/env/development.js new file mode 100644 index 0000000..35c08b7 --- /dev/null +++ b/config/env/development.js @@ -0,0 +1,58 @@ +'use strict'; + +module.exports = { + db: { + uri: 'mongodb://localhost/mean-dev', + options: { + user: '', + pass: '' + } + }, + log: { + // Can specify one of 'combined', 'common', 'dev', 'short', 'tiny' + format: 'dev', + // Stream defaults to process.stdout + // Uncomment to enable logging to a log on the file system + options: { + //stream: 'access.log' + } + }, + app: { + title: 'MEAN.JS - Development Environment' + }, + facebook: { + clientID: process.env.FACEBOOK_ID || 'APP_ID', + clientSecret: process.env.FACEBOOK_SECRET || 'APP_SECRET', + callbackURL: '/auth/facebook/callback' + }, + twitter: { + clientID: process.env.TWITTER_KEY || 'CONSUMER_KEY', + clientSecret: process.env.TWITTER_SECRET || 'CONSUMER_SECRET', + callbackURL: '/auth/twitter/callback' + }, + google: { + clientID: process.env.GOOGLE_ID || 'APP_ID', + clientSecret: process.env.GOOGLE_SECRET || 'APP_SECRET', + callbackURL: '/auth/google/callback' + }, + linkedin: { + clientID: process.env.LINKEDIN_ID || 'APP_ID', + clientSecret: process.env.LINKEDIN_SECRET || 'APP_SECRET', + callbackURL: '/auth/linkedin/callback' + }, + github: { + clientID: process.env.GITHUB_ID || 'APP_ID', + clientSecret: process.env.GITHUB_SECRET || 'APP_SECRET', + callbackURL: '/auth/github/callback' + }, + mailer: { + from: process.env.MAILER_FROM || 'MAILER_FROM', + options: { + service: process.env.MAILER_SERVICE_PROVIDER || 'MAILER_SERVICE_PROVIDER', + auth: { + user: process.env.MAILER_EMAIL_ID || 'MAILER_EMAIL_ID', + pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD' + } + } + } +}; diff --git a/config/env/production.js b/config/env/production.js new file mode 100644 index 0000000..ba25772 --- /dev/null +++ b/config/env/production.js @@ -0,0 +1,73 @@ +'use strict'; + +module.exports = { + db: { + uri: process.env.MONGO_URL || process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean', + options: { + user: '', + pass: '' + } + }, + log: { + // Can specify one of 'combined', 'common', 'dev', 'short', 'tiny' + format: 'combined', + // Stream defaults to process.stdout + // Uncomment to enable logging to a log on the file system + options: { + stream: 'access.log' + } + }, + assets: { + lib: { + css: [ + 'public/lib/bootstrap/dist/css/bootstrap.min.css', + 'public/lib/bootstrap/dist/css/bootstrap-theme.min.css', + ], + js: [ + 'public/lib/angular/angular.min.js', + 'public/lib/angular-resource/angular-resource.min.js', + 'public/lib/angular-animate/angular-animate.min.js', + 'public/lib/angular-ui-router/release/angular-ui-router.min.js', + 'public/lib/angular-ui-utils/ui-utils.min.js', + 'public/lib/angular-bootstrap/ui-bootstrap-tpls.min.js' + ] + }, + css: 'public/dist/application.min.css', + js: 'public/dist/application.min.js' + }, + facebook: { + clientID: process.env.FACEBOOK_ID || 'APP_ID', + clientSecret: process.env.FACEBOOK_SECRET || 'APP_SECRET', + callbackURL: '/auth/facebook/callback' + }, + twitter: { + clientID: process.env.TWITTER_KEY || 'CONSUMER_KEY', + clientSecret: process.env.TWITTER_SECRET || 'CONSUMER_SECRET', + callbackURL: '/auth/twitter/callback' + }, + google: { + clientID: process.env.GOOGLE_ID || 'APP_ID', + clientSecret: process.env.GOOGLE_SECRET || 'APP_SECRET', + callbackURL: '/auth/google/callback' + }, + linkedin: { + clientID: process.env.LINKEDIN_ID || 'APP_ID', + clientSecret: process.env.LINKEDIN_SECRET || 'APP_SECRET', + callbackURL: '/auth/linkedin/callback' + }, + github: { + clientID: process.env.GITHUB_ID || 'APP_ID', + clientSecret: process.env.GITHUB_SECRET || 'APP_SECRET', + callbackURL: '/auth/github/callback' + }, + mailer: { + from: process.env.MAILER_FROM || 'MAILER_FROM', + options: { + service: process.env.MAILER_SERVICE_PROVIDER || 'MAILER_SERVICE_PROVIDER', + auth: { + user: process.env.MAILER_EMAIL_ID || 'MAILER_EMAIL_ID', + pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD' + } + } + } +}; diff --git a/config/env/secure.js b/config/env/secure.js new file mode 100644 index 0000000..9ced6d7 --- /dev/null +++ b/config/env/secure.js @@ -0,0 +1,74 @@ +'use strict'; + +module.exports = { + port: 8443, + db: { + uri: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/mean', + options: { + user: '', + pass: '' + } + }, + log: { + // Can specify one of 'combined', 'common', 'dev', 'short', 'tiny' + format: 'combined', + // Stream defaults to process.stdout + // Uncomment to enable logging to a log on the file system + options: { + stream: 'access.log' + } + }, + assets: { + lib: { + css: [ + 'public/lib/bootstrap/dist/css/bootstrap.min.css', + 'public/lib/bootstrap/dist/css/bootstrap-theme.min.css', + ], + js: [ + 'public/lib/angular/angular.min.js', + 'public/lib/angular-resource/angular-resource.min.js', + 'public/lib/angular-animate/angular-animate.min.js', + 'public/lib/angular-ui-router/release/angular-ui-router.min.js', + 'public/lib/angular-ui-utils/ui-utils.min.js', + 'public/lib/angular-bootstrap/ui-bootstrap-tpls.min.js' + ] + }, + css: 'public/dist/application.min.css', + js: 'public/dist/application.min.js' + }, + facebook: { + clientID: process.env.FACEBOOK_ID || 'APP_ID', + clientSecret: process.env.FACEBOOK_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/facebook/callback' + }, + twitter: { + clientID: process.env.TWITTER_KEY || 'CONSUMER_KEY', + clientSecret: process.env.TWITTER_SECRET || 'CONSUMER_SECRET', + callbackURL: 'https://localhost:443/auth/twitter/callback' + }, + google: { + clientID: process.env.GOOGLE_ID || 'APP_ID', + clientSecret: process.env.GOOGLE_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/google/callback' + }, + linkedin: { + clientID: process.env.LINKEDIN_ID || 'APP_ID', + clientSecret: process.env.LINKEDIN_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/linkedin/callback' + }, + github: { + clientID: process.env.GITHUB_ID || 'APP_ID', + clientSecret: process.env.GITHUB_SECRET || 'APP_SECRET', + callbackURL: 'https://localhost:443/auth/github/callback' + }, + mailer: { + from: process.env.MAILER_FROM || 'MAILER_FROM', + options: { + service: process.env.MAILER_SERVICE_PROVIDER || 'MAILER_SERVICE_PROVIDER', + auth: { + user: process.env.MAILER_EMAIL_ID || 'MAILER_EMAIL_ID', + pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD' + } + } + } +}; \ No newline at end of file diff --git a/config/env/test.js b/config/env/test.js new file mode 100644 index 0000000..1d42d4e --- /dev/null +++ b/config/env/test.js @@ -0,0 +1,59 @@ +'use strict'; + +module.exports = { + db: { + uri: 'mongodb://localhost/mean-test', + options: { + user: '', + pass: '' + } + }, + port: 3001, + log: { + // Can specify one of 'combined', 'common', 'dev', 'short', 'tiny' + format: 'dev', + // Stream defaults to process.stdout + // Uncomment to enable logging to a log on the file system + options: { + //stream: 'access.log' + } + }, + app: { + title: 'MEAN.JS - Test Environment' + }, + facebook: { + clientID: process.env.FACEBOOK_ID || 'APP_ID', + clientSecret: process.env.FACEBOOK_SECRET || 'APP_SECRET', + callbackURL: '/auth/facebook/callback' + }, + twitter: { + clientID: process.env.TWITTER_KEY || 'CONSUMER_KEY', + clientSecret: process.env.TWITTER_SECRET || 'CONSUMER_SECRET', + callbackURL: '/auth/twitter/callback' + }, + google: { + clientID: process.env.GOOGLE_ID || 'APP_ID', + clientSecret: process.env.GOOGLE_SECRET || 'APP_SECRET', + callbackURL: '/auth/google/callback' + }, + linkedin: { + clientID: process.env.LINKEDIN_ID || 'APP_ID', + clientSecret: process.env.LINKEDIN_SECRET || 'APP_SECRET', + callbackURL: '/auth/linkedin/callback' + }, + github: { + clientID: process.env.GITHUB_ID || 'APP_ID', + clientSecret: process.env.GITHUB_SECRET || 'APP_SECRET', + callbackURL: '/auth/github/callback' + }, + mailer: { + from: process.env.MAILER_FROM || 'MAILER_FROM', + options: { + service: process.env.MAILER_SERVICE_PROVIDER || 'MAILER_SERVICE_PROVIDER', + auth: { + user: process.env.MAILER_EMAIL_ID || 'MAILER_EMAIL_ID', + pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD' + } + } + } +}; \ No newline at end of file diff --git a/config/express.js b/config/express.js new file mode 100755 index 0000000..6b5e1b8 --- /dev/null +++ b/config/express.js @@ -0,0 +1,165 @@ +'use strict'; + +/** + * Module dependencies. + */ +var fs = require('fs'), + http = require('http'), + https = require('https'), + express = require('express'), + morgan = require('morgan'), + logger = require('./logger'), + bodyParser = require('body-parser'), + session = require('express-session'), + compression = require('compression'), + methodOverride = require('method-override'), + cookieParser = require('cookie-parser'), + helmet = require('helmet'), + passport = require('passport'), + mongoStore = require('connect-mongo')({ + session: session + }), + flash = require('connect-flash'), + config = require('./config'), + consolidate = require('consolidate'), + path = require('path'); + +module.exports = function(db) { + // Initialize express app + var app = express(); + + // Globbing model files + config.getGlobbedFiles('./app/models/**/*.js').forEach(function(modelPath) { + require(path.resolve(modelPath)); + }); + + // Setting application local variables + app.locals.title = config.app.title; + app.locals.description = config.app.description; + app.locals.keywords = config.app.keywords; + app.locals.facebookAppId = config.facebook.clientID; + app.locals.jsFiles = config.getJavaScriptAssets(); + app.locals.cssFiles = config.getCSSAssets(); + + // Passing the request url to environment locals + app.use(function(req, res, next) { + res.locals.url = req.protocol + '://' + req.headers.host + req.url; + next(); + }); + + // Should be placed before express.static + app.use(compression({ + // only compress files for the following content types + filter: function(req, res) { + return (/json|text|javascript|css/).test(res.getHeader('Content-Type')); + }, + // zlib option for compression level + level: 3 + })); + + // Showing stack errors + app.set('showStackError', true); + + // Set swig as the template engine + app.engine('server.view.html', consolidate[config.templateEngine]); + + // Set views path and view engine + app.set('view engine', 'server.view.html'); + app.set('views', './app/views'); + + // Enable logger (morgan) + app.use(morgan(logger.getLogFormat(), logger.getLogOptions())); + + // Environment dependent middleware + if (process.env.NODE_ENV === 'development') { + // Disable views cache + app.set('view cache', false); + } else if (process.env.NODE_ENV === 'production') { + app.locals.cache = 'memory'; + } + + // Request body parsing middleware should be above methodOverride + app.use(bodyParser.urlencoded({ + extended: true + })); + app.use(bodyParser.json()); + app.use(methodOverride()); + + // Use helmet to secure Express headers + app.use(helmet.xframe()); + app.use(helmet.xssFilter()); + app.use(helmet.nosniff()); + app.use(helmet.ienoopen()); + app.disable('x-powered-by'); + + // Setting the app router and static folder + app.use(express.static(path.resolve('./public'))); + + // CookieParser should be above session + app.use(cookieParser()); + + // Express MongoDB session storage + app.use(session({ + saveUninitialized: true, + resave: true, + secret: config.sessionSecret, + store: new mongoStore({ + db: db.connection.db, + collection: config.sessionCollection + }), + cookie: config.sessionCookie, + name: config.sessionName + })); + + // use passport session + app.use(passport.initialize()); + app.use(passport.session()); + + // connect flash for flash messages + app.use(flash()); + + // Globbing routing files + config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) { + require(path.resolve(routePath))(app); + }); + + // Assume 'not found' in the error msgs is a 404. this is somewhat silly, but valid, you can do whatever you like, set properties, use instanceof etc. + app.use(function(err, req, res, next) { + // If the error object doesn't exists + if (!err) return next(); + + // Log it + console.error(err.stack); + + // Error page + res.status(500).render('500', { + error: err.stack + }); + }); + + // Assume 404 since no middleware responded + app.use(function(req, res) { + res.status(404).render('404', { + url: req.originalUrl, + error: 'Not Found' + }); + }); + + if (process.env.NODE_ENV === 'secure') { + // Load SSL key and certificate + var privateKey = fs.readFileSync('./config/sslcerts/key.pem', 'utf8'); + var certificate = fs.readFileSync('./config/sslcerts/cert.pem', 'utf8'); + + // Create HTTPS Server + var httpsServer = https.createServer({ + key: privateKey, + cert: certificate + }, app); + + // Return HTTPS server instance + return httpsServer; + } + + // Return Express server instance + return app; +}; diff --git a/config/init.js b/config/init.js new file mode 100644 index 0000000..34af434 --- /dev/null +++ b/config/init.js @@ -0,0 +1,31 @@ +'use strict'; + +/** + * Module dependencies. + */ +var glob = require('glob'), + chalk = require('chalk'); + +/** + * Module init function. + */ +module.exports = function() { + /** + * Before we begin, lets set the environment variable + * We'll Look for a valid NODE_ENV variable and if one cannot be found load the development NODE_ENV + */ + glob('./config/env/' + process.env.NODE_ENV + '.js', { + sync: true + }, function(err, environmentFiles) { + if (!environmentFiles.length) { + if (process.env.NODE_ENV) { + console.error(chalk.red('No configuration file found for "' + process.env.NODE_ENV + '" environment using development instead')); + } else { + console.error(chalk.red('NODE_ENV is not defined! Using default development environment')); + } + + process.env.NODE_ENV = 'development'; + } + }); + +}; \ No newline at end of file diff --git a/config/logger.js b/config/logger.js new file mode 100644 index 0000000..98b3a4c --- /dev/null +++ b/config/logger.js @@ -0,0 +1,36 @@ +'use strict'; + +/** + * Module dependencies. + */ + +var morgan = require('morgan'); +var config = require('./config'); +var fs = require('fs'); + +/** + * Module init function. + */ +module.exports = { + + getLogFormat: function() { + return config.log.format; + }, + + getLogOptions: function() { + var options = {}; + + try { + if ('stream' in config.log.options) { + options = { + stream: fs.createWriteStream(process.cwd() + '/' + config.log.options.stream, {flags: 'a'}) + }; + } + } catch (e) { + options = {}; + } + + return options; + } + +}; \ No newline at end of file diff --git a/config/passport.js b/config/passport.js new file mode 100755 index 0000000..fabbf77 --- /dev/null +++ b/config/passport.js @@ -0,0 +1,33 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'), + User = require('mongoose').model('User'), + path = require('path'), + config = require('./config'); + +/** + * Module init function. + */ +module.exports = function() { + // Serialize sessions + passport.serializeUser(function(user, done) { + done(null, user.id); + }); + + // Deserialize sessions + passport.deserializeUser(function(id, done) { + User.findOne({ + _id: id + }, '-salt -password', function(err, user) { + done(err, user); + }); + }); + + // Initialize strategies + config.getGlobbedFiles('./config/strategies/**/*.js').forEach(function(strategy) { + require(path.resolve(strategy))(); + }); +}; \ No newline at end of file diff --git a/config/strategies/facebook.js b/config/strategies/facebook.js new file mode 100644 index 0000000..5508bec --- /dev/null +++ b/config/strategies/facebook.js @@ -0,0 +1,41 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'), + FacebookStrategy = require('passport-facebook').Strategy, + config = require('../config'), + users = require('../../app/controllers/users.server.controller'); + +module.exports = function() { + // Use facebook strategy + passport.use(new FacebookStrategy({ + clientID: config.facebook.clientID, + clientSecret: config.facebook.clientSecret, + callbackURL: config.facebook.callbackURL, + passReqToCallback: true + }, + function(req, accessToken, refreshToken, profile, done) { + // Set the provider data and include tokens + var providerData = profile._json; + providerData.accessToken = accessToken; + providerData.refreshToken = refreshToken; + + // Create the user OAuth profile + var providerUserProfile = { + firstName: profile.name.givenName, + lastName: profile.name.familyName, + displayName: profile.displayName, + email: profile.emails[0].value, + username: profile.username, + provider: 'facebook', + providerIdentifierField: 'id', + providerData: providerData + }; + + // Save the user OAuth profile + users.saveOAuthUserProfile(req, providerUserProfile, done); + } + )); +}; \ No newline at end of file diff --git a/config/strategies/github.js b/config/strategies/github.js new file mode 100644 index 0000000..97c7c76 --- /dev/null +++ b/config/strategies/github.js @@ -0,0 +1,46 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'), + GithubStrategy = require('passport-github').Strategy, + config = require('../config'), + users = require('../../app/controllers/users.server.controller'); + +module.exports = function() { + // Use github strategy + passport.use(new GithubStrategy({ + clientID: config.github.clientID, + clientSecret: config.github.clientSecret, + callbackURL: config.github.callbackURL, + passReqToCallback: true + }, + function(req, accessToken, refreshToken, profile, done) { + // Set the provider data and include tokens + var providerData = profile._json; + providerData.accessToken = accessToken; + providerData.refreshToken = refreshToken; + + // Create the user OAuth profile + var displayName = profile.displayName.trim(); + var iSpace = displayName.indexOf(' '); // index of the whitespace following the firstName + var firstName = iSpace !== -1 ? displayName.substring(0, iSpace) : displayName; + var lastName = iSpace !== -1 ? displayName.substring(iSpace + 1) : ''; + + var providerUserProfile = { + firstName: firstName, + lastName: lastName, + displayName: displayName, + email: profile.emails[0].value, + username: profile.username, + provider: 'github', + providerIdentifierField: 'id', + providerData: providerData + }; + + // Save the user OAuth profile + users.saveOAuthUserProfile(req, providerUserProfile, done); + } + )); +}; \ No newline at end of file diff --git a/config/strategies/google.js b/config/strategies/google.js new file mode 100644 index 0000000..f8b454c --- /dev/null +++ b/config/strategies/google.js @@ -0,0 +1,41 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'), + GoogleStrategy = require('passport-google-oauth').OAuth2Strategy, + config = require('../config'), + users = require('../../app/controllers/users.server.controller'); + +module.exports = function() { + // Use google strategy + passport.use(new GoogleStrategy({ + clientID: config.google.clientID, + clientSecret: config.google.clientSecret, + callbackURL: config.google.callbackURL, + passReqToCallback: true + }, + function(req, accessToken, refreshToken, profile, done) { + // Set the provider data and include tokens + var providerData = profile._json; + providerData.accessToken = accessToken; + providerData.refreshToken = refreshToken; + + // Create the user OAuth profile + var providerUserProfile = { + firstName: profile.name.givenName, + lastName: profile.name.familyName, + displayName: profile.displayName, + email: profile.emails[0].value, + username: profile.username, + provider: 'google', + providerIdentifierField: 'id', + providerData: providerData + }; + + // Save the user OAuth profile + users.saveOAuthUserProfile(req, providerUserProfile, done); + } + )); +}; \ No newline at end of file diff --git a/config/strategies/linkedin.js b/config/strategies/linkedin.js new file mode 100644 index 0000000..c6ce090 --- /dev/null +++ b/config/strategies/linkedin.js @@ -0,0 +1,42 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'), + LinkedInStrategy = require('passport-linkedin').Strategy, + config = require('../config'), + users = require('../../app/controllers/users.server.controller'); + +module.exports = function() { + // Use linkedin strategy + passport.use(new LinkedInStrategy({ + consumerKey: config.linkedin.clientID, + consumerSecret: config.linkedin.clientSecret, + callbackURL: config.linkedin.callbackURL, + passReqToCallback: true, + profileFields: ['id', 'first-name', 'last-name', 'email-address'] + }, + function(req, accessToken, refreshToken, profile, done) { + // Set the provider data and include tokens + var providerData = profile._json; + providerData.accessToken = accessToken; + providerData.refreshToken = refreshToken; + + // Create the user OAuth profile + var providerUserProfile = { + firstName: profile.name.givenName, + lastName: profile.name.familyName, + displayName: profile.displayName, + email: profile.emails[0].value, + username: profile.username, + provider: 'linkedin', + providerIdentifierField: 'id', + providerData: providerData + }; + + // Save the user OAuth profile + users.saveOAuthUserProfile(req, providerUserProfile, done); + } + )); +}; \ No newline at end of file diff --git a/config/strategies/local.js b/config/strategies/local.js new file mode 100644 index 0000000..7101cac --- /dev/null +++ b/config/strategies/local.js @@ -0,0 +1,38 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'), + LocalStrategy = require('passport-local').Strategy, + User = require('mongoose').model('User'); + +module.exports = function() { + // Use local strategy + passport.use(new LocalStrategy({ + usernameField: 'username', + passwordField: 'password' + }, + function(username, password, done) { + User.findOne({ + username: username + }, function(err, user) { + if (err) { + return done(err); + } + if (!user) { + return done(null, false, { + message: 'Unknown user or invalid password' + }); + } + if (!user.authenticate(password)) { + return done(null, false, { + message: 'Unknown user or invalid password' + }); + } + + return done(null, user); + }); + } + )); +}; \ No newline at end of file diff --git a/config/strategies/twitter.js b/config/strategies/twitter.js new file mode 100644 index 0000000..2bc57cb --- /dev/null +++ b/config/strategies/twitter.js @@ -0,0 +1,45 @@ +'use strict'; + +/** + * Module dependencies. + */ +var passport = require('passport'), + TwitterStrategy = require('passport-twitter').Strategy, + config = require('../config'), + users = require('../../app/controllers/users.server.controller'); + +module.exports = function() { + // Use twitter strategy + passport.use(new TwitterStrategy({ + consumerKey: config.twitter.clientID, + consumerSecret: config.twitter.clientSecret, + callbackURL: config.twitter.callbackURL, + passReqToCallback: true + }, + function(req, token, tokenSecret, profile, done) { + // Set the provider data and include tokens + var providerData = profile._json; + providerData.token = token; + providerData.tokenSecret = tokenSecret; + + // Create the user OAuth profile + var displayName = profile.displayName.trim(); + var iSpace = displayName.indexOf(' '); // index of the whitespace following the firstName + var firstName = iSpace !== -1 ? displayName.substring(0, iSpace) : displayName; + var lastName = iSpace !== -1 ? displayName.substring(iSpace + 1) : ''; + + var providerUserProfile = { + firstName: firstName, + lastName: lastName, + displayName: displayName, + username: profile.username, + provider: 'twitter', + providerIdentifierField: 'id_str', + providerData: providerData + }; + + // Save the user OAuth profile + users.saveOAuthUserProfile(req, providerUserProfile, done); + } + )); +}; \ No newline at end of file diff --git a/fig.yml b/fig.yml new file mode 100644 index 0000000..4726a15 --- /dev/null +++ b/fig.yml @@ -0,0 +1,12 @@ +web: + build: . + links: + - db + ports: + - "3000:3000" + environment: + NODE_ENV: development +db: + image: mongo + ports: + - "27017:27017" \ No newline at end of file diff --git a/gruntfile.js b/gruntfile.js new file mode 100644 index 0000000..b45a9cd --- /dev/null +++ b/gruntfile.js @@ -0,0 +1,183 @@ +'use strict'; + +module.exports = function(grunt) { + // Unified Watch Object + var watchFiles = { + serverViews: ['app/views/**/*.*'], + serverJS: ['gruntfile.js', 'server.js', 'config/**/*.js', 'app/**/*.js', '!app/tests/'], + clientViews: ['public/modules/**/views/**/*.html'], + clientJS: ['public/js/*.js', 'public/modules/**/*.js'], + clientCSS: ['public/modules/**/*.css'], + mochaTests: ['app/tests/**/*.js'] + }; + + // Project Configuration + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + watch: { + serverViews: { + files: watchFiles.serverViews, + options: { + livereload: true + } + }, + serverJS: { + files: watchFiles.serverJS, + tasks: ['jshint'], + options: { + livereload: true + } + }, + clientViews: { + files: watchFiles.clientViews, + options: { + livereload: true + } + }, + clientJS: { + files: watchFiles.clientJS, + tasks: ['jshint'], + options: { + livereload: true + } + }, + clientCSS: { + files: watchFiles.clientCSS, + tasks: ['csslint'], + options: { + livereload: true + } + }, + mochaTests: { + files: watchFiles.mochaTests, + tasks: ['test:server'], + } + }, + jshint: { + all: { + src: watchFiles.clientJS.concat(watchFiles.serverJS), + options: { + jshintrc: true + } + } + }, + csslint: { + options: { + csslintrc: '.csslintrc' + }, + all: { + src: watchFiles.clientCSS + } + }, + uglify: { + production: { + options: { + mangle: false + }, + files: { + 'public/dist/application.min.js': 'public/dist/application.js' + } + } + }, + cssmin: { + combine: { + files: { + 'public/dist/application.min.css': '<%= applicationCSSFiles %>' + } + } + }, + nodemon: { + dev: { + script: 'server.js', + options: { + nodeArgs: ['--debug'], + ext: 'js,html', + watch: watchFiles.serverViews.concat(watchFiles.serverJS) + } + } + }, + 'node-inspector': { + custom: { + options: { + 'web-port': 1337, + 'web-host': 'localhost', + 'debug-port': 5858, + 'save-live-edit': true, + 'no-preload': true, + 'stack-trace-limit': 50, + 'hidden': [] + } + } + }, + ngAnnotate: { + production: { + files: { + 'public/dist/application.js': '<%= applicationJavaScriptFiles %>' + } + } + }, + concurrent: { + default: ['nodemon', 'watch'], + debug: ['nodemon', 'watch', 'node-inspector'], + options: { + logConcurrentOutput: true, + limit: 10 + } + }, + env: { + test: { + NODE_ENV: 'test' + }, + secure: { + NODE_ENV: 'secure' + } + }, + mochaTest: { + src: watchFiles.mochaTests, + options: { + reporter: 'spec', + require: 'server.js' + } + }, + karma: { + unit: { + configFile: 'karma.conf.js' + } + } + }); + + // Load NPM tasks + require('load-grunt-tasks')(grunt); + + // Making grunt default to force in order not to break the project. + grunt.option('force', true); + + // A Task for loading the configuration object + grunt.task.registerTask('loadConfig', 'Task that loads the config into a grunt option.', function() { + var init = require('./config/init')(); + var config = require('./config/config'); + + grunt.config.set('applicationJavaScriptFiles', config.assets.js); + grunt.config.set('applicationCSSFiles', config.assets.css); + }); + + // Default task(s). + grunt.registerTask('default', ['lint', 'concurrent:default']); + + // Debug task. + grunt.registerTask('debug', ['lint', 'concurrent:debug']); + + // Secure task(s). + grunt.registerTask('secure', ['env:secure', 'lint', 'concurrent:default']); + + // Lint task(s). + grunt.registerTask('lint', ['jshint', 'csslint']); + + // Build task(s). + grunt.registerTask('build', ['lint', 'loadConfig', 'ngAnnotate', 'uglify', 'cssmin']); + + // Test task. + grunt.registerTask('test', ['test:server', 'test:client']); + grunt.registerTask('test:server', ['env:test', 'mochaTest']); + grunt.registerTask('test:client', ['env:test', 'karma:unit']); +}; diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..62d500b --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,51 @@ +'use strict'; + +/** + * Module dependencies. + */ +var applicationConfiguration = require('./config/config'); + +// Karma configuration +module.exports = function(config) { + config.set({ + // Frameworks to use + frameworks: ['jasmine'], + + // List of files / patterns to load in the browser + files: applicationConfiguration.assets.lib.js.concat(applicationConfiguration.assets.js, applicationConfiguration.assets.tests), + + // Test results reporter to use + // Possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' + reporters: ['progress'], + + // Web server port + port: process.env.PORT || 9876, + + // Enable / disable colors in the output (reporters and logs) + colors: true, + + // Level of logging + // Possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + // Enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + // Start these browsers, currently available: + // - Chrome + // - ChromeCanary + // - Firefox + // - Opera + // - Safari (only Mac) + // - PhantomJS + // - IE (only Windows) + browsers: ['PhantomJS'], + + // If browser does not capture in given timeout [ms], kill it + captureTimeout: 60000, + + // Continuous Integration mode + // If true, it capture browsers, run tests and exit + singleRun: true + }); +}; diff --git a/package.json b/package.json new file mode 100755 index 0000000..7807d27 --- /dev/null +++ b/package.json @@ -0,0 +1,73 @@ +{ + "name": "meanjs", + "description": "Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js.", + "version": "0.3.3", + "private": false, + "author": "https://github.com/meanjs/mean/graphs/contributors", + "repository": { + "type": "git", + "url": "https://github.com/meanjs/mean.git" + }, + "engines": { + "node": ">=0.10.28", + "npm": ">=1.4.28" + }, + "scripts": { + "start": "grunt", + "test": "grunt test", + "postinstall": "bower install --config.interactive=false" + }, + "dependencies": { + "express": "~4.10.1", + "express-session": "~1.9.1", + "body-parser": "~1.9.0", + "cookie-parser": "~1.3.2", + "compression": "~1.2.0", + "method-override": "~2.3.0", + "morgan": "~1.4.1", + "connect-mongo": "~0.4.1", + "connect-flash": "~0.1.1", + "helmet": "~0.5.0", + "consolidate": "~0.10.0", + "swig": "~1.4.1", + "mongoose": "~3.8.8", + "passport": "~0.2.0", + "passport-local": "~1.0.0", + "passport-facebook": "~1.0.2", + "passport-twitter": "~1.0.2", + "passport-linkedin": "~0.1.3", + "passport-google-oauth": "~0.1.5", + "passport-github": "~0.1.5", + "lodash": "~2.4.1", + "forever": "~0.11.0", + "bower": "~1.3.8", + "grunt-cli": "~0.1.13", + "glob": "~4.0.5", + "async": "~0.9.0", + "nodemailer": "~1.3.0", + "chalk": "~0.5" + }, + "devDependencies": { + "supertest": "~0.14.0", + "should": "~4.1.0", + "grunt-env": "~0.4.1", + "grunt-node-inspector": "~0.1.3", + "grunt-contrib-watch": "~0.6.1", + "grunt-contrib-jshint": "~0.10.0", + "grunt-contrib-csslint": "^0.3.1", + "grunt-ng-annotate": "~0.4.0", + "grunt-contrib-uglify": "~0.6.0", + "grunt-contrib-cssmin": "~0.10.0", + "grunt-nodemon": "~0.3.0", + "grunt-concurrent": "~1.0.0", + "grunt-mocha-test": "~0.12.1", + "grunt-karma": "~0.9.0", + "load-grunt-tasks": "~1.0.0", + "karma": "~0.12.0", + "karma-jasmine": "~0.2.1", + "karma-coverage": "~0.2.0", + "karma-chrome-launcher": "~0.1.2", + "karma-firefox-launcher": "~0.1.3", + "karma-phantomjs-launcher": "~0.1.2" + } +} diff --git a/public/application.js b/public/application.js new file mode 100644 index 0000000..19bb411 --- /dev/null +++ b/public/application.js @@ -0,0 +1,20 @@ +'use strict'; + +//Start by defining the main module and adding the module dependencies +angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies); + +// Setting HTML5 Location Mode +angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', + function($locationProvider) { + $locationProvider.hashPrefix('!'); + } +]); + +//Then define the init function for starting up the application +angular.element(document).ready(function() { + //Fixing facebook bug with redirect + if (window.location.hash === '#_=_') window.location.hash = '#!'; + + //Then init the app + angular.bootstrap(document, [ApplicationConfiguration.applicationModuleName]); +}); \ No newline at end of file diff --git a/public/config.js b/public/config.js new file mode 100644 index 0000000..75de1c4 --- /dev/null +++ b/public/config.js @@ -0,0 +1,23 @@ +'use strict'; + +// Init the application configuration module for AngularJS application +var ApplicationConfiguration = (function() { + // Init module configuration options + var applicationModuleName = 'mean'; + var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ui.router', 'ui.bootstrap', 'ui.utils']; + + // Add a new vertical module + var registerModule = function(moduleName, dependencies) { + // Create angular module + angular.module(moduleName, dependencies || []); + + // Add the module to the AngularJS configuration file + angular.module(applicationModuleName).requires.push(moduleName); + }; + + return { + applicationModuleName: applicationModuleName, + applicationModuleVendorDependencies: applicationModuleVendorDependencies, + registerModule: registerModule + }; +})(); \ No newline at end of file diff --git a/public/humans.txt b/public/humans.txt new file mode 100755 index 0000000..5b037cf --- /dev/null +++ b/public/humans.txt @@ -0,0 +1,15 @@ +# humanstxt.org/ +# The humans responsible & technology colophon + +# TEAM + + -- -- + +# THANKS + + + +# TECHNOLOGY COLOPHON + + HTML5, CSS3 + jQuery, Modernizr diff --git a/public/modules/articles/articles.client.module.js b/public/modules/articles/articles.client.module.js new file mode 100755 index 0000000..3f4c63f --- /dev/null +++ b/public/modules/articles/articles.client.module.js @@ -0,0 +1,4 @@ +'use strict'; + +// Use Application configuration module to register a new module +ApplicationConfiguration.registerModule('articles'); diff --git a/public/modules/articles/config/articles.client.config.js b/public/modules/articles/config/articles.client.config.js new file mode 100644 index 0000000..7e1b0ff --- /dev/null +++ b/public/modules/articles/config/articles.client.config.js @@ -0,0 +1,11 @@ +'use strict'; + +// Configuring the Articles module +angular.module('articles').run(['Menus', + function(Menus) { + // Set top bar menu items + Menus.addMenuItem('topbar', 'Articles', 'articles', 'dropdown', '/articles(/create)?'); + Menus.addSubMenuItem('topbar', 'articles', 'List Articles', 'articles'); + Menus.addSubMenuItem('topbar', 'articles', 'New Article', 'articles/create'); + } +]); \ No newline at end of file diff --git a/public/modules/articles/config/articles.client.routes.js b/public/modules/articles/config/articles.client.routes.js new file mode 100755 index 0000000..1531a9a --- /dev/null +++ b/public/modules/articles/config/articles.client.routes.js @@ -0,0 +1,25 @@ +'use strict'; + +// Setting up route +angular.module('articles').config(['$stateProvider', + function($stateProvider) { + // Articles state routing + $stateProvider. + state('listArticles', { + url: '/articles', + templateUrl: 'modules/articles/views/list-articles.client.view.html' + }). + state('createArticle', { + url: '/articles/create', + templateUrl: 'modules/articles/views/create-article.client.view.html' + }). + state('viewArticle', { + url: '/articles/:articleId', + templateUrl: 'modules/articles/views/view-article.client.view.html' + }). + state('editArticle', { + url: '/articles/:articleId/edit', + templateUrl: 'modules/articles/views/edit-article.client.view.html' + }); + } +]); \ No newline at end of file diff --git a/public/modules/articles/controllers/articles.client.controller.js b/public/modules/articles/controllers/articles.client.controller.js new file mode 100644 index 0000000..364987e --- /dev/null +++ b/public/modules/articles/controllers/articles.client.controller.js @@ -0,0 +1,58 @@ +'use strict'; + +angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles', + function($scope, $stateParams, $location, Authentication, Articles) { + $scope.authentication = Authentication; + + $scope.create = function() { + var article = new Articles({ + title: this.title, + content: this.content + }); + article.$save(function(response) { + $location.path('articles/' + response._id); + + $scope.title = ''; + $scope.content = ''; + }, function(errorResponse) { + $scope.error = errorResponse.data.message; + }); + }; + + $scope.remove = function(article) { + if (article) { + article.$remove(); + + for (var i in $scope.articles) { + if ($scope.articles[i] === article) { + $scope.articles.splice(i, 1); + } + } + } else { + $scope.article.$remove(function() { + $location.path('articles'); + }); + } + }; + + $scope.update = function() { + var article = $scope.article; + + article.$update(function() { + $location.path('articles/' + article._id); + }, function(errorResponse) { + $scope.error = errorResponse.data.message; + }); + }; + + $scope.find = function() { + $scope.articles = Articles.query(); + }; + + $scope.findOne = function() { + $scope.article = Articles.get({ + articleId: $stateParams.articleId + }); + }; + } +]); \ No newline at end of file diff --git a/public/modules/articles/services/articles.client.service.js b/public/modules/articles/services/articles.client.service.js new file mode 100644 index 0000000..deeb7da --- /dev/null +++ b/public/modules/articles/services/articles.client.service.js @@ -0,0 +1,14 @@ +'use strict'; + +//Articles service used for communicating with the articles REST endpoints +angular.module('articles').factory('Articles', ['$resource', + function($resource) { + return $resource('articles/:articleId', { + articleId: '@_id' + }, { + update: { + method: 'PUT' + } + }); + } +]); \ No newline at end of file diff --git a/public/modules/articles/tests/articles.client.controller.test.js b/public/modules/articles/tests/articles.client.controller.test.js new file mode 100644 index 0000000..7e25c69 --- /dev/null +++ b/public/modules/articles/tests/articles.client.controller.test.js @@ -0,0 +1,170 @@ +'use strict'; + +(function() { + // Articles Controller Spec + describe('ArticlesController', function() { + // Initialize global variables + var ArticlesController, + scope, + $httpBackend, + $stateParams, + $location; + + // The $resource service augments the response object with methods for updating and deleting the resource. + // If we were to use the standard toEqual matcher, our tests would fail because the test values would not match + // the responses exactly. To solve the problem, we define a new toEqualData Jasmine matcher. + // When the toEqualData matcher compares two objects, it takes only object properties into + // account and ignores methods. + beforeEach(function() { + jasmine.addMatchers({ + toEqualData: function(util, customEqualityTesters) { + return { + compare: function(actual, expected) { + return { + pass: angular.equals(actual, expected) + }; + } + }; + } + }); + }); + + // Then we can start by loading the main application module + beforeEach(module(ApplicationConfiguration.applicationModuleName)); + + // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_). + // This allows us to inject a service but then attach it to a variable + // with the same name as the service. + beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) { + // Set a new global scope + scope = $rootScope.$new(); + + // Point global variables to injected services + $stateParams = _$stateParams_; + $httpBackend = _$httpBackend_; + $location = _$location_; + + // Initialize the Articles controller. + ArticlesController = $controller('ArticlesController', { + $scope: scope + }); + })); + + it('$scope.find() should create an array with at least one article object fetched from XHR', inject(function(Articles) { + // Create sample article using the Articles service + var sampleArticle = new Articles({ + title: 'An Article about MEAN', + content: 'MEAN rocks!' + }); + + // Create a sample articles array that includes the new article + var sampleArticles = [sampleArticle]; + + // Set GET response + $httpBackend.expectGET('articles').respond(sampleArticles); + + // Run controller functionality + scope.find(); + $httpBackend.flush(); + + // Test scope value + expect(scope.articles).toEqualData(sampleArticles); + })); + + it('$scope.findOne() should create an array with one article object fetched from XHR using a articleId URL parameter', inject(function(Articles) { + // Define a sample article object + var sampleArticle = new Articles({ + title: 'An Article about MEAN', + content: 'MEAN rocks!' + }); + + // Set the URL parameter + $stateParams.articleId = '525a8422f6d0f87f0e407a33'; + + // Set GET response + $httpBackend.expectGET(/articles\/([0-9a-fA-F]{24})$/).respond(sampleArticle); + + // Run controller functionality + scope.findOne(); + $httpBackend.flush(); + + // Test scope value + expect(scope.article).toEqualData(sampleArticle); + })); + + it('$scope.create() with valid form data should send a POST request with the form input values and then locate to new object URL', inject(function(Articles) { + // Create a sample article object + var sampleArticlePostData = new Articles({ + title: 'An Article about MEAN', + content: 'MEAN rocks!' + }); + + // Create a sample article response + var sampleArticleResponse = new Articles({ + _id: '525cf20451979dea2c000001', + title: 'An Article about MEAN', + content: 'MEAN rocks!' + }); + + // Fixture mock form input values + scope.title = 'An Article about MEAN'; + scope.content = 'MEAN rocks!'; + + // Set POST response + $httpBackend.expectPOST('articles', sampleArticlePostData).respond(sampleArticleResponse); + + // Run controller functionality + scope.create(); + $httpBackend.flush(); + + // Test form inputs are reset + expect(scope.title).toEqual(''); + expect(scope.content).toEqual(''); + + // Test URL redirection after the article was created + expect($location.path()).toBe('/articles/' + sampleArticleResponse._id); + })); + + it('$scope.update() should update a valid article', inject(function(Articles) { + // Define a sample article put data + var sampleArticlePutData = new Articles({ + _id: '525cf20451979dea2c000001', + title: 'An Article about MEAN', + content: 'MEAN Rocks!' + }); + + // Mock article in scope + scope.article = sampleArticlePutData; + + // Set PUT response + $httpBackend.expectPUT(/articles\/([0-9a-fA-F]{24})$/).respond(); + + // Run controller functionality + scope.update(); + $httpBackend.flush(); + + // Test URL location to new object + expect($location.path()).toBe('/articles/' + sampleArticlePutData._id); + })); + + it('$scope.remove() should send a DELETE request with a valid articleId and remove the article from the scope', inject(function(Articles) { + // Create new article object + var sampleArticle = new Articles({ + _id: '525a8422f6d0f87f0e407a33' + }); + + // Create new articles array and include the article + scope.articles = [sampleArticle]; + + // Set expected DELETE response + $httpBackend.expectDELETE(/articles\/([0-9a-fA-F]{24})$/).respond(204); + + // Run controller functionality + scope.remove(sampleArticle); + $httpBackend.flush(); + + // Test array after successful delete + expect(scope.articles.length).toBe(0); + })); + }); +}()); \ No newline at end of file diff --git a/public/modules/articles/views/create-article.client.view.html b/public/modules/articles/views/create-article.client.view.html new file mode 100644 index 0000000..ab8db8e --- /dev/null +++ b/public/modules/articles/views/create-article.client.view.html @@ -0,0 +1,29 @@ +
+ +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+ +
+
+
+
+
\ No newline at end of file diff --git a/public/modules/articles/views/edit-article.client.view.html b/public/modules/articles/views/edit-article.client.view.html new file mode 100644 index 0000000..353cb8e --- /dev/null +++ b/public/modules/articles/views/edit-article.client.view.html @@ -0,0 +1,35 @@ +
+ +
+
+
+
+ +
+ +
+
+

Title is required

+
+
+
+ +
+ +
+
+

Content is required

+
+
+
+ +
+
+ +
+
+
+
+
\ No newline at end of file diff --git a/public/modules/articles/views/list-articles.client.view.html b/public/modules/articles/views/list-articles.client.view.html new file mode 100644 index 0000000..861ae5b --- /dev/null +++ b/public/modules/articles/views/list-articles.client.view.html @@ -0,0 +1,20 @@ +
+ + +
+ No articles yet, why don't you create one? +
+
\ No newline at end of file diff --git a/public/modules/articles/views/view-article.client.view.html b/public/modules/articles/views/view-article.client.view.html new file mode 100644 index 0000000..312d25c --- /dev/null +++ b/public/modules/articles/views/view-article.client.view.html @@ -0,0 +1,22 @@ +
+ + + + + Posted on + + by + + + +

+
\ No newline at end of file diff --git a/public/modules/core/config/core.client.routes.js b/public/modules/core/config/core.client.routes.js new file mode 100755 index 0000000..894e3a6 --- /dev/null +++ b/public/modules/core/config/core.client.routes.js @@ -0,0 +1,16 @@ +'use strict'; + +// Setting up route +angular.module('core').config(['$stateProvider', '$urlRouterProvider', + function($stateProvider, $urlRouterProvider) { + // Redirect to home view when route not found + $urlRouterProvider.otherwise('/'); + + // Home state routing + $stateProvider. + state('home', { + url: '/', + templateUrl: 'modules/core/views/home.client.view.html' + }); + } +]); \ No newline at end of file diff --git a/public/modules/core/controllers/header.client.controller.js b/public/modules/core/controllers/header.client.controller.js new file mode 100644 index 0000000..1b8c2b7 --- /dev/null +++ b/public/modules/core/controllers/header.client.controller.js @@ -0,0 +1,18 @@ +'use strict'; + +angular.module('core').controller('HeaderController', ['$scope', 'Authentication', 'Menus', + function($scope, Authentication, Menus) { + $scope.authentication = Authentication; + $scope.isCollapsed = false; + $scope.menu = Menus.getMenu('topbar'); + + $scope.toggleCollapsibleMenu = function() { + $scope.isCollapsed = !$scope.isCollapsed; + }; + + // Collapsing the menu after navigation + $scope.$on('$stateChangeSuccess', function() { + $scope.isCollapsed = false; + }); + } +]); \ No newline at end of file diff --git a/public/modules/core/controllers/home.client.controller.js b/public/modules/core/controllers/home.client.controller.js new file mode 100644 index 0000000..63d0f29 --- /dev/null +++ b/public/modules/core/controllers/home.client.controller.js @@ -0,0 +1,9 @@ +'use strict'; + + +angular.module('core').controller('HomeController', ['$scope', 'Authentication', + function($scope, Authentication) { + // This provides Authentication context. + $scope.authentication = Authentication; + } +]); \ No newline at end of file diff --git a/public/modules/core/core.client.module.js b/public/modules/core/core.client.module.js new file mode 100755 index 0000000..01c9d32 --- /dev/null +++ b/public/modules/core/core.client.module.js @@ -0,0 +1,4 @@ +'use strict'; + +// Use Application configuration module to register a new module +ApplicationConfiguration.registerModule('core'); diff --git a/public/modules/core/css/core.css b/public/modules/core/css/core.css new file mode 100644 index 0000000..f20a04c --- /dev/null +++ b/public/modules/core/css/core.css @@ -0,0 +1,20 @@ +.content { + margin-top: 50px; +} +.undecorated-link:hover { + text-decoration: none; +} +[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak { + display: none !important; +} +.ng-invalid.ng-dirty { + border-color: #FA787E; +} +.ng-valid.ng-dirty { + border-color: #78FA89; +} +.browsehappy.jumbotron.hide, +body.ng-cloak +{ + display: block; +} diff --git a/public/modules/core/img/brand/favicon.ico b/public/modules/core/img/brand/favicon.ico new file mode 100644 index 0000000..756ec7e Binary files /dev/null and b/public/modules/core/img/brand/favicon.ico differ diff --git a/public/modules/core/img/brand/logo.png b/public/modules/core/img/brand/logo.png new file mode 100644 index 0000000..d28cc87 Binary files /dev/null and b/public/modules/core/img/brand/logo.png differ diff --git a/public/modules/core/img/loaders/loader.gif b/public/modules/core/img/loaders/loader.gif new file mode 100644 index 0000000..f89b233 Binary files /dev/null and b/public/modules/core/img/loaders/loader.gif differ diff --git a/public/modules/core/services/menus.client.service.js b/public/modules/core/services/menus.client.service.js new file mode 100644 index 0000000..d2366d1 --- /dev/null +++ b/public/modules/core/services/menus.client.service.js @@ -0,0 +1,166 @@ +'use strict'; + +//Menu service used for managing menus +angular.module('core').service('Menus', [ + + function() { + // Define a set of default roles + this.defaultRoles = ['*']; + + // Define the menus object + this.menus = {}; + + // A private function for rendering decision + var shouldRender = function(user) { + if (user) { + if (!!~this.roles.indexOf('*')) { + return true; + } else { + for (var userRoleIndex in user.roles) { + for (var roleIndex in this.roles) { + if (this.roles[roleIndex] === user.roles[userRoleIndex]) { + return true; + } + } + } + } + } else { + return this.isPublic; + } + + return false; + }; + + // Validate menu existance + this.validateMenuExistance = function(menuId) { + if (menuId && menuId.length) { + if (this.menus[menuId]) { + return true; + } else { + throw new Error('Menu does not exists'); + } + } else { + throw new Error('MenuId was not provided'); + } + + return false; + }; + + // Get the menu object by menu id + this.getMenu = function(menuId) { + // Validate that the menu exists + this.validateMenuExistance(menuId); + + // Return the menu object + return this.menus[menuId]; + }; + + // Add new menu object by menu id + this.addMenu = function(menuId, isPublic, roles) { + // Create the new menu + this.menus[menuId] = { + isPublic: isPublic || false, + roles: roles || this.defaultRoles, + items: [], + shouldRender: shouldRender + }; + + // Return the menu object + return this.menus[menuId]; + }; + + // Remove existing menu object by menu id + this.removeMenu = function(menuId) { + // Validate that the menu exists + this.validateMenuExistance(menuId); + + // Return the menu object + delete this.menus[menuId]; + }; + + // Add menu item object + this.addMenuItem = function(menuId, menuItemTitle, menuItemURL, menuItemType, menuItemUIRoute, isPublic, roles, position) { + // Validate that the menu exists + this.validateMenuExistance(menuId); + + // Push new menu item + this.menus[menuId].items.push({ + title: menuItemTitle, + link: menuItemURL, + menuItemType: menuItemType || 'item', + menuItemClass: menuItemType, + uiRoute: menuItemUIRoute || ('/' + menuItemURL), + isPublic: ((isPublic === null || typeof isPublic === 'undefined') ? this.menus[menuId].isPublic : isPublic), + roles: ((roles === null || typeof roles === 'undefined') ? this.menus[menuId].roles : roles), + position: position || 0, + items: [], + shouldRender: shouldRender + }); + + // Return the menu object + return this.menus[menuId]; + }; + + // Add submenu item object + this.addSubMenuItem = function(menuId, rootMenuItemURL, menuItemTitle, menuItemURL, menuItemUIRoute, isPublic, roles, position) { + // Validate that the menu exists + this.validateMenuExistance(menuId); + + // Search for menu item + for (var itemIndex in this.menus[menuId].items) { + if (this.menus[menuId].items[itemIndex].link === rootMenuItemURL) { + // Push new submenu item + this.menus[menuId].items[itemIndex].items.push({ + title: menuItemTitle, + link: menuItemURL, + uiRoute: menuItemUIRoute || ('/' + menuItemURL), + isPublic: ((isPublic === null || typeof isPublic === 'undefined') ? this.menus[menuId].items[itemIndex].isPublic : isPublic), + roles: ((roles === null || typeof roles === 'undefined') ? this.menus[menuId].items[itemIndex].roles : roles), + position: position || 0, + shouldRender: shouldRender + }); + } + } + + // Return the menu object + return this.menus[menuId]; + }; + + // Remove existing menu object by menu id + this.removeMenuItem = function(menuId, menuItemURL) { + // Validate that the menu exists + this.validateMenuExistance(menuId); + + // Search for menu item to remove + for (var itemIndex in this.menus[menuId].items) { + if (this.menus[menuId].items[itemIndex].link === menuItemURL) { + this.menus[menuId].items.splice(itemIndex, 1); + } + } + + // Return the menu object + return this.menus[menuId]; + }; + + // Remove existing menu object by menu id + this.removeSubMenuItem = function(menuId, submenuItemURL) { + // Validate that the menu exists + this.validateMenuExistance(menuId); + + // Search for menu item to remove + for (var itemIndex in this.menus[menuId].items) { + for (var subitemIndex in this.menus[menuId].items[itemIndex].items) { + if (this.menus[menuId].items[itemIndex].items[subitemIndex].link === submenuItemURL) { + this.menus[menuId].items[itemIndex].items.splice(subitemIndex, 1); + } + } + } + + // Return the menu object + return this.menus[menuId]; + }; + + //Adding the topbar menu + this.addMenu('topbar'); + } +]); \ No newline at end of file diff --git a/public/modules/core/tests/header.client.controller.test.js b/public/modules/core/tests/header.client.controller.test.js new file mode 100644 index 0000000..76ee4fb --- /dev/null +++ b/public/modules/core/tests/header.client.controller.test.js @@ -0,0 +1,24 @@ +'use strict'; + +(function() { + describe('HeaderController', function() { + //Initialize global variables + var scope, + HeaderController; + + // Load the main application module + beforeEach(module(ApplicationConfiguration.applicationModuleName)); + + beforeEach(inject(function($controller, $rootScope) { + scope = $rootScope.$new(); + + HeaderController = $controller('HeaderController', { + $scope: scope + }); + })); + + it('should expose the authentication service', function() { + expect(scope.authentication).toBeTruthy(); + }); + }); +})(); \ No newline at end of file diff --git a/public/modules/core/tests/home.client.controller.test.js b/public/modules/core/tests/home.client.controller.test.js new file mode 100644 index 0000000..a5b1a56 --- /dev/null +++ b/public/modules/core/tests/home.client.controller.test.js @@ -0,0 +1,24 @@ +'use strict'; + +(function() { + describe('HomeController', function() { + //Initialize global variables + var scope, + HomeController; + + // Load the main application module + beforeEach(module(ApplicationConfiguration.applicationModuleName)); + + beforeEach(inject(function($controller, $rootScope) { + scope = $rootScope.$new(); + + HomeController = $controller('HomeController', { + $scope: scope + }); + })); + + it('should expose the authentication service', function() { + expect(scope.authentication).toBeTruthy(); + }); + }); +})(); \ No newline at end of file diff --git a/public/modules/core/views/header.client.view.html b/public/modules/core/views/header.client.view.html new file mode 100644 index 0000000..541aa3a --- /dev/null +++ b/public/modules/core/views/header.client.view.html @@ -0,0 +1,58 @@ +
+ + +
\ No newline at end of file diff --git a/public/modules/core/views/home.client.view.html b/public/modules/core/views/home.client.view.html new file mode 100644 index 0000000..2625a98 --- /dev/null +++ b/public/modules/core/views/home.client.view.html @@ -0,0 +1,94 @@ +
+
+
+
+ MEAN.JS +
+
+
+
+

+ Open-Source Full-Stack Solution For MEAN Applications +

+
+
+

+ Learn more +

+
+
+
+

Congrats! You've configured and ran the sample application successfully.

+

MEAN.JS is a web application boilerplate, which means you should start changing everything :-)

+

This sample application tracks users and articles.

+
    +
  • + Click + Signup + to get started. +
  • +
  • + Configure your app to work with your social accounts, by editing the + /config/env/*.js + files. +
  • +
  • + Edit your users module. +
  • +
  • + Add new CRUD modules. +
  • +
  • + Have fun... +
  • +
+
+
+
+

+ MongoDB +

+

MongoDB is a database. MongoDB's great manual is the place to get started with NoSQL and MongoDB.

+
+
+

+ Express +

+

Express is an app server. Check out The ExpressJS API reference for more information or StackOverflow for more info.

+
+
+

+ AngularJS +

+

AngularJS is web app framework. Angular's website offers a lot. The Thinkster Popular Guide and Egghead Videos are great resources.

+
+
+

+ Node.js +

+

Node.js is a web server. Node's website and this stackOverflow thread offer excellent starting points to get to grasps with node.

+
+
+
+

MEAN.JS Documentation

+

+ Once you're familiar with the foundation technology, check out the MEAN.JS Documentation: +

+

+
+
Enjoy & Keep Us Updated, +
The MEAN.JS Team. +
diff --git a/public/modules/users/config/users.client.config.js b/public/modules/users/config/users.client.config.js new file mode 100644 index 0000000..0bfc8b6 --- /dev/null +++ b/public/modules/users/config/users.client.config.js @@ -0,0 +1,30 @@ +'use strict'; + +// Config HTTP Error Handling +angular.module('users').config(['$httpProvider', + function($httpProvider) { + // Set the httpProvider "not authorized" interceptor + $httpProvider.interceptors.push(['$q', '$location', 'Authentication', + function($q, $location, Authentication) { + return { + responseError: function(rejection) { + switch (rejection.status) { + case 401: + // Deauthenticate the global user + Authentication.user = null; + + // Redirect to signin page + $location.path('signin'); + break; + case 403: + // Add unauthorized behaviour + break; + } + + return $q.reject(rejection); + } + }; + } + ]); + } +]); \ No newline at end of file diff --git a/public/modules/users/config/users.client.routes.js b/public/modules/users/config/users.client.routes.js new file mode 100755 index 0000000..879c2c4 --- /dev/null +++ b/public/modules/users/config/users.client.routes.js @@ -0,0 +1,45 @@ +'use strict'; + +// Setting up route +angular.module('users').config(['$stateProvider', + function($stateProvider) { + // Users state routing + $stateProvider. + state('profile', { + url: '/settings/profile', + templateUrl: 'modules/users/views/settings/edit-profile.client.view.html' + }). + state('password', { + url: '/settings/password', + templateUrl: 'modules/users/views/settings/change-password.client.view.html' + }). + state('accounts', { + url: '/settings/accounts', + templateUrl: 'modules/users/views/settings/social-accounts.client.view.html' + }). + state('signup', { + url: '/signup', + templateUrl: 'modules/users/views/authentication/signup.client.view.html' + }). + state('signin', { + url: '/signin', + templateUrl: 'modules/users/views/authentication/signin.client.view.html' + }). + state('forgot', { + url: '/password/forgot', + templateUrl: 'modules/users/views/password/forgot-password.client.view.html' + }). + state('reset-invalid', { + url: '/password/reset/invalid', + templateUrl: 'modules/users/views/password/reset-password-invalid.client.view.html' + }). + state('reset-success', { + url: '/password/reset/success', + templateUrl: 'modules/users/views/password/reset-password-success.client.view.html' + }). + state('reset', { + url: '/password/reset/:token', + templateUrl: 'modules/users/views/password/reset-password.client.view.html' + }); + } +]); \ No newline at end of file diff --git a/public/modules/users/controllers/authentication.client.controller.js b/public/modules/users/controllers/authentication.client.controller.js new file mode 100644 index 0000000..3e27cc3 --- /dev/null +++ b/public/modules/users/controllers/authentication.client.controller.js @@ -0,0 +1,34 @@ +'use strict'; + +angular.module('users').controller('AuthenticationController', ['$scope', '$http', '$location', 'Authentication', + function($scope, $http, $location, Authentication) { + $scope.authentication = Authentication; + + // If user is signed in then redirect back home + if ($scope.authentication.user) $location.path('/'); + + $scope.signup = function() { + $http.post('/auth/signup', $scope.credentials).success(function(response) { + // If successful we assign the response to the global user model + $scope.authentication.user = response; + + // And redirect to the index page + $location.path('/'); + }).error(function(response) { + $scope.error = response.message; + }); + }; + + $scope.signin = function() { + $http.post('/auth/signin', $scope.credentials).success(function(response) { + // If successful we assign the response to the global user model + $scope.authentication.user = response; + + // And redirect to the index page + $location.path('/'); + }).error(function(response) { + $scope.error = response.message; + }); + }; + } +]); \ No newline at end of file diff --git a/public/modules/users/controllers/password.client.controller.js b/public/modules/users/controllers/password.client.controller.js new file mode 100644 index 0000000..dbc9e92 --- /dev/null +++ b/public/modules/users/controllers/password.client.controller.js @@ -0,0 +1,44 @@ +'use strict'; + +angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$http', '$location', 'Authentication', + function($scope, $stateParams, $http, $location, Authentication) { + $scope.authentication = Authentication; + + //If user is signed in then redirect back home + if ($scope.authentication.user) $location.path('/'); + + // Submit forgotten password account id + $scope.askForPasswordReset = function() { + $scope.success = $scope.error = null; + + $http.post('/auth/forgot', $scope.credentials).success(function(response) { + // Show user success message and clear form + $scope.credentials = null; + $scope.success = response.message; + + }).error(function(response) { + // Show user error message and clear form + $scope.credentials = null; + $scope.error = response.message; + }); + }; + + // Change user password + $scope.resetUserPassword = function() { + $scope.success = $scope.error = null; + + $http.post('/auth/reset/' + $stateParams.token, $scope.passwordDetails).success(function(response) { + // If successful show success message and clear form + $scope.passwordDetails = null; + + // Attach user profile + Authentication.user = response; + + // And redirect to the index page + $location.path('/password/reset/success'); + }).error(function(response) { + $scope.error = response.message; + }); + }; + } +]); \ No newline at end of file diff --git a/public/modules/users/controllers/settings.client.controller.js b/public/modules/users/controllers/settings.client.controller.js new file mode 100644 index 0000000..8616fc9 --- /dev/null +++ b/public/modules/users/controllers/settings.client.controller.js @@ -0,0 +1,71 @@ +'use strict'; + +angular.module('users').controller('SettingsController', ['$scope', '$http', '$location', 'Users', 'Authentication', + function($scope, $http, $location, Users, Authentication) { + $scope.user = Authentication.user; + + // If user is not signed in then redirect back home + if (!$scope.user) $location.path('/'); + + // Check if there are additional accounts + $scope.hasConnectedAdditionalSocialAccounts = function(provider) { + for (var i in $scope.user.additionalProvidersData) { + return true; + } + + return false; + }; + + // Check if provider is already in use with current user + $scope.isConnectedSocialAccount = function(provider) { + return $scope.user.provider === provider || ($scope.user.additionalProvidersData && $scope.user.additionalProvidersData[provider]); + }; + + // Remove a user social account + $scope.removeUserSocialAccount = function(provider) { + $scope.success = $scope.error = null; + + $http.delete('/users/accounts', { + params: { + provider: provider + } + }).success(function(response) { + // If successful show success message and clear form + $scope.success = true; + $scope.user = Authentication.user = response; + }).error(function(response) { + $scope.error = response.message; + }); + }; + + // Update a user profile + $scope.updateUserProfile = function(isValid) { + if (isValid) { + $scope.success = $scope.error = null; + var user = new Users($scope.user); + + user.$update(function(response) { + $scope.success = true; + Authentication.user = response; + }, function(response) { + $scope.error = response.data.message; + }); + } else { + $scope.submitted = true; + } + }; + + // Change user password + $scope.changeUserPassword = function() { + $scope.success = $scope.error = null; + + $http.post('/users/password', $scope.passwordDetails).success(function(response) { + // If successful show success message and clear form + $scope.success = true; + $scope.passwordDetails = null; + }).error(function(response) { + $scope.error = response.message; + }); + }; + } +]); \ No newline at end of file diff --git a/public/modules/users/css/users.css b/public/modules/users/css/users.css new file mode 100644 index 0000000..de67bf9 --- /dev/null +++ b/public/modules/users/css/users.css @@ -0,0 +1,14 @@ +@media (min-width: 992px) { + .nav-users { + position: fixed; + } +} +.remove-account-container { + display: inline-block; + position: relative; +} +.btn-remove-account { + top: 10px; + right: 10px; + position: absolute; +} \ No newline at end of file diff --git a/public/modules/users/img/buttons/facebook.png b/public/modules/users/img/buttons/facebook.png new file mode 100644 index 0000000..8ebea4f Binary files /dev/null and b/public/modules/users/img/buttons/facebook.png differ diff --git a/public/modules/users/img/buttons/github.png b/public/modules/users/img/buttons/github.png new file mode 100644 index 0000000..6ff6672 Binary files /dev/null and b/public/modules/users/img/buttons/github.png differ diff --git a/public/modules/users/img/buttons/google.png b/public/modules/users/img/buttons/google.png new file mode 100644 index 0000000..0c00712 Binary files /dev/null and b/public/modules/users/img/buttons/google.png differ diff --git a/public/modules/users/img/buttons/linkedin.png b/public/modules/users/img/buttons/linkedin.png new file mode 100644 index 0000000..ff039a4 Binary files /dev/null and b/public/modules/users/img/buttons/linkedin.png differ diff --git a/public/modules/users/img/buttons/twitter.png b/public/modules/users/img/buttons/twitter.png new file mode 100644 index 0000000..2e399c1 Binary files /dev/null and b/public/modules/users/img/buttons/twitter.png differ diff --git a/public/modules/users/services/authentication.client.service.js b/public/modules/users/services/authentication.client.service.js new file mode 100644 index 0000000..56225db --- /dev/null +++ b/public/modules/users/services/authentication.client.service.js @@ -0,0 +1,10 @@ +'use strict'; + +// Authentication service for user variables +angular.module('users').factory('Authentication', ['$window', function($window) { + var auth = { + user: $window.user + }; + + return auth; +}]); diff --git a/public/modules/users/services/users.client.service.js b/public/modules/users/services/users.client.service.js new file mode 100644 index 0000000..664828f --- /dev/null +++ b/public/modules/users/services/users.client.service.js @@ -0,0 +1,12 @@ +'use strict'; + +// Users service used for communicating with the users REST endpoint +angular.module('users').factory('Users', ['$resource', + function($resource) { + return $resource('users', {}, { + update: { + method: 'PUT' + } + }); + } +]); \ No newline at end of file diff --git a/public/modules/users/tests/authentication.client.controller.test.js b/public/modules/users/tests/authentication.client.controller.test.js new file mode 100644 index 0000000..4c95d68 --- /dev/null +++ b/public/modules/users/tests/authentication.client.controller.test.js @@ -0,0 +1,118 @@ +'use strict'; + +(function() { + // Authentication controller Spec + describe('AuthenticationController', function() { + // Initialize global variables + var AuthenticationController, + scope, + $httpBackend, + $stateParams, + $location; + + beforeEach(function() { + jasmine.addMatchers({ + toEqualData: function(util, customEqualityTesters) { + return { + compare: function(actual, expected) { + return { + pass: angular.equals(actual, expected) + }; + } + }; + } + }); + }); + + // Load the main application module + beforeEach(module(ApplicationConfiguration.applicationModuleName)); + + // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_). + // This allows us to inject a service but then attach it to a variable + // with the same name as the service. + beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) { + // Set a new global scope + scope = $rootScope.$new(); + + // Point global variables to injected services + $stateParams = _$stateParams_; + $httpBackend = _$httpBackend_; + $location = _$location_; + + // Initialize the Authentication controller + AuthenticationController = $controller('AuthenticationController', { + $scope: scope + }); + })); + + + it('$scope.signin() should login with a correct user and password', function() { + // Test expected GET request + $httpBackend.when('POST', '/auth/signin').respond(200, 'Fred'); + + scope.signin(); + $httpBackend.flush(); + + // Test scope value + expect(scope.authentication.user).toEqual('Fred'); + expect($location.url()).toEqual('/'); + }); + + it('$scope.signin() should fail to log in with nothing', function() { + // Test expected POST request + $httpBackend.expectPOST('/auth/signin').respond(400, { + 'message': 'Missing credentials' + }); + + scope.signin(); + $httpBackend.flush(); + + // Test scope value + expect(scope.error).toEqual('Missing credentials'); + }); + + it('$scope.signin() should fail to log in with wrong credentials', function() { + // Foo/Bar combo assumed to not exist + scope.authentication.user = 'Foo'; + scope.credentials = 'Bar'; + + // Test expected POST request + $httpBackend.expectPOST('/auth/signin').respond(400, { + 'message': 'Unknown user' + }); + + scope.signin(); + $httpBackend.flush(); + + // Test scope value + expect(scope.error).toEqual('Unknown user'); + }); + + it('$scope.signup() should register with correct data', function() { + // Test expected GET request + scope.authentication.user = 'Fred'; + $httpBackend.when('POST', '/auth/signup').respond(200, 'Fred'); + + scope.signup(); + $httpBackend.flush(); + + // test scope value + expect(scope.authentication.user).toBe('Fred'); + expect(scope.error).toEqual(undefined); + expect($location.url()).toBe('/'); + }); + + it('$scope.signup() should fail to register with duplicate Username', function() { + // Test expected POST request + $httpBackend.when('POST', '/auth/signup').respond(400, { + 'message': 'Username already exists' + }); + + scope.signup(); + $httpBackend.flush(); + + // Test scope value + expect(scope.error).toBe('Username already exists'); + }); + }); +}()); \ No newline at end of file diff --git a/public/modules/users/users.client.module.js b/public/modules/users/users.client.module.js new file mode 100755 index 0000000..7b2f646 --- /dev/null +++ b/public/modules/users/users.client.module.js @@ -0,0 +1,4 @@ +'use strict'; + +// Use Application configuration module to register a new module +ApplicationConfiguration.registerModule('users'); \ No newline at end of file diff --git a/public/modules/users/views/authentication/signin.client.view.html b/public/modules/users/views/authentication/signin.client.view.html new file mode 100644 index 0000000..91e256e --- /dev/null +++ b/public/modules/users/views/authentication/signin.client.view.html @@ -0,0 +1,45 @@ +
+

Sign in using your social accounts

+ +

Or with your account

+
+ +
+
\ No newline at end of file diff --git a/public/modules/users/views/authentication/signup.client.view.html b/public/modules/users/views/authentication/signup.client.view.html new file mode 100644 index 0000000..e205176 --- /dev/null +++ b/public/modules/users/views/authentication/signup.client.view.html @@ -0,0 +1,54 @@ +
+

Sign up using your social accounts

+ +

Or with your email

+
+ +
+
\ No newline at end of file diff --git a/public/modules/users/views/password/forgot-password.client.view.html b/public/modules/users/views/password/forgot-password.client.view.html new file mode 100644 index 0000000..e6275f9 --- /dev/null +++ b/public/modules/users/views/password/forgot-password.client.view.html @@ -0,0 +1,22 @@ +
+

Restore your password

+

Enter your account username.

+
+ +
+
\ No newline at end of file diff --git a/public/modules/users/views/password/reset-password-invalid.client.view.html b/public/modules/users/views/password/reset-password-invalid.client.view.html new file mode 100644 index 0000000..d5fc237 --- /dev/null +++ b/public/modules/users/views/password/reset-password-invalid.client.view.html @@ -0,0 +1,4 @@ +
+

Password reset is invalid

+ Ask for a new password reset +
\ No newline at end of file diff --git a/public/modules/users/views/password/reset-password-success.client.view.html b/public/modules/users/views/password/reset-password-success.client.view.html new file mode 100644 index 0000000..4de46c4 --- /dev/null +++ b/public/modules/users/views/password/reset-password-success.client.view.html @@ -0,0 +1,4 @@ +
+

Password successfully reset

+ Continue to home page +
\ No newline at end of file diff --git a/public/modules/users/views/password/reset-password.client.view.html b/public/modules/users/views/password/reset-password.client.view.html new file mode 100644 index 0000000..dc8b2ea --- /dev/null +++ b/public/modules/users/views/password/reset-password.client.view.html @@ -0,0 +1,26 @@ +
+

Reset your password

+
+ +
+
\ No newline at end of file diff --git a/public/modules/users/views/settings/change-password.client.view.html b/public/modules/users/views/settings/change-password.client.view.html new file mode 100644 index 0000000..9811011 --- /dev/null +++ b/public/modules/users/views/settings/change-password.client.view.html @@ -0,0 +1,30 @@ +
+

Change your password

+
+ +
+
\ No newline at end of file diff --git a/public/modules/users/views/settings/edit-profile.client.view.html b/public/modules/users/views/settings/edit-profile.client.view.html new file mode 100644 index 0000000..a4be680 --- /dev/null +++ b/public/modules/users/views/settings/edit-profile.client.view.html @@ -0,0 +1,34 @@ +
+

Edit your profile

+
+ +
+
\ No newline at end of file diff --git a/public/modules/users/views/settings/social-accounts.client.view.html b/public/modules/users/views/settings/social-accounts.client.view.html new file mode 100644 index 0000000..4712ee0 --- /dev/null +++ b/public/modules/users/views/settings/social-accounts.client.view.html @@ -0,0 +1,29 @@ +
+

Connected social accounts:

+
+ +
+

Connect other social accounts:

+ +
\ No newline at end of file diff --git a/public/robots.txt b/public/robots.txt new file mode 100755 index 0000000..ee2cc21 --- /dev/null +++ b/public/robots.txt @@ -0,0 +1,3 @@ +# robotstxt.org/ + +User-agent: * diff --git a/scripts/generate-ssl-certs.sh b/scripts/generate-ssl-certs.sh new file mode 100644 index 0000000..5caaaa9 --- /dev/null +++ b/scripts/generate-ssl-certs.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ ! -e server.js ] +then + echo "Error: could not find main application server.js file" + echo "You should run the generate-ssl-certs.sh script from the main MEAN application root directory" + echo "i.e: bash scripts/generate-ssl-cers.sh" + exit -1 +fi + +echo "Generating self-signed certificates..." +mkdir -p ./config/sslcerts +openssl genrsa -out ./config/sslcerts/key.pem -aes256 1024 +openssl req -new -key ./config/sslcerts/key.pem -out ./config/sslcerts/csr.pem +openssl x509 -req -days 9999 -in ./config/sslcerts/csr.pem -signkey ./config/sslcerts/key.pem -out ./config/sslcerts/cert.pem +rm ./config/sslcerts/csr.pem +chmod 600 ./config/sslcerts/key.pem ./config/sslcerts/cert.pem diff --git a/server.js b/server.js new file mode 100755 index 0000000..abb888c --- /dev/null +++ b/server.js @@ -0,0 +1,49 @@ +'use strict'; +/** + * Module dependencies. + */ +var init = require('./config/init')(), + config = require('./config/config'), + mongoose = require('mongoose'), + chalk = require('chalk'); + +/** + * Main application entry file. + * Please note that the order of loading is important. + */ + +// Bootstrap db connection +var db = mongoose.connect(config.db.uri, config.db.options, function(err) { + if (err) { + console.error(chalk.red('Could not connect to MongoDB!')); + console.log(chalk.red(err)); + } +}); +mongoose.connection.on('error', function(err) { + console.error(chalk.red('MongoDB connection error: ' + err)); + process.exit(-1); + } +); + +// Init the express application +var app = require('./config/express')(db); + +// Bootstrap passport config +require('./config/passport')(); + +// Start the app by listening on +app.listen(config.port); + +// Expose app +exports = module.exports = app; + +// Logging initialization +console.log('--'); +console.log(chalk.green(config.app.title + ' application started')); +console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV)); +console.log(chalk.green('Port:\t\t\t\t' + config.port)); +console.log(chalk.green('Database:\t\t\t' + config.db.uri)); +if (process.env.NODE_ENV === 'secure') { + console.log(chalk.green('HTTPs:\t\t\t\ton')); +} +console.log('--'); \ No newline at end of file