// 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'), angularUiBootstrap = 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'); var loginModule = angular.module('accountant.login', [ ngHttpAuth, ngStorage, angularUiBootstrap ]) .factory('LoginService', function($uibModal, $storage, $document, $log, authService) { var login = function () { $storage.session.clear(); var modalInstance = $uibModal.open({ animation: true, ariaLabelledBy: 'modal-title', ariaDescribedBy: 'modal-body', templateUrl: loginTmpl, controller: 'LoginModalController', controllerAs: '$ctrl', size: 'lg', }); modalInstance.result.then(function (data) { $log.log(data); // TODO Alexis Lahouze 2015-08-28 Handle callback. // Call to /api/login to retrieve the token $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()); authService.loginCancelled(null, 'Login cancelled by user action.'); }); }; return { 'login': login }; }) .factory('sessionInjector', function($storage) { var sessionInjector = { 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; } }; return sessionInjector; }) .config(function($httpProvider) { // Define interceptors. $httpProvider.interceptors.push('sessionInjector'); }) .config(function($storageProvider) { // 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: 'user@example.com', password: 'password' }; vm.ok = function() { var email = vm.data.email; var password = vm.data.password; $log.log(email, password); var authdata = base64.encode(email + ':' + password); return $http.post('/api/user/login', {}, { headers: { 'authorization': 'Basic ' + authdata } }).then(function(result) { $log.log(result); $uibModalInstance.close(result.data); }); }; vm.cancel = function() { $uibModalInstance.dismiss('cancel'); }; }) .run(function($rootScope, LoginService) { var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', LoginService.login); $rootScope.$on('$destroy', function() { onAuthLoginRequired = angular.noop(); }); }); module.exports = loginModule;