This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAuthorisation.js
More file actions
113 lines (101 loc) · 2.71 KB
/
Authorisation.js
File metadata and controls
113 lines (101 loc) · 2.71 KB
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
110
111
112
113
import React from 'react';
import PropTypes from 'prop-types';
import { Login, Logout } from './index';
import { reloadInvalidatedSections } from '../../util/magento/customerData';
import config from '../../config';
class Authorisation extends React.Component {
state = {
message: {
type: '',
text: ''
}
};
handleLogin = async (username, password) => {
const { loginLink } = config;
await window
.fetch(loginLink, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
credentials: 'include',
body: JSON.stringify({
username: username,
password: password,
// persistent_remember_me: '0', // 0 || 1
context: 'checkout'
})
})
.then(response => {
if (response.status >= 200 && response.status < 300) {
return response.json();
} else {
let error = new Error(response.statusText);
error.response = response;
throw error;
}
})
.then(response => {
if (
response.errors === true ||
(response.hasOwnProperty('ok') && response.ok === false)
) {
response.error = true;
this.setState({
message: { text: response.message, type: 'error' }
});
return response;
} else {
this.setState({
message: { text: response.message, type: 'success' }
});
reloadInvalidatedSections();
return response;
}
})
.catch(error => {
this.setState({
message: { text: error.message, type: 'error' }
});
console.log('login action failed:', error);
});
};
render() {
const {
props: {
customer: { isLoggedIn },
isLoading
},
state: { message },
handleLogin
} = this;
const { loginLink, forgotPasswordLink, logoutLink } = config;
return (
<div className={'login-container'}>
{isLoading && 'Loading...'}
{!isLoading && isLoggedIn && <Logout logoutLink={logoutLink} />}
{!isLoading && !isLoggedIn && (
<Login
handleLogin={handleLogin}
message={message}
forgotPasswordLink={forgotPasswordLink}
loginLink={loginLink}
/>
)}
</div>
);
}
}
Authorisation.propTypes = {
customer: PropTypes.object.isRequired,
isLoading: PropTypes.bool.isRequired
};
Authorisation.defaultValues = {
customer: {},
isLoading: true
};
export default Authorisation;