1
0
mirror of https://github.com/Foltik/Shimapan synced 2025-01-07 08:42:49 -05:00

Fix login shaking and update AuthService

This commit is contained in:
Jack Foltz 2018-12-31 17:01:44 -05:00
parent f8ddb3924f
commit 679a85a1b5
Signed by: foltik
GPG Key ID: D1F0331758D1F29A
3 changed files with 45 additions and 37 deletions

View File

@ -1,31 +1,31 @@
var angular = require('angular'); var angular = require('angular');
angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', function($http, $window) { angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', function($http) {
this.login = (displayname, password) => { this.login = async (displayname, password) =>
return $http({ $http({
method: 'POST', method: 'POST',
url: '/api/auth/login', url: '/api/auth/login',
data: { data: {
displayname: displayname, displayname: displayname,
password: password password: password
} }
}).then(res => { }).catch(err => {
if (res.status === 200) if (err.status === 401)
$window.location.href = '/home'; throw 'unauthorized';
}) else if (err.status === 429)
}; throw 'ratelimited';
else
this.logout = () => { throw 'unknown';
$http({
method: 'GET',
url: '/api/auth/logout'
}).then(() => {
$window.location.href = '/';
}); });
};
this.register = (displayname, password, invite) => { this.logout = async () =>
return $http({ $http({
method: 'POST',
url: '/api/auth/logout'
});
this.register = async (displayname, password, invite) =>
$http({
method: 'POST', method: 'POST',
url: '/api/auth/register', url: '/api/auth/register',
data: { data: {
@ -33,18 +33,15 @@ angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', functi
password: password, password: password,
invite: invite invite: invite
} }
}).then(function(res) { }).catch(err => {
if (res.status === 200) throw err;
$window.location.href = '/home';
}); });
};
this.whoami = function(cb) { this.whoami = async () =>
return $http({ $http({
method: 'GET', method: 'GET',
url: '/api/auth/whoami' url: '/api/auth/whoami'
}).then(function(res) { }).catch(err => {
cb(res.data); throw err;
}); });
}
}]); }]);

View File

@ -1,15 +1,26 @@
var angular = require('angular'); const angular = require('angular');
angular.module('LoginComp', ['AuthSvc']).component('loginComponent', { angular.module('LoginComp', ['AuthSvc']).component('loginComponent', {
templateUrl: '/views/shimapan/login-form.html', templateUrl: '/views/shimapan/login-form.html',
controller: ['$scope', '$timeout', 'AuthService', function($scope, $timeout, AuthService) { controller: ['$scope', '$window', '$timeout', 'AuthService', function($scope, $window, $timeout, AuthService) {
$scope.login = function() { $scope.flash = classname => {
AuthService.login($scope.username, $scope.password).catch(function() { $scope.$apply(() => $scope[classname] = true);
$scope.error = true; $timeout(() => $scope[classname] = false, 820);
$timeout(function() { };
$scope.error = 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');
}
}; };
}] }]
}); });

View File

@ -4,7 +4,7 @@
<form ng-submit="login()"> <form ng-submit="login()">
<input id="username" placeholder="Username" class="form-control" type="text" ng-model="username"/> <input id="username" placeholder="Username" class="form-control" type="text" ng-model="username"/>
<input id="password" placeholder="Password" class="form-control" type="password" ng-model="password"/> <input id="password" placeholder="Password" class="form-control" type="password" ng-model="password"/>
<button type="submit" class="btn" ng-class="{shake: error}">Submit</button> <button type="submit" class="btn" ng-class="{error: error, warn: warn}">Submit</button>
</form> </form>
</fieldset> </fieldset>
</div> </div>