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) {
var sessionInjector = {
request: function(config) {
var token = $storage.get('token');
var access_token = $storage.session.get('access_token');
if (token) {
var tokenType = $storage.get('token_type');
var authorization = tokenType + ' ' + token;
config.headers.Authorization = authorization;
if (access_token) {
//var tokenType = $storage.get('token_type');
var tokenType = 'Bearer';
var authorization = tokenType + ' ' + access_token;
config.headers.authorization = authorization;
}
return config;
@ -121,9 +122,12 @@ var app = angular.module('accountant', [
.factory('LoginService', function($http) {
var login = function(email, password) {
return $http.post('/api/user/login', {
email: email,
password: password
var authdata = base64.encode(email + ':' + 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;
vm.dialogShown = false;
@ -145,7 +149,7 @@ var app = angular.module('accountant', [
vm.dialogShown = true;
$storage.clear();
$storage.session.clear();
$ngBootbox.customDialog({
title: 'Authentification requise',
@ -157,14 +161,15 @@ var app = angular.module('accountant', [
callback: function() {
vm.dialogShown = false;
var email = angular.element('#email').val();
var password = angular.element('#password').val();
LoginService.login(email, password).success(function(result) {
var email = angular.element($document.querySelector('#email')).val();
var password = angular.element($document.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.set('token_type', result.token_type);
$storage.set('token', result.token);
$storage.set('expiration_date', result.expiration_date);
$storage.session.set('refresh_token', result.data.refresh_token);
$storage.session.set('access_token', result.data.access_token);
authService.loginConfirmed();
});