1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-12-29 05:11:55 -05:00

Update auth controller

This commit is contained in:
Jack Foltz 2018-08-14 05:14:26 -04:00
parent 189a438028
commit a24c84f9d0
Signed by: foltik
GPG Key ID: 303F88F996E95541
6 changed files with 28 additions and 45 deletions

View File

@ -2,8 +2,8 @@ var angular = require('angular');
angular.module('NavCtrl', ['AuthSvc']).controller('NavController', ['$scope', '$window', 'AuthService', function($scope, $window, AuthService) { angular.module('NavCtrl', ['AuthSvc']).controller('NavController', ['$scope', '$window', 'AuthService', function($scope, $window, AuthService) {
$scope.user = {}; $scope.user = {};
AuthService.currentUser(function(user) { AuthService.whoami(data => {
$scope.user = username; $scope.user = data;
}); });
$scope.logout = AuthService.logout; $scope.logout = AuthService.logout;

View File

@ -1,58 +1,48 @@
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, $window) {
this.login = function(user) { this.login = (displayname, password) => {
return $http({ return $http({
method: 'POST', method: 'POST',
url: '/api/auth/login', url: '/api/auth/login',
headers: {'Content-Type': 'application/x-www-form-urlencoded'}, data: {
transformRequest: function(obj) { displayname: displayname,
var str = []; password: password
for (var p in obj) }
if (obj.hasOwnProperty(p)) }).then(res => {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: user
}).then(function(res) {
if (res.status === 200) if (res.status === 200)
$window.location.href = '/home'; $window.location.href = '/home';
}) })
}; };
this.logout = function() { this.logout = () => {
$http({ $http({
method: 'GET', method: 'GET',
url: '/api/auth/logout' url: '/api/auth/logout'
}).then(function() { }).then(() => {
$window.location.href = '/'; $window.location.href = '/';
}); });
}; };
this.register = function(user) { this.register = (displayname, password, invite) => {
return $http({ return $http({
method: 'POST', method: 'POST',
url: '/api/auth/register', url: '/api/auth/register',
headers: {'Content-Type': 'application/x-www-form-urlencoded'}, data: {
transformRequest: function(obj) { displayname: displayname,
var str = []; password: password,
for (var p in obj) invite: invite
if (obj.hasOwnProperty(p)) }
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: user
}).then(function(res) { }).then(function(res) {
if (res.status === 200) if (res.status === 200)
$window.location.href = '/home'; $window.location.href = '/home';
}); });
}; };
this.currentUser = function(cb) { this.whoami = function(cb) {
return $http({ return $http({
method: 'GET', method: 'GET',
url: '/api/auth/session', url: '/api/auth/whoami'
headers: {'Content-Type': 'application/json'}
}).then(function(res) { }).then(function(res) {
cb(res.data); cb(res.data);
}); });

View File

@ -2,12 +2,9 @@ var 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', '$timeout', 'AuthService', function($scope, $timeout, AuthService) {
$scope.login = function () { $scope.login = function() {
AuthService.login({ AuthService.login($scope.username, $scope.password).catch(function() {
username: $scope.username,
password: $scope.password
}).catch(function() {
$scope.error = true; $scope.error = true;
$timeout(function() { $timeout(function() {
$scope.error = false; $scope.error = false;

View File

@ -2,13 +2,9 @@ var angular = require('angular');
angular.module('RegisterComp', ['AuthSvc']).component('registerComponent', { angular.module('RegisterComp', ['AuthSvc']).component('registerComponent', {
templateUrl: '/views/shimapan/register-form.html', templateUrl: '/views/shimapan/register-form.html',
controller: ['$scope', '$timeout', 'AuthService', function ($scope, $timeout, AuthService) { controller: ['$scope', '$timeout', 'AuthService', function($scope, $timeout, AuthService) {
$scope.register = function () { $scope.register = function() {
AuthService.register({ AuthService.register($scope.username, $scope.password, $scope.invite).catch(function() {
username: $scope.username,
password: $scope.password,
invite: $scope.invite
}).catch(function() {
$scope.error = true; $scope.error = true;
$timeout(function() { $timeout(function() {
$scope.error = false $scope.error = false

View File

@ -30,9 +30,9 @@ angular.module('UploadComp', ['ngFileUpload', 'AuthSvc']).component('uploadCompo
function (response) { function (response) {
if (response.status !== 200) { if (response.status !== 200) {
if (response.status === 401) { if (response.status === 401) {
file.$error = "Invalid authorization token"; file.$error = "Unauthorized.";
} else if (response.status === 403) { } else if (response.status === 403) {
file.$error = "Forbidden"; file.$error = "Forbidden.";
} else { } else {
file.$error = "Unknown error " + response.status; file.$error = "Unknown error " + response.status;
} }

View File

@ -14,9 +14,9 @@
<ul class="nav"> <ul class="nav">
<li><a draggable="false" ui-sref="dashboard" ng-class="{active: $state.$current.name=='dashboard'}">Dashboard</a></li> <li><a draggable="false" ui-sref="dashboard" ng-class="{active: $state.$current.name=='dashboard'}">Dashboard</a></li>
<li><a draggable="false" ui-sref="search" ng-class="{active: $state.$current.name=='search'}">Search</a></li> <li><a draggable="false" ui-sref="search" ng-class="{active: $state.$current.name=='search'}">Search</a></li>
<li><a draggable="false" ui-sref="api" ng-class="{active: $state.$current.name=='api'}">API</a></li> <li><a draggable="false" ui-sref="keys" ng-class="{active: $state.$current.name=='keys'}">Keys</a></li>
<li><a draggable="false" ui-sref="invites" ng-class="{active: $state.$current.name=='invites'}">Invites</a></li> <li><a draggable="false" ui-sref="invites" ng-class="{active: $state.$current.name=='invites'}">Invites</a></li>
<li ng-hide="!hasPermission('users.view')"><a draggable="false" ui-sref="users" ng-class="{active: $state.$current.name=='users'}">Users</a></li> <li ng-hide="!hasPermission('user.get')"><a draggable="false" ui-sref="users" ng-class="{active: $state.$current.name=='users'}">Users</a></li>
<li><a draggable="false" ui-sref="stats" ng-class="{active: $state.$current.name=='stats'}">Statistics</a></li> <li><a draggable="false" ui-sref="stats" ng-class="{active: $state.$current.name=='stats'}">Statistics</a></li>
<li><a draggable="false" ng-click="logout()">Logout</a></li> <li><a draggable="false" ng-click="logout()">Logout</a></li>
</ul> </ul>