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) {
$scope.user = {};
AuthService.currentUser(function(user) {
$scope.user = username;
AuthService.whoami(data => {
$scope.user = data;
});
$scope.logout = AuthService.logout;

View File

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

View File

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

View File

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

View File

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

View File

@ -14,9 +14,9 @@
<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="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 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" ng-click="logout()">Logout</a></li>
</ul>