Move login handling in a separate module.

This commit is contained in:
Alexis Lahouze 2017-06-10 22:50:48 +02:00
parent 5627746e98
commit f4d0988fdd
3 changed files with 162 additions and 130 deletions

View File

@ -20,20 +20,10 @@
var angular = require('angular'); var angular = require('angular');
var ngRoute = require('angular-route'), var ngRoute = require('angular-route');
ngBootbox = require('ngbootbox'),
ngStorage = require('meanie-angular-storage'),
ngHttpAuth = require('angular-http-auth');
var base64 =require('base64util');
// Note: ngBootbox seems to have no module.exports.
ngBootbox = 'ngBootbox';
// Note: ngHttpAuth seems to have no module.exports.
ngHttpAuth = 'http-auth-interceptor';
var accountModule = require('./accounts'), var accountModule = require('./accounts'),
loginModule = require('./login'),
operationModule = require('./operations'), operationModule = require('./operations'),
schedulerModule = require('./scheduler'); schedulerModule = require('./scheduler');
@ -43,42 +33,14 @@ var operationsTmpl = require('./operations/operations.html');
var accountsTmpl = require('./accounts/accounts.html'); var accountsTmpl = require('./accounts/accounts.html');
var schedulerTmpl = require('./scheduler/scheduler.html'); var schedulerTmpl = require('./scheduler/scheduler.html');
var loginTmpl = require('../views/login.tmpl.html');
var app = angular.module('accountant', [ var app = angular.module('accountant', [
accountModule.name, accountModule.name,
loginModule.name,
operationModule.name, operationModule.name,
schedulerModule.name, schedulerModule.name,
ngRoute, ngRoute,
ngBootbox,
ngHttpAuth,
ngStorage
]) ])
.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($routeProvider) { .config(function($routeProvider) {
// Defining template and controller in function of route. // Defining template and controller in function of route.
$routeProvider $routeProvider
@ -102,98 +64,11 @@ var app = angular.module('accountant', [
}); });
}) })
.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(editableOptions) { .run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default' editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
}) })
.factory('LoginService', function($http) { .controller('MainController', function() {
var login = function(email, password) {
var authdata = base64.encode(email + ':' + password);
return $http.post('/api/user/login', {}, {
headers: {
'authorization': 'Basic ' + authdata
}
}); });
};
return {
'login': login
};
})
.controller('MainController', function($rootScope, LoginService, authService, $storage, $ngBootbox, $document) {
var vm = this;
vm.dialogShown = false;
vm.showLoginForm = function() {
// First, if there are registered credentials, use them
if (vm.dialogShown) {
return;
}
vm.dialogShown = true;
$storage.session.clear();
$ngBootbox.customDialog({
title: 'Authentification requise',
templateUrl: loginTmpl,
buttons: {
login: {
label: 'Login',
className: 'btn-primary',
callback: function() {
vm.dialogShown = false;
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.');
vm.dialogShown = false;
}
}
}
});
};
vm.onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', vm.showLoginForm);
$rootScope.$on('$destroy', function() {
vm.onAuthLoginRequired = angular.noop();
});
})
;
module.exports = app; module.exports = app;

157
src/js/login/index.js Normal file
View File

@ -0,0 +1,157 @@
// 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']);
})
.value('dialogShow', false)
.run(function($rootScope, LoginService, authService, $storage, $ngBootbox, $document) {
//var dialogShown = false;
var showLoginForm = function() {
// First, if there are registered credentials, use them
//if (dialogShown) {
// return;
//}
//dialogShown = true;
$storage.session.clear();
$ngBootbox.customDialog({
title: 'Authentification requise',
templateUrl: loginTmpl,
buttons: {
login: {
label: 'Login',
className: 'btn-primary',
callback: function() {
//dialogShown = false;
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.');
//dialogShown = false;
}
}
}
});
};
var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', showLoginForm);
$rootScope.$on('$destroy', function() {
onAuthLoginRequired = angular.noop();
});
});
module.exports = loginModule;