Separate login module into different files.

This commit is contained in:
Alexis Lahouze 2017-07-08 00:51:59 +02:00
parent 292486f8fd
commit 0c85266ab2
4 changed files with 108 additions and 101 deletions

View File

@ -29,6 +29,10 @@ ngHttpAuth = 'http-auth-interceptor';
var loginTmpl = require('./login.tmpl.html'); var loginTmpl = require('./login.tmpl.html');
var LoginService = require('./login.service');
var LoginConfig = require('./login.config');
var LoginController = require('./login.controller');
var base64 = require('base64util'); var base64 = require('base64util');
module.exports = angular.module('accountant.login', [ module.exports = angular.module('accountant.login', [
@ -37,109 +41,11 @@ module.exports = angular.module('accountant.login', [
ngUiBootstrap ngUiBootstrap
]) ])
.service('LoginService', function($uibModal, $storage, $document, $log, authService) { .service('LoginService', LoginService)
var login = function () {
$storage.session.clear();
var modalInstance = $uibModal.open({ .config(LoginConfig)
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
templateUrl: loginTmpl,
controller: 'LoginModalController',
controllerAs: '$ctrl'
});
modalInstance.result.then(function (data) { .controller('LoginModalController', LoginController)
$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) { .run(function($rootScope, LoginService) {
var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', LoginService.login); var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', LoginService.login);

31
src/login/login.config.js Normal file
View File

@ -0,0 +1,31 @@
module.exports = 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']);
};

View File

@ -0,0 +1,34 @@
module.exports = 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');
};
};

View File

@ -0,0 +1,36 @@
module.exports = 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
};
};