From 0c85266ab24d6de21700164a25e72f6a496d7d3b Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 8 Jul 2017 00:51:59 +0200 Subject: [PATCH] Separate login module into different files. --- src/login/index.js | 108 +++------------------------------- src/login/login.config.js | 31 ++++++++++ src/login/login.controller.js | 34 +++++++++++ src/login/login.service.js | 36 ++++++++++++ 4 files changed, 108 insertions(+), 101 deletions(-) create mode 100644 src/login/login.config.js create mode 100644 src/login/login.controller.js create mode 100644 src/login/login.service.js diff --git a/src/login/index.js b/src/login/index.js index 450c26d..be174fd 100644 --- a/src/login/index.js +++ b/src/login/index.js @@ -29,6 +29,10 @@ ngHttpAuth = 'http-auth-interceptor'; var loginTmpl = require('./login.tmpl.html'); +var LoginService = require('./login.service'); +var LoginConfig = require('./login.config'); +var LoginController = require('./login.controller'); + var base64 = require('base64util'); module.exports = angular.module('accountant.login', [ @@ -37,109 +41,11 @@ module.exports = angular.module('accountant.login', [ ngUiBootstrap ]) - .service('LoginService', function($uibModal, $storage, $document, $log, authService) { - var login = function () { - $storage.session.clear(); + .service('LoginService', LoginService) - var modalInstance = $uibModal.open({ - ariaLabelledBy: 'modal-title', - ariaDescribedBy: 'modal-body', - templateUrl: loginTmpl, - controller: 'LoginModalController', - controllerAs: '$ctrl' - }); + .config(LoginConfig) - modalInstance.result.then(function (data) { - $log.log(data); - - $storage.session.set('refresh_token', data.refresh_token); - $storage.session.set('access_token', data.access_token); - - authService.loginConfirmed(); - }, function () { - $log.info('Modal dismissed at: ' + new Date()); - - // FIXME Alexis Lahouze 2017-06-11 Redirect to error page. - authService.loginCancelled(null, 'Login cancelled by user action.'); - }); - }; - - var cancelLogin = function () { - // FIXME Alexis Lahouze 2017-06-11 Redirect to error page. - }; - - return { - 'login': login, - 'cancelLogin': cancelLogin - }; - }) - - .config(function($httpProvider, $storageProvider) { - // Define interceptors. - $httpProvider.interceptors.push(function($storage) { - return { - request: function(config) { - var access_token = $storage.session.get('access_token'); - - if (access_token) { - //var tokenType = $storage.get('token_type'); - var tokenType = 'Bearer'; - var authorization = tokenType + ' ' + access_token; - config.headers.authorization = authorization; - } - - return config; - }, - }; - }); - - // Configure storage - // Set global prefix for stored keys - $storageProvider.setPrefix('accountant'); - - // Change the default storage engine - // Defaults to 'local' - $storageProvider.setDefaultStorageEngine('session'); - - // Change the enabled storage engines - // Defaults to ['memory', 'cookie', 'session', 'local'] - $storageProvider.setEnabledStorageEngines(['local', 'session']); - }) - - .controller('LoginModalController', function($scope, $uibModalInstance, $http, $log) { - var vm = this; - - vm.data = { - email: null, - password: null - }; - - vm.ok = function() { - var email = vm.data.email; - var password = vm.data.password; - - // Encode authentication data. - var authdata = base64.encode(email + ':' + password); - - return $http.post('/api/user/login', {}, { - ignoreAuthModule: true, - headers: { - 'authorization': 'Basic ' + authdata - } - }).then(function(result) { - $log.log(result); - - $uibModalInstance.close(result.data); - }, function(response) { - // FIXME Alexis Lahouze 2017-06-11 Handle error. - $log.log("Error on login", response); - }); - }; - - vm.cancel = function() { - $uibModalInstance.dismiss('cancel'); - }; - }) + .controller('LoginModalController', LoginController) .run(function($rootScope, LoginService) { var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', LoginService.login); diff --git a/src/login/login.config.js b/src/login/login.config.js new file mode 100644 index 0000000..d7cf68c --- /dev/null +++ b/src/login/login.config.js @@ -0,0 +1,31 @@ +module.exports = function($httpProvider, $storageProvider) { + // Define interceptors. + $httpProvider.interceptors.push(function($storage) { + return { + request: function(config) { + var access_token = $storage.session.get('access_token'); + + if (access_token) { + //var tokenType = $storage.get('token_type'); + var tokenType = 'Bearer'; + var authorization = tokenType + ' ' + access_token; + config.headers.authorization = authorization; + } + + return config; + }, + }; + }); + + // Configure storage + // Set global prefix for stored keys + $storageProvider.setPrefix('accountant'); + + // Change the default storage engine + // Defaults to 'local' + $storageProvider.setDefaultStorageEngine('session'); + + // Change the enabled storage engines + // Defaults to ['memory', 'cookie', 'session', 'local'] + $storageProvider.setEnabledStorageEngines(['local', 'session']); +}; diff --git a/src/login/login.controller.js b/src/login/login.controller.js new file mode 100644 index 0000000..ad8651d --- /dev/null +++ b/src/login/login.controller.js @@ -0,0 +1,34 @@ +module.exports = function($scope, $uibModalInstance, $http, $log) { + var vm = this; + + vm.data = { + email: null, + password: null + }; + + vm.ok = function() { + var email = vm.data.email; + var password = vm.data.password; + + // Encode authentication data. + var authdata = base64.encode(email + ':' + password); + + return $http.post('/api/user/login', {}, { + ignoreAuthModule: true, + headers: { + 'authorization': 'Basic ' + authdata + } + }).then(function(result) { + $log.log(result); + + $uibModalInstance.close(result.data); + }, function(response) { + // FIXME Alexis Lahouze 2017-06-11 Handle error. + $log.log("Error on login", response); + }); + }; + + vm.cancel = function() { + $uibModalInstance.dismiss('cancel'); + }; +}; diff --git a/src/login/login.service.js b/src/login/login.service.js new file mode 100644 index 0000000..fcc714a --- /dev/null +++ b/src/login/login.service.js @@ -0,0 +1,36 @@ +module.exports = function($uibModal, $storage, $document, $log, authService) { + var login = function () { + $storage.session.clear(); + + var modalInstance = $uibModal.open({ + ariaLabelledBy: 'modal-title', + ariaDescribedBy: 'modal-body', + templateUrl: loginTmpl, + controller: 'LoginModalController', + controllerAs: '$ctrl' + }); + + modalInstance.result.then(function (data) { + $log.log(data); + + $storage.session.set('refresh_token', data.refresh_token); + $storage.session.set('access_token', data.access_token); + + authService.loginConfirmed(); + }, function () { + $log.info('Modal dismissed at: ' + new Date()); + + // FIXME Alexis Lahouze 2017-06-11 Redirect to error page. + authService.loginCancelled(null, 'Login cancelled by user action.'); + }); + }; + + var cancelLogin = function () { + // FIXME Alexis Lahouze 2017-06-11 Redirect to error page. + }; + + return { + 'login': login, + 'cancelLogin': cancelLogin + }; +};