Skip to content

Commit 1f98962

Browse files
committed
- fixed the wrong uppercase services filenames to be lowercase
- added coffeescripts for all the javascript equivalents - moved navbar also to controllers folder, just for consistency - TODO: extensive testing
1 parent 11e1ffd commit 1f98962

File tree

15 files changed

+218
-18
lines changed

15 files changed

+218
-18
lines changed

Diff for: app/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ Generator.prototype.appJs = function appJs() {
378378
appendOptions.sourceFileList.push('scripts/controllers/login.js');
379379
appendOptions.sourceFileList.push('scripts/controllers/signup.js');
380380
appendOptions.sourceFileList.push('scripts/controllers/settings.js');
381-
appendOptions.sourceFileList.push('scripts/services/Auth.js');
382-
appendOptions.sourceFileList.push('scripts/services/Session.js');
383-
appendOptions.sourceFileList.push('scripts/services/User.js');
381+
appendOptions.sourceFileList.push('scripts/services/auth.js');
382+
appendOptions.sourceFileList.push('scripts/services/session.js');
383+
appendOptions.sourceFileList.push('scripts/services/user.js');
384384
appendOptions.sourceFileList.push('scripts/directives/mongooseError.js');
385385
}
386386

@@ -497,9 +497,9 @@ Generator.prototype.mongoFiles = function () {
497497
copyScriptWithEnvOptions(this, 'controllers/signup', 'app/scripts/');
498498
copyScriptWithEnvOptions(this, 'controllers/settings', 'app/scripts/');
499499

500-
copyScriptWithEnvOptions(this, 'services/Auth', 'app/scripts/');
501-
copyScriptWithEnvOptions(this, 'services/Session', 'app/scripts/');
502-
copyScriptWithEnvOptions(this, 'services/User', 'app/scripts/');
500+
copyScriptWithEnvOptions(this, 'services/auth', 'app/scripts/');
501+
copyScriptWithEnvOptions(this, 'services/session', 'app/scripts/');
502+
copyScriptWithEnvOptions(this, 'services/user', 'app/scripts/');
503503

504504
copyScriptWithEnvOptions(this, 'directives/mongooseError', 'app/scripts/');
505505

Diff for: templates/coffeescript/auth.coffee

Whitespace-only changes.

Diff for: templates/coffeescript/controllers/login.coffee

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
angular.module('<%= scriptAppName %>')
4+
.controller 'LoginCtrl', ($scope, Auth, $location) ->
5+
$scope.user = {}
6+
$scope.errors = {}
7+
$scope.login = (form) ->
8+
$scope.submitted = true
9+
if form.$valid
10+
Auth.login(
11+
email: $scope.user.email
12+
password: $scope.user.password
13+
)
14+
.then ->
15+
# Logged in, redirect to home
16+
$location.path '/'
17+
.catch (err) ->
18+
err = err.data;
19+
$scope.errors.other = err.message;

Diff for: templates/coffeescript/controllers/settings.coffee

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
angular.module('<%= scriptAppName %>')
4+
.controller 'SettingsCtrl', ($scope, User, Auth) ->
5+
$scope.errors = {}
6+
$scope.changePassword = (form) ->
7+
$scope.submitted = true
8+
if form.$valid
9+
Auth.changePassword($scope.user.oldPassword, $scope.user.newPassword)
10+
.then(->
11+
$scope.message = 'Password successfully changed.'
12+
).catch( ->
13+
form.password.$setValidity 'mongoose', false
14+
$scope.errors.other = 'Incorrect password'
15+
)

Diff for: templates/coffeescript/controllers/signup.coffee

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
angular.module('<%= scriptAppName %>')
4+
.controller 'SignupCtrl', ($scope, Auth, $location) ->
5+
$scope.user = {}
6+
$scope.errors = {}
7+
$scope.register = (form) ->
8+
$scope.submitted = true
9+
if form.$valid
10+
11+
# Account created, redirect to home
12+
Auth.createUser(
13+
name: $scope.user.name
14+
email: $scope.user.email
15+
password: $scope.user.password
16+
).then( ->
17+
$location.path '/'
18+
).catch( (err) ->
19+
err = err.data
20+
$scope.errors = {}
21+
22+
# Update validity of form fields that match the mongoose errors
23+
angular.forEach err.errors, (error, field) ->
24+
form[field].$setValidity 'mongoose', false
25+
$scope.errors[field] = error.type
26+
)
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
###
4+
Removes server error when user updates input
5+
###
6+
angular.module('<%= scriptAppName %>')
7+
.directive 'mongooseError', ->
8+
restrict: 'A'
9+
require: 'ngModel'
10+
link: (scope, element, attrs, ngModel) ->
11+
element.on 'keydown', ->
12+
ngModel.$setValidity 'mongoose', true
13+

Diff for: templates/coffeescript/services/auth.coffee

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
'use strict'
2+
3+
angular.module('<%= scriptAppName %>')
4+
.factory 'Auth', Auth = ($location, $rootScope, Session, User, $cookieStore) ->
5+
6+
# Get currentUser from cookie
7+
$rootScope.currentUser = $cookieStore.get('user') or null
8+
$cookieStore.remove 'user'
9+
10+
###
11+
Authenticate user
12+
13+
@param {Object} user - login info
14+
@param {Function} callback - optional
15+
@return {Promise}
16+
###
17+
login: (user, callback) ->
18+
cb = callback or angular.noop
19+
Session.save(
20+
email: user.email
21+
password: user.password
22+
, (user) ->
23+
$rootScope.currentUser = user
24+
cb()
25+
, (err) ->
26+
cb err
27+
).$promise
28+
29+
30+
###
31+
Unauthenticate user
32+
33+
@param {Function} callback - optional
34+
@return {Promise}
35+
###
36+
logout: (callback) ->
37+
cb = callback or angular.noop
38+
Session.delete(->
39+
$rootScope.currentUser = null
40+
cb()
41+
, (err) ->
42+
cb err
43+
).$promise
44+
45+
46+
###
47+
Create a new user
48+
49+
@param {Object} user - user info
50+
@param {Function} callback - optional
51+
@return {Promise}
52+
###
53+
createUser: (user, callback) ->
54+
cb = callback or angular.noop
55+
User.save(user, (user) ->
56+
$rootScope.currentUser = user
57+
cb user
58+
, (err) ->
59+
cb err
60+
).$promise
61+
62+
63+
###
64+
Change password
65+
66+
@param {String} oldPassword
67+
@param {String} newPassword
68+
@param {Function} callback - optional
69+
@return {Promise}
70+
###
71+
changePassword: (oldPassword, newPassword, callback) ->
72+
cb = callback or angular.noop
73+
User.update(
74+
oldPassword: oldPassword
75+
newPassword: newPassword
76+
, (user) ->
77+
cb user
78+
, (err) ->
79+
cb err
80+
).$promise
81+
82+
83+
###
84+
Gets all available info on authenticated user
85+
86+
@return {Object} user
87+
###
88+
currentUser: ->
89+
User.get()
90+
91+
92+
###
93+
Simple check to see if a user is logged in
94+
95+
@return {Boolean}
96+
###
97+
isLoggedIn: ->
98+
user = $rootScope.currentUser
99+
!!user

Diff for: templates/coffeescript/services/session.coffee

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
angular.module('<%= scriptAppName %>')
4+
.factory 'Session', ($resource) ->
5+
$resource '/api/session/'

Diff for: templates/coffeescript/services/user.coffee

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict"
2+
angular.module("<%= scriptAppName %>")
3+
.factory "User", ($resource) ->
4+
$resource "/api/users/:id",
5+
id: "@id"
6+
,
7+
update:
8+
method: "PUT"
9+
params: {}
10+
11+
get:
12+
method: "GET"
13+
params:
14+
id: "me"
15+

Diff for: templates/javascript/controllers/settings.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ angular.module('<%= scriptAppName %>')
44
.controller('SettingsCtrl', function ($scope, User, Auth) {
55
$scope.errors = {};
66

7-
$scope.changePassword = function(form) {
7+
$scope.changePassword = function(form) {
88
$scope.submitted = true;
99

1010
if(form.$valid) {

Diff for: templates/javascript/services/User.js

-11
This file was deleted.
File renamed without changes.
File renamed without changes.

Diff for: templates/javascript/services/user.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
angular.module('<%= scriptAppName %>')
4+
.factory('User', function ($resource) {
5+
return $resource('/api/users/:id', {
6+
id: '@id'
7+
}, { //parameters default
8+
update: {
9+
method: 'PUT',
10+
params: {}
11+
},
12+
get: {
13+
method: 'GET',
14+
params: {
15+
id:'me'
16+
}
17+
}
18+
});
19+
});

0 commit comments

Comments
 (0)