diff --git a/app/scripts/controllers/register.coffee b/app/scripts/controllers/register.coffee index a243883..74647b5 100644 --- a/app/scripts/controllers/register.coffee +++ b/app/scripts/controllers/register.coffee @@ -1,6 +1,6 @@ 'use strict' -angular.module('angularCmsApp').controller 'RegisterCtrl', ($scope, $location, $log, cmsAuthService, cmsSessionService, cmsNotify) -> +angular.module('angularCmsApp').controller 'RegisterCtrl', ($scope, $log, cmsAuthService) -> $scope.awesomeThings = [ 'HTML5 Boilerplate' 'AngularJS' @@ -21,4 +21,5 @@ angular.module('angularCmsApp').controller 'RegisterCtrl', ($scope, $location, $ #Handle registering a user $scope.register = (user)-> $log.info('register', user) + cmsAuthService.register(user) diff --git a/app/scripts/services/cmsauthservice.coffee b/app/scripts/services/cmsauthservice.coffee index 8290b73..1dea7b5 100644 --- a/app/scripts/services/cmsauthservice.coffee +++ b/app/scripts/services/cmsauthservice.coffee @@ -31,9 +31,11 @@ angular.module('angularCmsApp').service 'cmsAuthService', ($q, $http, $log, $roo register - I handle register a user. ### register: (user) -> - $http.post(@endpoint + "/register", user).then( + $log.info('trying to register', user); + return $http.post(@endpoint + "/register", user).then( (res)=> - @authorize(res.data) + $log.info(res); + return @authorize(res.data) , (err) -> $log.error(err) cmsNotify( '.message', 'danger', 'Error!', err.data.message, 4000) @@ -59,7 +61,7 @@ angular.module('angularCmsApp').service 'cmsAuthService', ($q, $http, $log, $roo #Set user session session = - user: res.data.result + user: res.data authorized: true #Set user cookie @@ -68,6 +70,8 @@ angular.module('angularCmsApp').service 'cmsAuthService', ($q, $http, $log, $roo #Set session on scope $rootScope.App.session = session + $log.info('login-result', res) + #Change location $rootScope.App.location.path('/dashboard') , (err)-> diff --git a/routes/cms-auth.js b/routes/cms-auth.js index 06ae36b..554f0d5 100644 --- a/routes/cms-auth.js +++ b/routes/cms-auth.js @@ -3,23 +3,20 @@ var bodyParser = require( 'body-parser' ), util = require('util'), User = require( './models/user' ), session = require( 'express-session' ), - crypto = require( 'crypto' ); + crypto = require( 'crypto'), + bcrypt = require( 'bcrypt-nodejs' ); -var bcrypt = require( 'bcrypt-nodejs' ); - -module.exports = function (config, app) { +var cmsAuth = function (config, app) { console.warn( 'cms-auth' ); - mongoose.connect( config.mongodb ); + //### hashPassword //Hash password using basic sha1 hash. var hashPassword = function (pass, salt) { - var p = bcrypt.hashSync(pass); - - return p; + return bcrypt.hashSync(pass); }; var cmsAuth = { @@ -39,20 +36,20 @@ module.exports = function (config, app) { query.username = req.body.email; } query.password = hashPassword( req.body.password, query.username ); + console.warn('trying to login', query); User.findOne( {username: query.username}, function (err, data) { if (err) { - res.jsonp( 400, err ); + return res.jsonp( 400, err ); } - try { if (data && bcrypt.compareSync(req.body.password, data.password)) { req.session.user = data; - res.jsonp( 200, data ); + return res.json( 200, data ); } else { - res.jsonp( 404, {message: 'Wrong username/password!'} ); + return res.json( 404, {message: 'Wrong username/password!'} ); } } catch (error) { - res.jsonp( 404, {message: error} ); + return res.json( 404, {message: error} ); } } ); }, @@ -70,37 +67,39 @@ module.exports = function (config, app) { data.username = req.body.username; } if (req.body.email) { - data.username = req.body.email; + data.email = req.body.email; } + data.password = hashPassword( req.body.password, data.username ); data.created_at = new Date(); data.updated_at = new Date(); data.active = false; data.groups = ['public']; - var user = new User( data ); + console.warn('trying to register', data); + + //Try and find user User.find( {username: data.username}, function (err, u) { - console.log(err, util.inspect(u, {colors: true})); - + console.log( 'found user', err, util.inspect(u, {colors: true})); + var user = new User( data ); if(err){ - res.json( 400, {message: 'Problem registering!'} ); - } - - if (u.length > 0) { - res.json( 400, {message: 'Username already exists!'} ); + res.jsonp( 400, {message: 'Problem registering!'} ); } - if(!err){ + if (u.length) { + res.jsonp( 400, {message: 'Username already exists!'} ); + } else { user.save( function (er, ok) { if (er) { - res.json( 400, {message: 'Problem registering!'} ); + return res.jsonp( 400, {message: 'Problem registering!'} ); } else { - res.json( 201, ok ); + return res.jsonp( 201, ok ); } } ); } + } ); }, session: function (req, res, next) { @@ -109,7 +108,7 @@ module.exports = function (config, app) { user = req.session.user } console.warn(util.inspect(user, {colors: true})); - res.send({message: 'Your session', data: user}); + return res.send({message: 'Your session', data: user}); } }; @@ -124,3 +123,5 @@ module.exports = function (config, app) { app.post( config.apiBase + '/register', bodyParser.json(), cmsAuth.register ); app.get( config.apiBase + '/session', bodyParser.json(), cmsAuth.session ); }; + +module.exports = cmsAuth; diff --git a/routes/cms-passport.js b/routes/cms-passport.js index c40dd4d..ed32753 100644 --- a/routes/cms-passport.js +++ b/routes/cms-passport.js @@ -1,127 +1,169 @@ -var passport = require( 'passport' ), - BasicStrategy = require( 'passport-http' ).Strategy, - LocalStrategy = require( 'passport-local' ).Strategy, - GoogleStrategy = require( 'passport-google' ).Strategy, - express = require( 'express' ), - path = require( 'path' ), - q = require( 'q' ), - flash = require( 'connect-flash' ), - User = require( './models/user' ), - cookieParser = require( 'cookie-parser' ), - bodyParser = require( 'body-parser' ), - session = require( 'express-session' ); +var passport = require('passport'), + BasicStrategy = require('passport-http').Strategy, + LocalStrategy = require('passport-local').Strategy, + GoogleStrategy = require('passport-google').Strategy, + express = require('express'), + path = require('path'), + q = require('q'), + flash = require('connect-flash'), + User = require('./models/user'), + cookieParser = require('cookie-parser'), + bodyParser = require('body-parser'), + session = require('express-session'), + mongoose = require('mongoose'), + util = require('util'), + User = require('./models/user'), + session = require('express-session'), + crypto = require('crypto'), + bcrypt = require('bcrypt-nodejs'); +/** + * I handle ensuring a user is authenticated. + * @param req + * @param res + * @param next + * @returns {*} + */ var ensureAuthenticated = function (req, res, next) { if (req.isAuthenticated()) { return next(); } - res.redirect( '/login' ); + res.redirect('/login'); }; -module.exports = function (config, app) { - if (!app) { - throw new Error( 'Must provide express application!' ); - } +/** + * I am the cmsPassport module that has various ways of + * authenticating a user. + * @param config - Configuration settings + * @param app - Express app + */ +var cmsPassport = function (config, app) { - passport.serializeUser( function (user, done) { - done( null, user.id ); - } ); - passport.deserializeUser( function (id, done) { - User.findById( id, function (err, user) { - done( err, user ); - } ); - } ); + if (!app) { + throw new Error('Must provide express application!'); + } - passport.use( new LocalStrategy( { - usernameField: 'email', + /** + * I handle serializing a user. + * @param user + * @param done + */ + var serializeUser = function (user, done) { + done(null, user.id); + }; + + /** + * I handle deserializing a user. + * @param id + * @param done + */ + var deserializeUser = function (id, done) { + User.findById(id, function (err, user) { + done(err, user); + }); + }; + + passport.serializeUser(serializeUser); + + passport.deserializeUser(deserializeUser); + + passport.use(new LocalStrategy({ + usernameField: 'username', passwordField: 'password', passReqToCallback: true }, function (username, password, done) { - process.nextTick( function () { - console.warn( 'find by username' ); - User.findByUsername( username ).then( function (user) { - return done( null, user ); - }, function (err) { - return done( null, false ); - } ); - } ); + console.warn('username', username, password); + process.nextTick(function () { + console.warn('find by username'); + User.find({ + username: username + }, function (err, user) { + console.warn(err, user); + if (err) { + return done(null, false); + } + return done(null, user); + }); + }); } - ) ); + )); - passport.use( new GoogleStrategy( { - returnURL: 'http://localhost:8181/auth/google/return', realm: 'http://localhost:8181/' + passport.use(new GoogleStrategy({ + returnURL: 'http://localhost:8181/auth/google/return', + realm: 'http://localhost:8181/' }, function (identifier, profile, done) { - console.warn( 'googleCallback', profile ); + console.warn('googleCallback', profile); profile.openId = identifier; - User.findOrCreate( profile, function (err, user) { - done( err, user ); - } ); + User.findOrCreate(profile, function (err, user) { + done(err, user); + }); } - ) ); - - app.use( express.static( path.resolve( __dirname, '../www' ) ) ); - app.set( 'views', path.resolve( __dirname, '../www' ) ); - app.set( 'view engine', 'ejs' ); - app.engine( 'ejs', require( 'ejs-locals' ) ); - app.use( cookieParser() ); - app.use( bodyParser.urlencoded( {extended: false} ) ); - app.use( bodyParser.json() ); - app.use( session( { + )); + + app.use(express.static(path.resolve(__dirname, '../www'))); + app.set('views', path.resolve(__dirname, '../www')); + app.set('view engine', 'ejs'); + app.engine('ejs', require('ejs-locals')); + app.use(cookieParser()); + app.use(bodyParser.urlencoded({extended: false})); + app.use(bodyParser.json()); + app.use(session({ secret: 'angular-cms', resave: true, saveUninitialized: true - } ) ); - app.use( passport.initialize() ); - app.use( passport.session() ); - app.use( flash() ); + })); + app.use(passport.initialize()); + app.use(passport.session()); + app.use(flash()); - app.get( '/api/me', passport.authenticate( 'basic', {session: false} ), function (req, res) { - res.json( req.user ); - } ); + app.get('/api/me', passport.authenticate('basic', {session: false}), function (req, res) { + res.json(req.user); + }); - app.all( '*', function (req, res, next) { - console.warn( 'cmsAuth', req.params ); + app.all('*', function (req, res, next) { + console.warn('cmsAuth', req.params); next(); - } ); + }); - app.get( '/', function (req, res) { - res.render( 'index', {user: req.user, message: 'Please login', status: 'info'} ); - } ); + app.get('/', function (req, res) { + res.render('index', {user: req.user, message: 'Please login', status: 'info'}); + }); - app.get( '/account', ensureAuthenticated, function (req, res) { - res.render( 'account', {user: req.user} ); - } ); + app.get('/account', ensureAuthenticated, function (req, res) { + res.render('account', {user: req.user}); + }); - app.get( '/login', function (req, res) { - res.render( 'login', {user: req.user, message: 'Please login', status: 'warning'} ); - } ); + app.get('/login', function (req, res) { + res.render('login', {user: req.user, message: 'Please login', status: 'warning'}); + }); - app.post( '/login', - passport.authenticate( 'local', { + app.post('/login', + passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login', failureFlash: false - } ) + }) ); - app.get( '/auth/user', ensureAuthenticated, function (req, res) { - res.json( 200, req.user ); - } ); + app.get('/auth/user', ensureAuthenticated, function (req, res) { + res.json(200, req.user); + }); - app.get( '/auth/logout', function (req, res) { + app.get('/auth/logout', function (req, res) { req.logout(); - res.redirect( options.apiBase ); - } ); + res.redirect(options.apiBase); + }); - app.get( '/auth/google', passport.authenticate( 'google' ) ); - app.get( '/auth/google/return', - passport.authenticate( 'google', { + app.get('/auth/google', passport.authenticate('google')); + app.get('/auth/google/return', + passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' - } ) ); + })); -}; +}; +module.exports = cmsPassport; diff --git a/routes/cms-routes.js b/routes/cms-routes.js index ed8b6f0..6042580 100644 --- a/routes/cms-routes.js +++ b/routes/cms-routes.js @@ -1,4 +1,7 @@ +var mongoose = require('mongoose'); + module.exports.mount = function (config, app) { + mongoose.connect(config.mongodb); require( './cms-auth' )( config, app ); require( './cms-passport' )( config, app ); require( './cms-rest' )( config, app ); diff --git a/routes/rest.js b/routes/rest.js index a6b68b3..44e3fd4 100755 --- a/routes/rest.js +++ b/routes/rest.js @@ -41,8 +41,8 @@ var MESSAGES = { }; var DS = require('jps-ds').DS; var _ds = new DS({ - //host: 'angularcms:angularcms@paulo.mongohq.com:10089/app19632340', - host: 'localhost/angular-cms', + host: 'angularcms:angularcms@paulo.mongohq.com:10089/app19632340', + //host: 'localhost/angular-cms', models: { 'groups': { title: String, diff --git a/server.js b/server.js index 1570592..cc22780 100755 --- a/server.js +++ b/server.js @@ -57,7 +57,8 @@ var cmsRoutes = require('./routes/cms-routes'); cmsRoutes.mount(config, app); var webapp = http.createServer(app).listen(config.port || process.env.PORT, function () { - util.log(String('App listening on port: ' + config.port).verbose); + util.log('App listening on port: ' + config.port + ''.verbose); + util.log(util.inspect(config, {colors: true})); }); diff --git a/test/protractor/pages/register-page.coffee b/test/protractor/pages/register-page.coffee index 15c3d7e..321dba9 100644 --- a/test/protractor/pages/register-page.coffee +++ b/test/protractor/pages/register-page.coffee @@ -19,7 +19,6 @@ RegisterPage = @password2.sendKeys(password) @agree.click().then(()=> @submit.click() - browser.sleep(1000) ) diff --git a/test/protractor/spec/login-spec.coffee b/test/protractor/spec/login-spec.coffee index 2439953..5db627a 100644 --- a/test/protractor/spec/login-spec.coffee +++ b/test/protractor/spec/login-spec.coffee @@ -9,6 +9,6 @@ describe 'Login:', -> afterEach -> loginPage.logout() it 'should allow a user to login', -> - loginPage.login('test@email.com', 'test').then(()-> + loginPage.login('test@gmail.com', 'test').then(()-> expect(browser.getLocationAbsUrl()).toContain '/dashboard' ) diff --git a/test/routes/cms-auth-spec.js b/test/routes/cms-auth-spec.js index e69de29..aff20fb 100644 --- a/test/routes/cms-auth-spec.js +++ b/test/routes/cms-auth-spec.js @@ -0,0 +1,17 @@ + + +describe('cms-auth', function(){ + it('should have /auth/login route', function (done) { + // + done(); + }); + it('should have /auth/register route', function (done) { + // + done(); + }); + it('should have /auth/me route', function (done) { + // + done(); + }); + +}); diff --git a/test/routes/cms-passport-spec.js b/test/routes/cms-passport-spec.js index e69de29..dbcb43b 100644 --- a/test/routes/cms-passport-spec.js +++ b/test/routes/cms-passport-spec.js @@ -0,0 +1,67 @@ +"use strict"; +var request = require('supertest'); +var path = require('path'); +var fs = require('fs'); +var expect = require('chai').expect; +var express = require('express'); +var app = express(); +var config = JSON.parse(fs.readFileSync(process.cwd() + '/config/config.json')); +config.port = 9191 + +var cmsPassport = require(process.cwd() + '/routes/cms-passport')(config, app); +describe('cms-passport', function () { + it('should have /account route', function (done) { + done(); + }); + it('should have /auth/login route', function (done) { + // + + it('POST - /login - should return user on successful login', function (done) { + var validUser = { + username: 'test@email.com', + password: 'test' + }; + request(app) + .post('/login') + .send(validUser) + .expect("Content-Type", /json/) + .expect(200, done); + }); + }); + + it('POST - /register - should return user on successful registation', function (done) { + request(app) + .post('/register') + .send({ + "username": Date.now() + "test@email.com", + "email": Date.now() + "test@email.com", + "password": "test", + "metadata": { + "avatar": "", + "name": "Jonnie Dollas" + } + }) + .expect("Content-Type", /json/) + .expect(201, done); + }); + it('should have /auth/register route', function (done) { + // + done(); + }); + it('should have /auth/me route', function (done) { + // + done(); + }); + + describe('Google Auth', function () { + it('should have a /auth/google route', function (done) { + // + done(); + }); + it('should have a /auth/google/callback route', function (done) { + // + done(); + }); + }); + +}); diff --git a/test/routes/cms-rest-spec.js b/test/routes/cms-rest-spec.js index e69de29..cb3d0ac 100644 --- a/test/routes/cms-rest-spec.js +++ b/test/routes/cms-rest-spec.js @@ -0,0 +1,20 @@ +describe('cms-rest', function () { + it('should have a /collection/:id route', function (done) { + // + done(); + }); + it('GET - /collection - should return array of items', function (done) { + done(); + }); + it('GET - /collection/:id - should return object item', function (done) { + // + done(); + }); + it('POST - /collection - should return object on success', function (done) { + // + done(); + }); + it('PUT - /collection/:id - should return object on success', function (done) { + done(); + }); +}); \ No newline at end of file diff --git a/test/routes/rest-spec.js b/test/routes/rest-spec.js index 225ed7d..0537429 100644 --- a/test/routes/rest-spec.js +++ b/test/routes/rest-spec.js @@ -34,14 +34,22 @@ describe('Testing: API Server', function () { it('POST - /api/v2/users/register - should return user on successful registation', function (done) { request(app) .post('/api/v2/register') - .send(postData) + .send({ + "username": Date.now() + "test@email.com", + "email": Date.now() + "test@email.com", + "password": "test", + "metadata": { + "avatar": "", + "name": "Jonnie Dollas" + } + }) .expect("Content-Type", /json/) .expect(201, done); }); it('POST - /api/v2/users/login - should return user on successful login', function (done) { var validUser = { - username: 'test@email.com', + username: 'test@gmail.com', password: 'test' }; request(app)