From a24c84f9d0b87c0e8464be722faf79700a806811 Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Tue, 14 Aug 2018 05:14:26 -0400 Subject: [PATCH] Update auth controller --- app/public/panel/controllers/NavCtrl.js | 4 +-- app/public/services/AuthSvc.js | 42 ++++++++++---------------- app/public/shimapan/components/LoginComp.js | 9 ++---- app/public/shimapan/components/RegisterComp.js | 10 ++---- app/public/shimapan/components/UploadComp.js | 4 +-- public/views/panel.html | 4 +-- 6 files changed, 28 insertions(+), 45 deletions(-) diff --git a/app/public/panel/controllers/NavCtrl.js b/app/public/panel/controllers/NavCtrl.js index 46aad7d..050191a 100644 --- a/app/public/panel/controllers/NavCtrl.js +++ b/app/public/panel/controllers/NavCtrl.js @@ -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; diff --git a/app/public/services/AuthSvc.js b/app/public/services/AuthSvc.js index 4e17d02..a3094f8 100644 --- a/app/public/services/AuthSvc.js +++ b/app/public/services/AuthSvc.js @@ -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); }); diff --git a/app/public/shimapan/components/LoginComp.js b/app/public/shimapan/components/LoginComp.js index 68baa47..16e3c48 100644 --- a/app/public/shimapan/components/LoginComp.js +++ b/app/public/shimapan/components/LoginComp.js @@ -2,12 +2,9 @@ var 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({ - username: $scope.username, - password: $scope.password - }).catch(function() { + 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; diff --git a/app/public/shimapan/components/RegisterComp.js b/app/public/shimapan/components/RegisterComp.js index fb1c321..fa94913 100644 --- a/app/public/shimapan/components/RegisterComp.js +++ b/app/public/shimapan/components/RegisterComp.js @@ -2,13 +2,9 @@ var angular = require('angular'); 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() { + controller: ['$scope', '$timeout', 'AuthService', function($scope, $timeout, AuthService) { + $scope.register = function() { + AuthService.register($scope.username, $scope.password, $scope.invite).catch(function() { $scope.error = true; $timeout(function() { $scope.error = false diff --git a/app/public/shimapan/components/UploadComp.js b/app/public/shimapan/components/UploadComp.js index 3a58d8a..98a3c43 100644 --- a/app/public/shimapan/components/UploadComp.js +++ b/app/public/shimapan/components/UploadComp.js @@ -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; } diff --git a/public/views/panel.html b/public/views/panel.html index b15c315..63647fc 100644 --- a/public/views/panel.html +++ b/public/views/panel.html @@ -14,9 +14,9 @@