2017-06-10 22:50:48 +02:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/* jshint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var angular = require('angular');
|
|
|
|
|
2017-06-11 00:48:03 +02:00
|
|
|
var ngStorage = require('meanie-angular-storage'),
|
|
|
|
ngHttpAuth = require('angular-http-auth'),
|
2017-06-11 09:16:44 +02:00
|
|
|
ngUiBootstrap = require('angular-ui-bootstrap');
|
2017-06-10 22:50:48 +02:00
|
|
|
|
|
|
|
// 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,
|
2017-06-11 00:48:03 +02:00
|
|
|
ngStorage,
|
2017-06-11 09:16:44 +02:00
|
|
|
ngUiBootstrap
|
2017-06-10 22:50:48 +02:00
|
|
|
])
|
|
|
|
|
2017-06-11 00:48:03 +02:00
|
|
|
.factory('LoginService', function($uibModal, $storage, $document, $log, authService) {
|
|
|
|
var login = function () {
|
|
|
|
$storage.session.clear();
|
2017-06-10 22:50:48 +02:00
|
|
|
|
2017-06-11 00:48:03 +02:00
|
|
|
var modalInstance = $uibModal.open({
|
|
|
|
ariaLabelledBy: 'modal-title',
|
|
|
|
ariaDescribedBy: 'modal-body',
|
|
|
|
templateUrl: loginTmpl,
|
|
|
|
controller: 'LoginModalController',
|
2017-06-11 01:14:26 +02:00
|
|
|
controllerAs: '$ctrl'
|
2017-06-11 00:48:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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());
|
2017-06-11 01:14:26 +02:00
|
|
|
|
|
|
|
// FIXME Alexis Lahouze 2017-06-11 Redirect to error page.
|
2017-06-11 00:48:03 +02:00
|
|
|
authService.loginCancelled(null, 'Login cancelled by user action.');
|
2017-06-10 22:50:48 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
})
|
|
|
|
|
2017-06-11 08:07:48 +02:00
|
|
|
.config(function($httpProvider, $storageProvider) {
|
2017-06-10 22:50:48 +02:00
|
|
|
// Define interceptors.
|
|
|
|
$httpProvider.interceptors.push('sessionInjector');
|
|
|
|
|
|
|
|
// 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']);
|
|
|
|
})
|
|
|
|
|
2017-06-11 00:48:03 +02:00
|
|
|
.controller('LoginModalController', function($scope, $uibModalInstance, $http, $log) {
|
|
|
|
var vm = this;
|
2017-06-10 22:50:48 +02:00
|
|
|
|
2017-06-11 00:48:03 +02:00
|
|
|
vm.data = {
|
|
|
|
email: 'user@example.com',
|
|
|
|
password: 'password'
|
|
|
|
};
|
2017-06-10 22:50:48 +02:00
|
|
|
|
2017-06-11 00:48:03 +02:00
|
|
|
vm.ok = function() {
|
|
|
|
var email = vm.data.email;
|
|
|
|
var password = vm.data.password;
|
|
|
|
|
2017-06-11 01:14:26 +02:00
|
|
|
// Encode authentication data.
|
2017-06-11 00:48:03 +02:00
|
|
|
var authdata = base64.encode(email + ':' + password);
|
|
|
|
|
|
|
|
return $http.post('/api/user/login', {}, {
|
|
|
|
headers: {
|
|
|
|
'authorization': 'Basic ' + authdata
|
2017-06-10 22:50:48 +02:00
|
|
|
}
|
2017-06-11 00:48:03 +02:00
|
|
|
}).then(function(result) {
|
|
|
|
$log.log(result);
|
|
|
|
|
|
|
|
$uibModalInstance.close(result.data);
|
2017-06-11 01:14:26 +02:00
|
|
|
}, function(response) {
|
|
|
|
// FIXME Alexis Lahouze 2017-06-11 Handle error.
|
|
|
|
$log.log("Error on login", response);
|
2017-06-10 22:50:48 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-11 00:48:03 +02:00
|
|
|
vm.cancel = function() {
|
|
|
|
$uibModalInstance.dismiss('cancel');
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
.run(function($rootScope, LoginService) {
|
|
|
|
var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', LoginService.login);
|
2017-06-10 22:50:48 +02:00
|
|
|
|
|
|
|
$rootScope.$on('$destroy', function() {
|
|
|
|
onAuthLoginRequired = angular.noop();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = loginModule;
|