accountant-ui/src/login/index.js

144 lines
4.5 KiB
JavaScript
Raw Normal View History

// 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');
var ngBootbox = require('ngbootbox'),
ngStorage = require('meanie-angular-storage'),
ngHttpAuth = require('angular-http-auth');
// Note: ngBootbox seems to have no module.exports.
ngBootbox = 'ngBootbox';
// 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', [
ngBootbox,
ngHttpAuth,
ngStorage
])
.factory('LoginService', function($http) {
var login = function(email, password) {
var authdata = base64.encode(email + ':' + password);
return $http.post('/api/user/login', {}, {
headers: {
'authorization': 'Basic ' + authdata
}
});
};
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']);
})
.run(function($rootScope, LoginService, authService, $storage, $ngBootbox, $document) {
var showLoginForm = function() {
$storage.session.clear();
$ngBootbox.customDialog({
title: 'Authentification requise',
templateUrl: loginTmpl,
buttons: {
login: {
label: 'Login',
className: 'btn-primary',
callback: function() {
var email = angular.element($document[0].querySelector('#email')).val();
var password = angular.element($document[0].querySelector('#password')).val();
LoginService.login(
email, password
).then(function(result) {
// TODO Alexis Lahouze 2015-08-28 Handle callback.
// Call to /api/login to retrieve the token
$storage.session.set('refresh_token', result.data.refresh_token);
$storage.session.set('access_token', result.data.access_token);
authService.loginConfirmed();
});
}
},
cancel: {
label: 'Annuler',
className: 'btn-default',
callback: function() {
authService.loginCancelled(null, 'Login cancelled by user action.');
}
}
}
});
};
var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', showLoginForm);
$rootScope.$on('$destroy', function() {
onAuthLoginRequired = angular.noop();
});
});
module.exports = loginModule;