Skip to content

Commit

Permalink
updated specs login/register
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniespratley committed Dec 9, 2014
1 parent dd7f013 commit 035124e
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 48 deletions.
5 changes: 3 additions & 2 deletions app/scripts/controllers/login.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ angular.module('angularCmsApp').controller 'LoginCtrl', ($scope, $rootScope, $co
#Change location
$rootScope.App.location.path('/dashboard')
,
(error)->
cmsNotify( '.login-message', 'danger', 'Error!', error.message, 2500)
(err)->
console.error(err)
cmsNotify( '.login-message', 'danger', 'Error!', err.data.message, 2500)
)

###
Expand Down
8 changes: 4 additions & 4 deletions app/scripts/controllers/register.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ angular.module('angularCmsApp').controller 'RegisterCtrl', ($scope, $location, $

#Welcome the user
cmsNotify( '.message', 'info', 'Registered!', "You have registered as #{data.user.email}")

#Set user session
session =
user: data.user
Expand All @@ -45,7 +45,7 @@ angular.module('angularCmsApp').controller 'RegisterCtrl', ($scope, $location, $
$location.path('/dashboard')
)
,
(error) ->
$log.error(error)
cmsNotify( '.message', 'danger', 'Error!', error.message, 4000)
(err) ->
$log.error(err)
cmsNotify( '.message', 'danger', 'Error!', err.data.message, 4000)
)
6 changes: 2 additions & 4 deletions app/scripts/controllers/users.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ angular.module('angularCmsApp').controller('UsersCtrl', ($scope, DataService) ->
email: null
password: null
role: 'member'
created: new Date()
modified: new Date()
created_at: new Date()
updated_at: new Date()
metadata:
avatar: ''
name: null
Expand Down Expand Up @@ -44,7 +44,6 @@ angular.module('angularCmsApp').controller('UsersCtrl', ($scope, DataService) ->
#Delete user
$scope.deleteUser = (index, user) ->
ask = confirm "Delete #{user.email}?"

if ask
DataService.destroy('users', user).then((res) ->
$scope.users.pop(index)
Expand All @@ -53,7 +52,6 @@ angular.module('angularCmsApp').controller('UsersCtrl', ($scope, DataService) ->

#Add user to database
$scope.addUser = (user) ->
user.created_at = new Date()
user.updated_at = new Date()
DataService.save('users', user).then((data) ->
$scope.getUsers()
Expand Down
9 changes: 5 additions & 4 deletions app/views/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ <h1><i class="fa fa-1x fa-group"></i> Users
<thead>
<tr>
<th class="tc"><input type="checkbox"></th>
<th>Username</th>
<th>E-mail</th>

<th>Active</th>
<th>Created</th>
<th>Last Updated</th>
<th class="actions">Actions</th>
</tr>
</thead>
Expand All @@ -26,13 +26,14 @@ <h1><i class="fa fa-1x fa-group"></i> Users
<td class="tc">
<input type="checkbox" value="{{user._id}}">
</td>
<td>{{ user.username }}</td>
<td> {{user.email}}
{{ user.username }}

<span class="label label-primary" ng-repeat="group in user.groups">{{group}}</span>
</td>

<td> {{user.active}}</td>
<td> {{user.created | date:'medium'}}</td>
<td> {{user.updated_at | date:'medium'}}</td>
<td class="actions">
<div class="btn-group">
<button type="button" class="btn btn-default btn-sm" ng-click="selectUser(user);" data-toggle="modal"
Expand Down
3 changes: 0 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@
},
"devDependencies": {
"angular-mocks": "~1.2.21"
},
"resolutions": {
"bootstrap": "~3.2.0"
}
}
2 changes: 1 addition & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"apiBase": "/api/v2",
"version": "v2",
"security": {"salt": "angular-cms"},
"mongodb": "angularcms:[email protected]:10089/app19632340",
"mongodb": "localhost/angular-cms",
"db": {
"name": "angular-cms",
"username": "amadmin",
Expand Down
52 changes: 31 additions & 21 deletions routes/cms-auth.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
var bodyParser = require( 'body-parser' ),
mongoose = require( 'mongoose' ),
util = require('util'),
User = require( './models/user' ),
session = require( 'express-session' ),
crypto = require( 'crypto' );

var bcrypt = require( 'bcrypt-nodejs' );



module.exports = function (config, app) {
console.warn( 'cms-auth' );

mongoose.connect( config.mongodb );

//### hashPassword
//Hash password using basic sha1 hash.
var hashPassword = function (pass) {
var shasum = crypto.createHash( 'sha1' );
shasum.update( config.security.salt + pass );
//### hashPassword
//Hash password using basic sha1 hash.
var hashPassword = function (pass, salt) {
var p = bcrypt.hashSync(pass);

return shasum.digest( 'hex' );
return p;
};

var cmsAuth = {
Expand All @@ -38,15 +42,16 @@ module.exports = function (config, app) {
}

//TODO: Hashing on client side
query.password = hashPassword( req.body.password );
query.password = hashPassword( req.body.password, query.username );

console.warn( 'Login Query: ' + JSON.stringify( query ) + ''.verbose );

User.findOne( query, function (err, data) {
User.findOne( {username: query.username}, function (err, data) {
if (err) {
res.jsonp( 400, err );
}
if (data) {
console.warn('found user', util.inspect(data, {colors: true}));
res.jsonp( 200, data );
} else {
res.jsonp( 404, {message: 'Wrong username/password!'} );
Expand All @@ -72,28 +77,33 @@ module.exports = function (config, app) {
}

//TODO: Hashing on client side
data.password = hashPassword( req.body.password );
data.password = hashPassword( req.body.password, data.username );

console.log( String( "Register user" ).debug, data );
data.created_at = new Date();
data.updated_at = new Date();
data.active = false;
data.groups = ['public'];

var user = new User( data );

//Try and find user
User.find( data, function (err, u) {
console.log(err, u);
if (u) {
res.json( 400, {message: 'Username already exists!'} );
}
} );

user.save( function (err, ok) {
if (err) {
User.find( {username: data.username}, function (err, u) {
console.log(err, util.inspect(u, {colors: true}));
if(err){
res.json( 400, {message: 'Problem registering!'} );
}
if (u.length > 0) {
res.json( 400, {message: 'Username already exists!'} );
} else {
res.json( 200, ok );
user.save( function (er, ok) {
if (er) {
res.json( 400, {message: 'Problem registering!'} );
} else {
res.json( 201, ok );
}
} );
}
} );

},
session: function (req, res, next) {
}
Expand Down
10 changes: 9 additions & 1 deletion routes/cms-sockets.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module.exports = function (config, app) {
const events = require('events'), util = require('util');

var cmsSockets = function (config, app) {
events.EventEmitter.call(this);

console.warn( 'cms-sockts' );

};

util.inherits(cmsSockets, events.EventEmitter);
module.exports = cmsSockets;
3 changes: 2 additions & 1 deletion routes/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// load the things we need
var mongoose = require( 'mongoose' );
var bcrypt = require( 'bcrypt-nodejs' );
var hash = bcrypt.hashSync("bacon");

// define the schema for our user model
var UserSchema = mongoose.Schema( {
Expand Down Expand Up @@ -84,4 +85,4 @@ UserSchema.method( 'findByEmail', function (email, fn) {
} );

// create the model for users and expose it to our app
module.exports = mongoose.model( 'User', UserSchema );
module.exports = mongoose.model( 'User', UserSchema );
4 changes: 2 additions & 2 deletions routes/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ var MESSAGES = {
};
var DS = require('jps-ds').DS;
var _ds = new DS({
host: 'angularcms:[email protected]:10089/app19632340',
//host: 'localhost:27017/angular-cms',
//host: 'angularcms:[email protected]:10089/app19632340',
host: 'localhost/angular-cms',
models: {
'groups': {
title: String,
Expand Down
9 changes: 4 additions & 5 deletions test/routes/rest-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ cmsRoutes.mount(config, app);
app.listen(9292);

var endpoint = 'http://localhost:8181/api/v2';
var username = "nodetest" + Date.now();
var postData = {
"username": "nodetest" + Date.now(),
"email": "nodetest@email.com",
"username": username,
"email": username + "@email.com",
"password": "test",
"active": true,
"groups": ["member"],
"created": new Date(),
"modified": new Date(),
"metadata": {
"avatar": "",
"name": "Node Test User"
Expand All @@ -44,7 +43,7 @@ describe('Testing: API Server', function () {

it('POST - /api/v2/users/login - should return user on successful login', function (done) {
var validUser = {
username: 'nodetest',
username: '[email protected]',
password: 'test'
};
request(app)
Expand Down

0 comments on commit 035124e

Please sign in to comment.