diff --git a/Gruntfile.js b/Gruntfile.js index 5eacb20..6c91946 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -330,7 +330,7 @@ module.exports = function (grunt) { html: ['<%= yeoman.views %>/*.html'] } }, - ngmin: { + ngAnnotate: { dist: { files: [{ expand: true, @@ -383,7 +383,7 @@ module.exports = function (grunt) { 'concurrent:dist', 'autoprefixer', 'concat', - 'ngmin', + 'ngAnnotate', 'copy:dist', 'cdnify', 'cssmin', diff --git a/README.md b/README.md index c28a98f..75f09ea 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,12 @@ Try out the app: (Heroku app may have go ## How to use angular-passport -Before you continue, make sure you have MongoDB installed . +Before you continue, make sure you have the following installed; + +* MongoDB +* Ruby + * compass (gem install compass) +* grunt-cli (npm install -g grunt-cli) ### Setup Run `npm install`, followed by `bower install` to grab the dependencies. diff --git a/app/scripts/app.js b/app/scripts/app.js index 4bd8fc0..f284635 100644 --- a/app/scripts/app.js +++ b/app/scripts/app.js @@ -8,7 +8,7 @@ angular.module('angularPassportApp', [ 'http-auth-interceptor', 'ui.bootstrap' ]) - .config(function ($routeProvider, $locationProvider) { + .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { $routeProvider .when('/', { templateUrl: 'partials/main.html', @@ -42,15 +42,26 @@ angular.module('angularPassportApp', [ redirectTo: '/' }); $locationProvider.html5Mode(true); - }) + }]) - .run(function ($rootScope, $location, Auth) { + .run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) { //watching the value of the currentUser variable. $rootScope.$watch('currentUser', function(currentUser) { // if no currentUser and on a page that requires authorization then try to update it // will trigger 401s if user does not have a valid session - if (!currentUser && (['/', '/login', '/logout', '/signup'].indexOf($location.path()) == -1 )) { + var isValidPath = false, validPaths = [/^\/$/, /^\/blogs$/, /^\/blogs\/[0-9a-zA-Z]*$/, /^\/login$/, /^\/logout$/, /^\/signup$/]; + var path = $location.path(); + // because /blogs/create matches one of the regular expressions, leave isValidPath=false + if (path != '/blogs/create') { + for (var i = 0; i < validPaths.length; i++) { + if (path.search(validPaths[i]) >= 0) { + isValidPath = true; + break; + } + } + } + if (!currentUser && !isValidPath) { Auth.currentUser(); } }); @@ -60,4 +71,4 @@ angular.module('angularPassportApp', [ $location.path('/login'); return false; }); - }); \ No newline at end of file + }]); \ No newline at end of file diff --git a/app/scripts/controllers/blogs.js b/app/scripts/controllers/blogs.js index 1137d44..9130c88 100644 --- a/app/scripts/controllers/blogs.js +++ b/app/scripts/controllers/blogs.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('angularPassportApp') - .controller('BlogsCtrl', function ($scope, Blogs, $location, $routeParams, $rootScope) { + .controller('BlogsCtrl', ['$scope', 'Blogs', '$location', '$routeParams', '$rootScope', function ($scope, Blogs, $location, $routeParams, $rootScope) { $scope.create = function() { var blog = new Blogs({ @@ -46,4 +46,4 @@ angular.module('angularPassportApp') $scope.blog = blog; }); }; - }); + }]); \ No newline at end of file diff --git a/app/scripts/controllers/login.js b/app/scripts/controllers/login.js index 389aa6d..0bf54bb 100644 --- a/app/scripts/controllers/login.js +++ b/app/scripts/controllers/login.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('angularPassportApp') - .controller('LoginCtrl', function ($scope, Auth, $location) { + .controller('LoginCtrl', ['$scope', 'Auth', '$location', function ($scope, Auth, $location) { $scope.error = {}; $scope.user = {}; @@ -24,4 +24,4 @@ angular.module('angularPassportApp') } }); }; - }); \ No newline at end of file + }]); \ No newline at end of file diff --git a/app/scripts/controllers/main.js b/app/scripts/controllers/main.js index 705024b..2460130 100644 --- a/app/scripts/controllers/main.js +++ b/app/scripts/controllers/main.js @@ -1,5 +1,5 @@ 'use strict'; angular.module('angularPassportApp') - .controller('MainCtrl', function ($scope) { - }); + .controller('MainCtrl', ['$scope', function ($scope) { + }]); \ No newline at end of file diff --git a/app/scripts/controllers/navbar.js b/app/scripts/controllers/navbar.js index 096fb8d..cbf07fd 100644 --- a/app/scripts/controllers/navbar.js +++ b/app/scripts/controllers/navbar.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('angularPassportApp') - .controller('NavbarCtrl', function ($scope, Auth, $location) { + .controller('NavbarCtrl', ['$scope', 'Auth', '$location', function ($scope, Auth, $location) { $scope.menu = [{ "title": "Blogs", "link": "blogs" @@ -19,4 +19,4 @@ angular.module('angularPassportApp') } }); }; - }); + }]); diff --git a/app/scripts/controllers/signup.js b/app/scripts/controllers/signup.js index 938ed39..0ea9e9b 100644 --- a/app/scripts/controllers/signup.js +++ b/app/scripts/controllers/signup.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('angularPassportApp') - .controller('SignupCtrl', function ($scope, Auth, $location) { + .controller('SignupCtrl', ['$scope', 'Auth', '$location', function ($scope, Auth, $location) { $scope.register = function(form) { Auth.createUser({ email: $scope.user.email, @@ -22,4 +22,4 @@ angular.module('angularPassportApp') } ); }; - }); \ No newline at end of file + }]); \ No newline at end of file diff --git a/app/scripts/directives/uniqueUsername.js b/app/scripts/directives/uniqueUsername.js index ea391df..b0ebb8e 100644 --- a/app/scripts/directives/uniqueUsername.js +++ b/app/scripts/directives/uniqueUsername.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('angularPassportApp') - .directive('uniqueUsername', function ($http) { + .directive('uniqueUsername', ['$http', function ($http) { return { restrict: 'A', require: 'ngModel', @@ -25,5 +25,4 @@ angular.module('angularPassportApp') }, validate); } }; - }); - + }]); \ No newline at end of file diff --git a/app/scripts/services/Auth.js b/app/scripts/services/Auth.js index 8dfbf1f..86f6583 100644 --- a/app/scripts/services/Auth.js +++ b/app/scripts/services/Auth.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('angularPassportApp') - .factory('Auth', function Auth($location, $rootScope, Session, User, $cookieStore) { + .factory('Auth', ['$location', '$rootScope', 'Session', 'User', '$cookieStore', function Auth($location, $rootScope, Session, User, $cookieStore) { $rootScope.currentUser = $cookieStore.get('user') || null; $cookieStore.remove('user'); @@ -78,4 +78,4 @@ angular.module('angularPassportApp') }); } }; - }) \ No newline at end of file + }]); \ No newline at end of file diff --git a/app/scripts/services/Blogs.js b/app/scripts/services/Blogs.js index e5330c6..6216837 100644 --- a/app/scripts/services/Blogs.js +++ b/app/scripts/services/Blogs.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('angularPassportApp') - .factory('Blogs', function ($resource) { + .factory('Blogs', ['$resource', function ($resource) { return $resource('api/blogs/:blogId', { blogId: '@_id' }, { @@ -9,4 +9,4 @@ angular.module('angularPassportApp') method: 'PUT' } }); - }); + }]); diff --git a/app/scripts/services/Session.js b/app/scripts/services/Session.js index fd22b18..74196ff 100644 --- a/app/scripts/services/Session.js +++ b/app/scripts/services/Session.js @@ -1,6 +1,6 @@ 'use strict'; angular.module('angularPassportApp') - .factory('Session', function ($resource) { + .factory('Session', ['$resource', function ($resource) { return $resource('/auth/session/'); - }); + }]); diff --git a/app/scripts/services/User.js b/app/scripts/services/User.js index 4ba91e4..93392ff 100644 --- a/app/scripts/services/User.js +++ b/app/scripts/services/User.js @@ -1,11 +1,11 @@ 'use strict'; angular.module('angularPassportApp') - .factory('User', function ($resource) { + .factory('User', ['$resource', function ($resource) { return $resource('/auth/users/:id/', {}, { 'update': { method:'PUT' } }); - }); + }]); diff --git a/app/views/index.html b/app/views/index.html index 03e9909..b3e0549 100644 --- a/app/views/index.html +++ b/app/views/index.html @@ -15,7 +15,7 @@ - + - - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/app/views/partials/navbar.html b/app/views/partials/navbar.html index 7adf496..76729c7 100644 --- a/app/views/partials/navbar.html +++ b/app/views/partials/navbar.html @@ -20,7 +20,7 @@ diff --git a/bower.json b/bower.json index 828c29c..c413e7a 100644 --- a/bower.json +++ b/bower.json @@ -2,20 +2,23 @@ "name": "angular-passport", "version": "0.0.0", "dependencies": { - "angular": "~1.2.1", - "json3": "~3.2.4", - "es5-shim": "~2.1.0", - "jquery": "~1.10.2", - "sass-bootstrap": "~3.0.0", - "angular-resource": "~1.2.0", - "angular-cookies": "~1.2.0", - "angular-sanitize": "~1.2.0", - "angular-route": "~1.2.0", - "angular-http-auth": "*", - "angular-bootstrap": "~0.7.0" + "angular": "~1.2.23", + "json3": "~3.3.2", + "es5-shim": "~4.0.3", + "jquery": "~1.11.1", + "sass-bootstrap": "*", + "angular-resource": "~1.2.23", + "angular-cookies": "~1.2.23", + "angular-sanitize": "~1.2.23", + "angular-route": "~1.2.23", + "angular-http-auth": "~1.2.1", + "angular-bootstrap": "~0.11.0" }, "devDependencies": { - "angular-mocks": "~1.2.0", - "angular-scenario": "~1.2.0" + "angular-mocks": "~1.2.23", + "angular-scenario": "~1.2.23" + }, + "resolutions": { + "angular": "~1.2.23" } } diff --git a/karma.conf.js b/karma.conf.js index ea75852..19668ca 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -11,7 +11,7 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ - 'app/bower_components/jquery/jquery.js', + 'app/bower_components/jquery/dist/jquery.js', 'app/bower_components/angular/angular.js', 'app/bower_components/angular-mocks/angular-mocks.js', 'app/bower_components/angular-resource/angular-resource.js', @@ -21,8 +21,7 @@ module.exports = function(config) { 'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js', 'app/bower_components/angular-http-auth/src/http-auth-interceptor.js', 'app/scripts/*.js', - 'app/scripts/**/*.js', - 'test/mock/**/*.js', + 'app/scripts/**/*.js', //'test/mock/**/*.js', 'test/spec/**/*.js' ], diff --git a/lib/config/auth.js b/lib/config/auth.js index a4ccc2f..0bf7485 100644 --- a/lib/config/auth.js +++ b/lib/config/auth.js @@ -5,7 +5,7 @@ */ exports.ensureAuthenticated = function ensureAuthenticated(req, res, next) { if (req.isAuthenticated()) { return next(); } - res.send(401); + res.status(401).end(); } /** @@ -14,7 +14,7 @@ exports.ensureAuthenticated = function ensureAuthenticated(req, res, next) { exports.blog = { hasAuthorization: function(req, res, next) { if (req.blog.creator._id.toString() !== req.user._id.toString()) { - return res.send(403); + return res.status(403).end(); } next(); } diff --git a/lib/config/config.js b/lib/config/config.js index 3f64ea2..7fb66b6 100644 --- a/lib/config/config.js +++ b/lib/config/config.js @@ -1,5 +1,5 @@ module.exports = { - port: process.env.PORT || 3000, + port: process.env.PORT || 3000, db: process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/test' diff --git a/lib/config/routes.js b/lib/config/routes.js index f79f12d..a58b30e 100644 --- a/lib/config/routes.js +++ b/lib/config/routes.js @@ -17,7 +17,7 @@ module.exports = function(app) { var session = require('../controllers/session'); app.get('/auth/session', auth.ensureAuthenticated, session.session); app.post('/auth/session', session.login); - app.del('/auth/session', session.logout); + app.delete('/auth/session', session.logout); // Blog Routes var blogs = require('../controllers/blogs'); @@ -25,7 +25,7 @@ module.exports = function(app) { app.post('/api/blogs', auth.ensureAuthenticated, blogs.create); app.get('/api/blogs/:blogId', blogs.show); app.put('/api/blogs/:blogId', auth.ensureAuthenticated, auth.blog.hasAuthorization, blogs.update); - app.del('/api/blogs/:blogId', auth.ensureAuthenticated, auth.blog.hasAuthorization, blogs.destroy); + app.delete('/api/blogs/:blogId', auth.ensureAuthenticated, auth.blog.hasAuthorization, blogs.destroy); //Setting up the blogId param app.param('blogId', blogs.blog); diff --git a/lib/controllers/session.js b/lib/controllers/session.js index e9cf3e6..9c8e070 100644 --- a/lib/controllers/session.js +++ b/lib/controllers/session.js @@ -18,7 +18,7 @@ exports.session = function (req, res) { exports.logout = function (req, res) { if(req.user) { req.logout(); - res.send(200); + res.status(200).end(); } else { res.send(400, "Not logged in"); } diff --git a/lib/controllers/users.js b/lib/controllers/users.js index 979e74f..530595d 100644 --- a/lib/controllers/users.js +++ b/lib/controllers/users.js @@ -62,4 +62,4 @@ exports.exists = function (req, res, next) { res.json({exists: false}); } }); -} +} \ No newline at end of file diff --git a/lib/db/mongo.js b/lib/db/mongo.js index 27fc206..4a6c0bb 100644 --- a/lib/db/mongo.js +++ b/lib/db/mongo.js @@ -2,10 +2,11 @@ var mongoose = require('mongoose'), config = require('../config/config') -exports.mongoose = mongoose; var mongoOptions = { db: { safe: true } }; +exports.mongoose = mongoose; + // Connect to Database exports.db = mongoose.connect(config.db, mongoOptions, function (err, res) { if (err) { @@ -13,4 +14,4 @@ exports.db = mongoose.connect(config.db, mongoOptions, function (err, res) { } else { console.log ('Successfully connected to: ' + config.db); } -}); +}); \ No newline at end of file diff --git a/lib/models/user.js b/lib/models/user.js index e310db9..7c05583 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -122,4 +122,4 @@ UserSchema.methods = { } }; -mongoose.model('User', UserSchema); +mongoose.model('User', UserSchema); \ No newline at end of file diff --git a/package.json b/package.json index a138d8e..18a0e35 100644 --- a/package.json +++ b/package.json @@ -2,54 +2,62 @@ "name": "angularpassport", "version": "0.0.0", "dependencies": { - "express": "~3.4.3", - "passport": "~0.1.15", - "passport-local": "~0.1.6", - "mongoose": "~3.5.5", - "ejs": "~0.8.4", - "underscore": "~1.5.2", - "connect-mongo": "~0.4.0" + "express": "~4.8.5", + "body-parser": "~1.6.5", + "multer": "~0.1.4", + "errorhandler": "~1.1.1", + "express-session": "~1.7.6", + "method-override": "~2.1.3", + "morgan": "~1.2.3", + "serve-favicon": "~2.0.1", + "cookie-parser": "~1.3.2", + "connect-mongo": "~0.4.1", + "passport": "~0.2.0", + "passport-local": "~1.0.0", + "mongoose": "~3.8.15", + "ejs": "~1.0.0", + "underscore": "~1.6.0" }, "devDependencies": { - "grunt": "~0.4.1", - "grunt-autoprefixer": "~0.4.0", - "grunt-concurrent": "~0.4.1", - "grunt-contrib-clean": "~0.5.0", - "grunt-contrib-coffee": "~0.7.0", - "grunt-contrib-compass": "~0.6.0", - "grunt-contrib-concat": "~0.3.0", - "grunt-contrib-copy": "~0.4.1", - "grunt-contrib-cssmin": "~0.7.0", - "grunt-contrib-htmlmin": "~0.1.3", - "grunt-contrib-imagemin": "~0.3.0", - "grunt-contrib-jshint": "~0.7.1", - "grunt-contrib-uglify": "~0.2.0", - "grunt-contrib-watch": "~0.5.2", - "grunt-google-cdn": "~0.2.0", - "grunt-ngmin": "~0.0.2", + "grunt": "~0.4.5", + "grunt-autoprefixer": "~1.0.1", + "grunt-concurrent": "~0.5.0", + "grunt-contrib-clean": "~0.6.0", + "grunt-contrib-coffee": "~0.11.1", + "grunt-contrib-compass": "~0.9.1", + "grunt-contrib-concat": "~0.5.0", + "grunt-contrib-copy": "~0.5.0", + "grunt-contrib-cssmin": "~0.10.0", + "grunt-contrib-htmlmin": "~0.3.0", + "grunt-contrib-imagemin": "~0.8.0", + "grunt-contrib-jshint": "~0.10.0", + "grunt-contrib-uglify": "~0.5.1", + "grunt-contrib-watch": "~0.6.1", + "grunt-google-cdn": "~0.4.1", + "grunt-ng-annotate": "~0.3.2", "grunt-rev": "~0.1.0", - "grunt-svgmin": "~0.2.0", - "grunt-usemin": "~2.0.0", - "jshint-stylish": "~0.1.3", - "load-grunt-tasks": "~0.2.0", - "time-grunt": "~0.2.1", - "grunt-express-server": "~0.4.5", - "grunt-open": "~0.2.0", + "grunt-svgmin": "~0.4.0", + "grunt-usemin": "~2.3.0", + "jshint-stylish": "~0.4.0", + "load-grunt-tasks": "~0.6.0", + "time-grunt": "~1.0.0", + "grunt-express-server": "~0.4.19", + "grunt-open": "~0.2.3", "karma-ng-scenario": "~0.1.0", - "grunt-karma": "~0.6.2", - "karma-requirejs": "~0.1.0", - "karma-firefox-launcher": "~0.1.0", + "grunt-karma": "~0.8.3", + "karma-requirejs": "~0.2.2", + "karma-firefox-launcher": "~0.1.3", "karma-html2js-preprocessor": "~0.1.0", "karma-script-launcher": "~0.1.0", - "karma-chrome-launcher": "~0.1.0", - "karma-jasmine": "~0.1.3", - "karma-coffee-preprocessor": "~0.1.0", - "karma-phantomjs-launcher": "~0.1.0", - "karma": "~0.10.4", + "karma-chrome-launcher": "~0.1.4", + "karma-jasmine": "~0.1.5", + "karma-coffee-preprocessor": "~0.2.1", + "karma-phantomjs-launcher": "~0.1.4", + "karma": "~0.12.22", "karma-ng-html2js-preprocessor": "~0.1.0" }, "engines": { - "node": ">=0.8.0" + "node": ">=0.10.0" }, "scripts": { "test": "grunt test" diff --git a/server.js b/server.js index 7857a2a..c25e217 100644 --- a/server.js +++ b/server.js @@ -2,11 +2,19 @@ // Module dependencies. var express = require('express'), + favicon = require('serve-favicon'), + logger = require('morgan'), + methodOverride = require('method-override'), + session = require('express-session'), + bodyParser = require('body-parser'), + multer = require('multer'), + errorHandler = require('errorhandler'), + cookieParser = require('cookie-parser'), http = require('http'), passport = require('passport'), path = require('path'), fs = require('fs'), - mongoStore = require('connect-mongo')(express), + MongoStore = require('connect-mongo')(session), config = require('./lib/config/config'); var app = express(); @@ -23,45 +31,48 @@ fs.readdirSync(modelsPath).forEach(function (file) { var pass = require('./lib/config/pass'); // App Configuration -app.configure('development', function(){ - app.use(express.static(path.join(__dirname, '.tmp'))); - app.use(express.static(path.join(__dirname, 'app'))); - app.use(express.errorHandler()); +app.engine('html', require('ejs').renderFile); +app.set('view engine', 'html'); +if ('development' == app.get('env')) { app.set('views', __dirname + '/app/views'); -}); - -app.configure('production', function(){ - app.use(express.favicon(path.join(__dirname, 'public', 'favicon.ico'))); - app.use(express.static(path.join(__dirname, 'public'))); +} else if ('production' == app.get('env')) { app.set('views', __dirname + '/views'); -}); + app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); +} -app.engine('html', require('ejs').renderFile); -app.set('view engine', 'html'); -app.use(express.logger('dev')); +app.use(logger('dev')); +app.use(methodOverride()); // cookieParser should be above session -app.use(express.cookieParser()); - -// bodyParser should be above methodOverride -app.use(express.bodyParser()); -app.use(express.methodOverride()); +app.use(cookieParser()); // express/mongo session storage -app.use(express.session({ +app.use(session({ + resave: true, + saveUninitialized: true, secret: 'MEAN', - store: new mongoStore({ + store: new MongoStore({ url: config.db, collection: 'sessions' }) })); +// bodyParser should be above methodOverride +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); +app.use(multer()); + // use passport session app.use(passport.initialize()); app.use(passport.session()); -//routes should be at the last -app.use(app.router); +if ('development' == app.get('env')) { + app.use(express.static(path.join(__dirname, '.tmp'))); + app.use(express.static(path.join(__dirname, 'app'))); + app.use(errorHandler()); +} else if ('production' == app.get('env')) { + app.use(express.static(path.join(__dirname, 'public'))); +} //Bootstrap routes require('./lib/config/routes')(app);