Skip to content

Commit

Permalink
Merge pull request #27 from jonniespratley/feature/intern
Browse files Browse the repository at this point in the history
Feature/intern
  • Loading branch information
jonniespratley committed Dec 15, 2014
2 parents 3ac2fa5 + 027e961 commit 6091c71
Show file tree
Hide file tree
Showing 38 changed files with 835 additions and 266 deletions.
16 changes: 15 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function (grunt) {

//Connect proxy to route requests to localhost:8181/api
grunt.loadNpmTasks('grunt-connect-proxy');
grunt.loadNpmTasks('intern');
require('json-proxy').initialize({});
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
Expand Down Expand Up @@ -90,7 +91,10 @@ module.exports = function (grunt) {
// Change this to '0.0.0.0' to access the server from outside.
hostname: '127.0.0.1',
livereload: 35729,
base: ['.tmp', '<%= yeoman.app %>']
base: ['.tmp', '<%= yeoman.app %>'],
onCreateServer: function(server, connect, options) {
console.warn('onCreateServer', options);
}
},
livereload: {
options: {
Expand Down Expand Up @@ -607,6 +611,16 @@ module.exports = function (grunt) {
'test/routes/*-spec.js'
]
}
},
intern: {
test: {
options: {
runType: 'client',
config: 'test/intern.conf',
reporters: [ 'console' ],
suites: []
}
}
}
};

Expand Down
10 changes: 1 addition & 9 deletions app/scripts/controllers/login.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,7 @@ angular.module('angularCmsApp').controller 'LoginCtrl', ($scope, $rootScope, $co
###

$scope.logout = (user) ->

#Clear cookie
$cookieStore.put('App.session', null)

#Clear session
$rootScope.App.session = null

#Change location
$rootScope.App.location.path($rootScope.App.logout.redirect)
cmsAuthService.logout(user)


#Controller name
Expand Down
10 changes: 5 additions & 5 deletions app/scripts/controllers/media.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ angular.module('angularCmsApp').controller 'MediaCtrl', ($scope, $http, DataServ

$scope.uploads = []
$scope.getUploads = () ->
$http.get('/api/upload').success((data)->
$scope.uploads = data
DataService.fetch('uploads').then((res)->
$scope.uploads = res.data
)

$ ->
$("#fileupload").fileupload(
dataType: 'json'
dropZone: $('.uploader-dropzone')
dropZone: $('.dropzone')
add: (e, data) ->
console.log(e, data)

data.url = '/api/upload'
data.context = $('<p/>').text('Uploading...').appendTo(document.body)
data.submit()
Expand All @@ -25,7 +25,7 @@ angular.module('angularCmsApp').controller 'MediaCtrl', ($scope, $http, DataServ
$.each data.result.files, (index, file) ->
$("<p/>").text(file.name).appendTo document.body
)

$scope.uploader = {
files: []
};
Expand Down
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)

3 changes: 1 addition & 2 deletions app/scripts/controllers/users.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ angular.module('angularCmsApp').controller('UsersCtrl', ($scope, DataService) ->
delete user.password if user.password
user.updated_at = new Date()
DataService.save('users', user).then((data) ->
$('#user-modal').modal('hide')
$scope.getUsers()
$scope.users.push(user) unless user._id
$scope.user = {}
console.log(data)
$('#user-modal').modal('hide')
)

)
4 changes: 2 additions & 2 deletions app/scripts/directives/cmsuploader.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@name angularCmsApp.directive:cmsUploader
@element div
@function
@description
@description
This is an example uploader.
###
'use strict'
Expand Down Expand Up @@ -56,7 +56,7 @@ angular.module('angularCmsApp').directive 'cmsUploader', ->
xhr.onload = () ->
console.log('Upload complete')

xhr.open('POST', '/api/v2/upload', true)
xhr.open('POST', $attrs.action, true)
xhr.send(form)

console.log('Upload ', file)
Expand Down
12 changes: 8 additions & 4 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 @@ -45,7 +47,7 @@ angular.module('angularCmsApp').service 'cmsAuthService', ($q, $http, $log, $roo
###
logout: (user) ->
#Clear cookie
cmsSessionService.logout(null)
cmsSessionService.setUserAuthenticated(null)
$rootScope.apply(()->
$location.reload()
)
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
17 changes: 9 additions & 8 deletions app/views/media.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<h1><i class="fa fa-1x fa-upload"></i> Media</h1>
<hr>
<div class="clearfix">
<cms-uploader
image="http://placehold.it/100x100"
name="image"
maxheight="400"
maxwidth="400"
multiple="true"
dragdrop="true"
btnupload="Upload"
<cms-uploader
image="http://placehold.it/100x100"
name="image"
maxheight="400"
maxwidth="400"
multiple="true"
dragdrop="true"
btnupload="Upload"
btncrop="Crop" cropper="true" collection="imageCollection" showthumbs="true" litmpl="#liTmpl" showtable="true"
showlist="true" filelimit="10" sizelimit="5000" action="/api/v2/upload"></cms-uploader>

Expand All @@ -18,6 +18,7 @@ <h1><i class="fa fa-1x fa-upload"></i> Media</h1>
<li ng-repeat="upload in uploads">
<cms-upload ng-model="upload"></cms-upload>
</li>

</ul>

<pre>$scope.uploads: {{uploads | json }}</pre>
Expand Down
8 changes: 4 additions & 4 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"hostname": "localhost",
"port": 5001
},
"staticDir": "/dist",
"publicDir": "/app",
"uploadsTmpDir": "/.tmp",
"uploadsDestDir": "/www/cms-content/uploads",
"staticDir": "www",
"publicDir": "www",
"uploadsTmpDir": ".tmp",
"uploadsDestDir": "www/cms-content/uploads",
"uploadsUrl": ":8181/cms-content/",
"logFormat": "[:date] - [:method] - :url - :status - :response-time ms"
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"logfmt": "~0.18.1",
"markdown": "~0.5.0",
"method-override": "~1.0.0",
"mongodb": "*",
"mongodb": "^1.4.23",
"mongoose": "^3.8.20",
"passport": "^0.2.1",
"passport-facebook": "^1.0.3",
Expand All @@ -59,7 +59,8 @@
"promised-io": "~0.3.4",
"q": "^1.1.2",
"request": "~2.34.0",
"socket.io": "~0.9.16"
"socket.io": "~0.9.16",
"websocket": "^1.0.14"
},
"devDependencies": {
"chai": "^1.10.0",
Expand Down Expand Up @@ -100,6 +101,7 @@
"gulp-ngmin": "^0.3.0",
"gulp-protractor": "0.0.11",
"gulp-uglify": "^1.0.2",
"intern": "^2.2.2",
"jasmine-core": "^2.1.2",
"jasmine-node": "~1.11.0",
"jasmine-reporters": "^1.0.1",
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 6091c71

Please sign in to comment.