Fix authentication process.

This commit is contained in:
Alexis Lahouze 2017-06-10 18:19:00 +02:00
parent c3daad9cd7
commit 548c4ae23e

View File

@ -58,12 +58,13 @@ var app = angular.module('accountant', [
.factory('sessionInjector', function($storage) { .factory('sessionInjector', function($storage) {
var sessionInjector = { var sessionInjector = {
request: function(config) { request: function(config) {
var token = $storage.get('token'); var access_token = $storage.session.get('access_token');
if (token) { if (access_token) {
var tokenType = $storage.get('token_type'); //var tokenType = $storage.get('token_type');
var authorization = tokenType + ' ' + token; var tokenType = 'Bearer';
config.headers.Authorization = authorization; var authorization = tokenType + ' ' + access_token;
config.headers.authorization = authorization;
} }
return config; return config;
@ -121,9 +122,12 @@ var app = angular.module('accountant', [
.factory('LoginService', function($http) { .factory('LoginService', function($http) {
var login = function(email, password) { var login = function(email, password) {
return $http.post('/api/user/login', { var authdata = base64.encode(email + ':' + password);
email: email,
password: password return $http.post('/api/user/login', {}, {
headers: {
'authorization': 'Basic ' + authdata
}
}); });
}; };
@ -132,7 +136,7 @@ var app = angular.module('accountant', [
}; };
}) })
.controller('MainController', function($rootScope, LoginService, authService, $storage, $ngBootbox) { .controller('MainController', function($rootScope, LoginService, authService, $storage, $ngBootbox, $document) {
var vm = this; var vm = this;
vm.dialogShown = false; vm.dialogShown = false;
@ -145,7 +149,7 @@ var app = angular.module('accountant', [
vm.dialogShown = true; vm.dialogShown = true;
$storage.clear(); $storage.session.clear();
$ngBootbox.customDialog({ $ngBootbox.customDialog({
title: 'Authentification requise', title: 'Authentification requise',
@ -157,14 +161,15 @@ var app = angular.module('accountant', [
callback: function() { callback: function() {
vm.dialogShown = false; vm.dialogShown = false;
var email = angular.element('#email').val(); var email = angular.element($document.querySelector('#email')).val();
var password = angular.element('#password').val(); var password = angular.element($document.querySelector('#password')).val();
LoginService.login(email, password).success(function(result) { LoginService.login(
email, password
).then(function(result) {
// TODO Alexis Lahouze 2015-08-28 Handle callback. // TODO Alexis Lahouze 2015-08-28 Handle callback.
// Call to /api/login to retrieve the token // Call to /api/login to retrieve the token
$storage.set('token_type', result.token_type); $storage.session.set('refresh_token', result.data.refresh_token);
$storage.set('token', result.token); $storage.session.set('access_token', result.data.access_token);
$storage.set('expiration_date', result.expiration_date);
authService.loginConfirmed(); authService.loginConfirmed();
}); });