Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
export const SHOW_LOGIN_DIALOG = 'SHOW_LOGIN_DIALOG';
export const HIDE_LOGIN_DIALOG = 'HIDE_LOGIN_DIALOG';
export const AUTHENTICATION_PENDING = 'AUTHENTICATION_PENDING'
export const AUTHENTICATION_SUCCESS = 'AUTHENTICATION_SUCCESS'
export const AUTHENTICATION_ERROR = 'AUTHENTICATION_ERROR'
export function getShowLoginDialogAction() {
return {
type: SHOW_LOGIN_DIALOG
}
}
export function getHideLoginDialogAction() {
return {
type: HIDE_LOGIN_DIALOG
}
}
export function getAuthenticationPendingAction() {
return {
type: AUTHENTICATION_PENDING
}
}
export function getAuthenticationSuccessAction(userSession) {
return {
type: AUTHENTICATION_SUCCESS,
user: userSession.user,
accessToken: userSession.accessToken
}
}
export function getAuthenticationErrorAction(error) {
return {
type: AUTHENTICATION_ERROR,
error: error
}
}
export function authenticateUser(userID, password) {
console.log("Authenticate")
return dispatch => {
dispatch(getAuthenticationPendingAction());
login(userID, password)
.then(
userSession => {
const action = getAuthenticationSuccessAction(userSession);
dispatch(action);
},
error => {
dispatch(getAuthenticationErrorAction(error));
}
)
.catch(error => {
dispatch(getAuthenticationErrorAction(error))
})
}
}
function login(userID, password) {
const requestOptions = {
method: 'GET',
headers: { 'Authorization': 'Basic ' + btoa(userID + ":" + password) }
};
console.log(userID)
console.log(password)
return fetch('https://localhost/api/authenticate', requestOptions)
.then(handleResponse)
.then(userSession => {
return userSession
});
}
function handleResponse(response) {
const authorizationHeader = response.headers.get('Authorization');
return response.text().then(text => {
console.log('Receive result: ' + authorizationHeader)
const data = text && JSON.parse(text);
var token
if (authorizationHeader) {
token = authorizationHeader.split(" ")[1];
}
if (!response.ok) {
if (response.status === 401) {
logout();
}
const error = (data && data.message) || response.statusText;
return Promise.reject(error);
} else {
let userSession = {
user: data,
accessToken: token
}
return userSession
}
});
}
function logout() {
console.error('Should irgendas')
}