Skip to content

Commit

Permalink
updated specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniespratley committed Dec 10, 2014
1 parent 16714fc commit d28d3bb
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 122 deletions.
3 changes: 2 additions & 1 deletion app/scripts/controllers/register.coffee
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)

10 changes: 7 additions & 3 deletions app/scripts/services/cmsauthservice.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)->
Expand Down
53 changes: 27 additions & 26 deletions routes/cms-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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} );
}
} );
},
Expand All @@ -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) {
Expand All @@ -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});
}
};

Expand All @@ -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;
Loading

0 comments on commit d28d3bb

Please sign in to comment.