From 9e5092a6d03722be7c6c00e5dd88ab61b747be5d Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 28 Jun 2016 12:54:29 -0400 Subject: [PATCH 1/4] refactor(client:login): rename error from 'other' to 'login' it makes more sense.. --- templates/app/client/app/account(auth)/login/login(html).html | 2 +- templates/app/client/app/account(auth)/login/login(jade).jade | 2 +- .../app/client/app/account(auth)/login/login.controller.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/app/client/app/account(auth)/login/login(html).html b/templates/app/client/app/account(auth)/login/login(html).html index 76382615f..2f72c7268 100644 --- a/templates/app/client/app/account(auth)/login/login(html).html +++ b/templates/app/client/app/account(auth)/login/login(html).html @@ -28,7 +28,7 @@

Login

Please enter a valid email.

-

{{ vm.errors.other }}

+

{{ vm.errors.login }}

diff --git a/templates/app/client/app/account(auth)/login/login(jade).jade b/templates/app/client/app/account(auth)/login/login(jade).jade index c46f0e5eb..a463419d4 100644 --- a/templates/app/client/app/account(auth)/login/login(jade).jade +++ b/templates/app/client/app/account(auth)/login/login(jade).jade @@ -27,7 +27,7 @@ .form-group.has-error p.help-block(ng-show='form.email.$error.required && form.password.$error.required && vm.submitted') | Please enter your email and password. - p.help-block {{ vm.errors.other }} + p.help-block {{ vm.errors.login }} div button.btn.btn-inverse.btn-lg.btn-login(type='submit') diff --git a/templates/app/client/app/account(auth)/login/login.controller.js b/templates/app/client/app/account(auth)/login/login.controller.js index 1aa48e1e3..03f4e532f 100644 --- a/templates/app/client/app/account(auth)/login/login.controller.js +++ b/templates/app/client/app/account(auth)/login/login.controller.js @@ -25,7 +25,7 @@ export default class LoginController { <% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %> }) .catch(err => { - this.errors.other = err.message; + this.errors.login = err.message; }); } } From f12cad79cf533350cc3b4f919d95f515cae12d9a Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 28 Jun 2016 12:57:43 -0400 Subject: [PATCH 2/4] feat(client): add a bunch of types, use class in auth.service --- .../account(auth)/login/login.controller.js | 38 +++++++++++--- .../settings/settings.controller.js | 26 +++++++++- .../account(auth)/signup/signup.controller.js | 49 ++++++++++++++----- .../app/client/app/main/main.component.js | 14 ++++-- .../components/auth(auth)/auth.service.js | 11 ++++- .../components/navbar/navbar.component.js | 11 +++-- 6 files changed, 120 insertions(+), 29 deletions(-) diff --git a/templates/app/client/app/account(auth)/login/login.controller.js b/templates/app/client/app/account(auth)/login/login.controller.js index 03f4e532f..a6dd073a4 100644 --- a/templates/app/client/app/account(auth)/login/login.controller.js +++ b/templates/app/client/app/account(auth)/login/login.controller.js @@ -1,14 +1,40 @@ 'use strict'; +// @flow +<%_ if(filters.flow) { -%> +type User = { + name: string; + email: string; + password: string; +}; +<%_ } -%> +<%_ if(filters.ts) { -%> +interface User { + name: string; + email: string; + password: string; +} +<%_ } -%> export default class LoginController { + user: User = { + name: '', + email: '', + password: '' + }; + errors = {login: undefined}; + submitted = false; + Auth; + <%_ if(filters.ngroute) { -%> + $location; + <%_ } if(filters.uirouter) { -%> + $state;<% } %> + /*@ngInject*/ constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) { - this.user = {}; - this.errors = {}; - this.submitted = false; - - this.Auth = Auth;<% if (filters.ngroute) { %> - this.$location = $location;<% } if (filters.uirouter) { %> + this.Auth = Auth; + <%_ if(filters.ngroute) { -%> + this.$location = $location; + <%_ } if(filters.uirouter) { -%> this.$state = $state;<% } %> } diff --git a/templates/app/client/app/account(auth)/settings/settings.controller.js b/templates/app/client/app/account(auth)/settings/settings.controller.js index 976bd5154..d6316b7a5 100644 --- a/templates/app/client/app/account(auth)/settings/settings.controller.js +++ b/templates/app/client/app/account(auth)/settings/settings.controller.js @@ -1,8 +1,30 @@ 'use strict'; +// @flow +<%_ if(filters.flow) { -%> +type User = { + oldPassword: string; + newPassword: string; + confirmPassword: string; +}; +<%_ } -%> +<%_ if(filters.ts) { -%> +interface User { + oldPassword: string; + newPassword: string; + confirmPassword: string; +} +<%_ } -%> export default class SettingsController { - errors = {}; + user: User = { + oldPassword: '', + newPassword: '', + confirmPassword: '' + }; + errors = {other: undefined}; + message = ''; submitted = false; + Auth; /*@ngInject*/ constructor(Auth) { @@ -12,7 +34,7 @@ export default class SettingsController { changePassword(form) { this.submitted = true; - if (form.$valid) { + if(form.$valid) { this.Auth.changePassword(this.user.oldPassword, this.user.newPassword) .then(() => { this.message = 'Password successfully changed.'; diff --git a/templates/app/client/app/account(auth)/signup/signup.controller.js b/templates/app/client/app/account(auth)/signup/signup.controller.js index 6ae9064e4..196684c24 100644 --- a/templates/app/client/app/account(auth)/signup/signup.controller.js +++ b/templates/app/client/app/account(auth)/signup/signup.controller.js @@ -1,44 +1,69 @@ 'use strict'; +// @flow +<%_ if(filters.flow) { -%> +type User = { + name: string; + email: string; + password: string; +}; +<%_ } -%> +<%_ if(filters.ts) { -%> +interface User { + name: string; + email: string; + password: string; +} +<%_ } -%> export default class SignupController { - //start-non-standard - user = {}; + user: User = { + name: '', + email: '', + password: '' + }; errors = {}; submitted = false; - //end-non-standard + Auth; + <%_ if(filters.ngroute) { -%> + $location; + <%_ } if(filters.uirouter) { -%> + $state;<% } %> /*@ngInject*/ constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) { - this.Auth = Auth;<% if (filters.ngroute) { %> - this.$location = $location;<% } if (filters.uirouter) { %> + this.Auth = Auth; + <%_ if(filters.ngroute) { -%> + this.$location = $location; + <%_ } if(filters.uirouter) { -%> this.$state = $state;<% } %> } register(form) { this.submitted = true; - if (form.$valid) { - this.Auth.createUser({ + if(form.$valid) { + return this.Auth.createUser({ name: this.user.name, email: this.user.email, password: this.user.password }) .then(() => { // Account created, redirect to home - <% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %> + <% if(filters.ngroute) { %>this.$location.path('/');<% } -%> + <% if(filters.uirouter) { %>this.$state.go('main');<% } -%> }) .catch(err => { err = err.data; this.errors = {}; -<% if (filters.mongooseModels) { %> + <%_ if(filters.mongooseModels) { -%> // Update validity of form fields that match the mongoose errors angular.forEach(err.errors, (error, field) => { form[field].$setValidity('mongoose', false); this.errors[field] = error.message; - });<% } -if (filters.sequelizeModels) { %> + });<% } %> + <%_ if(filters.sequelizeModels) { -%> // Update validity of form fields that match the sequelize errors - if (err.name) { + if(err.name) { angular.forEach(err.fields, field => { form[field].$setValidity('mongoose', false); this.errors[field] = err.message; diff --git a/templates/app/client/app/main/main.component.js b/templates/app/client/app/main/main.component.js index fab99e6a3..a1dc2479a 100644 --- a/templates/app/client/app/main/main.component.js +++ b/templates/app/client/app/main/main.component.js @@ -7,12 +7,18 @@ import uiRouter from 'angular-ui-router';<% } _%> import routing from './main.routes'; export class MainController { + $http; + <%_ if(filters.socketio) { -%> + socket;<% } %> + awesomeThings = []; + <%_ if(filters.models) { -%> + newThing = '';<% } %> + /*@ngInject*/ constructor($http<% if(filters.socketio) { %>, $scope, socket<% } %>) { - this.$http = $http;<% if (filters.socketio) { %> - this.socket = socket;<% } %> - this.awesomeThings = []; - <%_ if (filters.socketio) { _%> + this.$http = $http; + <%_ if(filters.socketio) { -%> + this.socket = socket; $scope.$on('$destroy', function() { socket.unsyncUpdates('thing'); diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 2e3bb33a9..4a074b94f 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -1,10 +1,17 @@ 'use strict'; // @flow +class User { + _id: string = ''; + name: string = ''; + email: string = ''; + role: string = ''; + $promise = undefined; +} export function AuthService($location, $http, $cookies, $q, appConfig, Util, User) { 'ngInject'; var safeCb = Util.safeCb; - var currentUser = {}; + var currentUser: User = new User(); var userRoles = appConfig.userRoles || []; /** * Check if userRole is >= role @@ -51,7 +58,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use */ logout() { $cookies.remove('token'); - currentUser = {}; + currentUser = new User(); }, /** diff --git a/templates/app/client/components/navbar/navbar.component.js b/templates/app/client/components/navbar/navbar.component.js index 1e9c3ad9d..6a6d37282 100644 --- a/templates/app/client/components/navbar/navbar.component.js +++ b/templates/app/client/components/navbar/navbar.component.js @@ -1,14 +1,19 @@ 'use strict'; export class NavbarComponent { - //start-non-standard menu = [{ 'title': 'Home', <% if (filters.uirouter) { %>'state': 'main'<% } else { %>'link': '/'<% } %> }]; - + <%_ if(!filters.uirouter) { -%> + $location; + <%_ } -%> + <%_ if (filters.auth) { -%> + isLoggedIn: Function; + isAdmin: Function; + getCurrentUser: Function; + <%_ } -%> isCollapsed = true; - //end-non-standard <%_ if(filters.ngroute || filters.auth) { _%> constructor(<% if(!filters.uirouter) { %>$location<% } if(!filters.uirouter && filters.auth) { %>, <% } if (filters.auth) { %>Auth<% } %>) { From aa2741da4efd88847cbe1aa349c8ca409e4a54d1 Mon Sep 17 00:00:00 2001 From: Koslun Date: Fri, 1 Jul 2016 12:46:47 +0200 Subject: [PATCH 3/4] fix(client:auth): fixes TS warnings, clarify function use --- templates/app/client/components/auth(auth)/auth.service.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 4a074b94f..8c407f396 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -100,7 +100,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use /** * Gets all available info on a user * - * @param {Function} [callback] - funciton(user) + * @param {Function} [callback] - optional, function(user) * @return {Promise} */ getCurrentUser(callback) { @@ -134,7 +134,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * @return {Bool|Promise} */ isLoggedIn(callback) { - return Auth.getCurrentUser() + return Auth.getCurrentUser(undefined) .then(user => { var is = user.hasOwnProperty('role'); safeCb(callback)(is); @@ -159,7 +159,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * @return {Bool|Promise} */ hasRole(role, callback) { - return Auth.getCurrentUser() + return Auth.getCurrentUser(undefined) .then(user => { var has = user.hasOwnProperty('role') ? hasRole(user.role, role) From 60d3bb2056ebfb29f1ef23c03c126354abe92433 Mon Sep 17 00:00:00 2001 From: Koslun Date: Fri, 1 Jul 2016 17:33:46 +0200 Subject: [PATCH 4/4] feat(client:auth): add types to async functions, update documentation --- .../components/auth(auth)/auth.service.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 8c407f396..87a0adb2f 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -32,10 +32,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * Authenticate user and save token * * @param {Object} user - login info - * @param {Function} callback - optional, function(error, user) + * @param {Function} callback - function(error, user) * @return {Promise} */ - login({email, password}, callback: Function) { + login({email, password}, callback?: Function) { return $http.post('/auth/local', { email, password }) .then(res => { $cookies.put('token', res.data.token); @@ -65,10 +65,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * Create a new user * * @param {Object} user - user info - * @param {Function} callback - optional, function(error, user) + * @param {Function} callback - function(error, user) * @return {Promise} */ - createUser(user, callback) { + createUser(user, callback?: Function) { return User.save(user, function(data) { $cookies.put('token', data.token); @@ -86,10 +86,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * * @param {String} oldPassword * @param {String} newPassword - * @param {Function} callback - optional, function(error, user) + * @param {Function} callback - function(error, user) * @return {Promise} */ - changePassword(oldPassword, newPassword, callback) { + changePassword(oldPassword, newPassword, callback?: Function) { return User.changePassword({ id: currentUser._id }, { oldPassword, newPassword }, function() { return safeCb(callback)(null); }, function(err) { @@ -100,10 +100,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use /** * Gets all available info on a user * - * @param {Function} [callback] - optional, function(user) + * @param {Function} [callback] - function(user) * @return {Promise} */ - getCurrentUser(callback) { + getCurrentUser(callback?: Function) { var value = currentUser.hasOwnProperty('$promise') ? currentUser.$promise : currentUser; @@ -131,9 +131,9 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * Check if a user is logged in * * @param {Function} [callback] - function(is) - * @return {Bool|Promise} + * @return {Promise} */ - isLoggedIn(callback) { + isLoggedIn(callback?: Function) { return Auth.getCurrentUser(undefined) .then(user => { var is = user.hasOwnProperty('role'); @@ -156,9 +156,9 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * * @param {String} role - the role to check against * @param {Function} [callback] - function(has) - * @return {Bool|Promise} + * @return {Promise} */ - hasRole(role, callback) { + hasRole(role, callback?: Function) { return Auth.getCurrentUser(undefined) .then(user => { var has = user.hasOwnProperty('role')