// vim: set tw=80 ts=4 sw=4 sts=4: /* This file is part of Accountant. Accountant is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Accountant is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Accountant. If not, see . */ /* jshint node: true */ 'use strict'; var angular = require('angular'); var ngStorage = require('meanie-angular-storage'), ngHttpAuth = require('angular-http-auth'), ngUiBootstrap = require('angular-ui-bootstrap'); // Note: ngHttpAuth seems to have no module.exports. ngHttpAuth = 'http-auth-interceptor'; var loginTmpl = require('./login.tmpl.html'); var base64 = require('base64util'); module.exports = angular.module('accountant.login', [ ngHttpAuth, ngStorage, ngUiBootstrap ]) .service('LoginService', 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 }; }) .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'); }; }) .run(function($rootScope, LoginService) { var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', LoginService.login); var onAuthLoginCancelled = $rootScope.$on('event:auth-loginCancelled', LoginService.cancelLogin); $rootScope.$on('$destroy', function() { onAuthLoginRequired = angular.noop(); onAuthLoginCancelled = angular.noop(); }); }) .name;