|
1 | | -import { getServiceURL } from '../utilities/rest'; |
| 1 | +import cookies from 'react-cookies'; |
| 2 | + |
| 3 | +import { |
| 4 | + getAuthenticationHeaders, |
| 5 | + getLoginHeaders, |
| 6 | + getServiceURL, |
| 7 | + handleHTTPErrors, |
| 8 | +} from '../utilities/rest'; |
2 | 9 |
|
3 | 10 | export const USER_LOGIN_BEGIN = 'USER_LOGIN_BEGIN'; |
4 | 11 | export const USER_LOGIN_SUCCESS = 'USER_LOGIN_SUCCESS'; |
5 | 12 | export const USER_LOGIN_FAILURE = 'USER_LOGIN_FAILURE'; |
| 13 | + |
6 | 14 | export const USER_LOGOUT = 'USER_LOGOUT'; |
7 | 15 |
|
8 | | -export const userLoginBegin = keepSignedIn => ({ |
| 16 | +const userLoginBegin = keepSignedIn => ({ |
9 | 17 | type: USER_LOGIN_BEGIN, |
10 | 18 | payload: { |
11 | 19 | keepSignedIn, |
12 | 20 | }, |
13 | 21 | }); |
14 | 22 |
|
15 | | -export const userLoginSuccess = (userId, authenticationToken) => ({ |
| 23 | +const userLoginSuccess = (userId, authenticationToken) => ({ |
16 | 24 | type: USER_LOGIN_SUCCESS, |
17 | 25 | payload: { |
18 | 26 | userId, |
19 | 27 | authenticationToken, |
20 | 28 | }, |
21 | 29 | }); |
22 | 30 |
|
23 | | -export const userLoginFailure = error => ({ |
| 31 | +const userLoginFailure = error => ({ |
24 | 32 | type: USER_LOGIN_FAILURE, |
25 | 33 | payload: { |
26 | 34 | error, |
27 | 35 | }, |
28 | 36 | }); |
29 | 37 |
|
30 | | -export const userLogout = () => ({ |
| 38 | +const userLogout = () => ({ |
31 | 39 | type: USER_LOGOUT, |
32 | 40 | }); |
33 | 41 |
|
34 | | -const authenticationUsername = 'Authentication-Username'; |
35 | | -const authenticationPassword = 'Authentication-Password'; |
| 42 | +const USER_ID_COOKIE = 'USER_ID'; |
| 43 | +const TOKEN_COOKIE = 'TOKEN'; |
36 | 44 |
|
37 | 45 | export const login = (username, password, keepSignedIn) => (dispatch) => { |
38 | 46 | dispatch(userLoginBegin(keepSignedIn)); |
39 | 47 | return fetch( |
40 | 48 | getServiceURL('authenticate/getToken'), |
41 | | - // 'https://httpbin.org/get', |
42 | 49 | { |
43 | 50 | method: 'GET', |
44 | | - headers: { |
45 | | - [authenticationUsername]: username, |
46 | | - [authenticationPassword]: password, |
47 | | - }, |
| 51 | + headers: getLoginHeaders(username, password), |
48 | 52 | }, |
49 | 53 | ) |
50 | | - // TODO: HANDLE HTTPS ERRORS? |
| 54 | + .then(handleHTTPErrors) |
51 | 55 | .then(response => response.json()) |
52 | | - .then((json) => { |
53 | | - // TODO: SAVE ID AND TOKEN AS COOKIES WHEN KEEPSIGNEDIN IS SET |
54 | | - dispatch(userLoginSuccess(json.id, json.token)); |
| 56 | + .then(({ id, authenticationToken }) => { |
| 57 | + if (keepSignedIn) { |
| 58 | + cookies.save(USER_ID_COOKIE, id); |
| 59 | + cookies.save(TOKEN_COOKIE, authenticationToken); |
| 60 | + } |
| 61 | + |
| 62 | + dispatch(userLoginSuccess(id, authenticationToken)); |
55 | 63 | }) |
56 | 64 | .catch(error => dispatch(userLoginFailure(error))); |
57 | 65 | }; |
| 66 | + |
| 67 | +export const logout = () => (dispatch) => { |
| 68 | + cookies.remove(USER_ID_COOKIE); |
| 69 | + cookies.remove(TOKEN_COOKIE); |
| 70 | + |
| 71 | + dispatch(userLogout()); |
| 72 | +}; |
| 73 | + |
| 74 | +export const loadSessionIfAvailable = () => (dispatch) => { |
| 75 | + const userID = cookies.load(USER_ID_COOKIE); |
| 76 | + const token = cookies.load(TOKEN_COOKIE); |
| 77 | + |
| 78 | + if (!userID || !token) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + dispatch(userLoginBegin(true)); |
| 83 | + |
| 84 | + fetch( |
| 85 | + getServiceURL('authenticate/initialContact'), |
| 86 | + { |
| 87 | + method: 'GET', |
| 88 | + headers: getAuthenticationHeaders(userID, token), |
| 89 | + }, |
| 90 | + ) |
| 91 | + .then(handleHTTPErrors) |
| 92 | + .then(response => response.json) |
| 93 | + .then(() => dispatch(userLoginSuccess(userID, token))) |
| 94 | + .catch(error => dispatch(userLoginFailure(error))); |
| 95 | +}; |
0 commit comments