From 679a85a1b5f8d580b6ca3e9a891bf8062fe7a8bb Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Mon, 31 Dec 2018 17:01:44 -0500 Subject: [PATCH] Fix login shaking and update AuthService --- app/public/services/AuthSvc.js | 51 ++++++++++++++--------------- app/public/shimapan/components/LoginComp.js | 29 +++++++++++----- public/views/shimapan/login-form.html | 2 +- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/app/public/services/AuthSvc.js b/app/public/services/AuthSvc.js index a3094f8..1220d06 100644 --- a/app/public/services/AuthSvc.js +++ b/app/public/services/AuthSvc.js @@ -1,31 +1,31 @@ var angular = require('angular'); -angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', function($http, $window) { - this.login = (displayname, password) => { - return $http({ +angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', function($http) { + this.login = async (displayname, password) => + $http({ method: 'POST', url: '/api/auth/login', data: { displayname: displayname, password: password } - }).then(res => { - if (res.status === 200) - $window.location.href = '/home'; - }) - }; - - this.logout = () => { - $http({ - method: 'GET', - url: '/api/auth/logout' - }).then(() => { - $window.location.href = '/'; + }).catch(err => { + if (err.status === 401) + throw 'unauthorized'; + else if (err.status === 429) + throw 'ratelimited'; + else + throw 'unknown'; }); - }; - this.register = (displayname, password, invite) => { - return $http({ + this.logout = async () => + $http({ + method: 'POST', + url: '/api/auth/logout' + }); + + this.register = async (displayname, password, invite) => + $http({ method: 'POST', url: '/api/auth/register', data: { @@ -33,18 +33,15 @@ angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', functi password: password, invite: invite } - }).then(function(res) { - if (res.status === 200) - $window.location.href = '/home'; + }).catch(err => { + throw err; }); - }; - this.whoami = function(cb) { - return $http({ + this.whoami = async () => + $http({ method: 'GET', url: '/api/auth/whoami' - }).then(function(res) { - cb(res.data); + }).catch(err => { + throw err; }); - } }]); diff --git a/app/public/shimapan/components/LoginComp.js b/app/public/shimapan/components/LoginComp.js index 16e3c48..2d3e1df 100644 --- a/app/public/shimapan/components/LoginComp.js +++ b/app/public/shimapan/components/LoginComp.js @@ -1,15 +1,26 @@ -var angular = require('angular'); +const angular = require('angular'); angular.module('LoginComp', ['AuthSvc']).component('loginComponent', { templateUrl: '/views/shimapan/login-form.html', - controller: ['$scope', '$timeout', 'AuthService', function($scope, $timeout, AuthService) { - $scope.login = function() { - AuthService.login($scope.username, $scope.password).catch(function() { - $scope.error = true; - $timeout(function() { - $scope.error = false; - }, 820); - }); + controller: ['$scope', '$window', '$timeout', 'AuthService', function($scope, $window, $timeout, AuthService) { + $scope.flash = classname => { + $scope.$apply(() => $scope[classname] = true); + $timeout(() => $scope[classname] = false, 820); + }; + + $scope.error = false; + $scope.login = async () => { + try { + await AuthService.login($scope.username, $scope.password); + $window.location.href = '/home'; + } catch(err) { + if (err === 'limited') + $scope.flash('warn'); + else if (err === 'unauthorized') + $scope.flash('error'); + else + $scope.flash('error'); + } }; }] }); \ No newline at end of file diff --git a/public/views/shimapan/login-form.html b/public/views/shimapan/login-form.html index 88b65dc..83d8073 100644 --- a/public/views/shimapan/login-form.html +++ b/public/views/shimapan/login-form.html @@ -4,7 +4,7 @@
- +
\ No newline at end of file