| author | Alexandre Segura <mex.zktk@gmail.com> |
| Mon, 26 Jun 2017 16:48:52 +0200 | |
| changeset 91 | 143ff08ec2cc |
| parent 90 | 990f2c928b15 |
| child 100 | 6fd752d98933 |
| permissions | -rw-r--r-- |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
1 |
import Immutable from 'immutable'; |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
2 |
import * as types from '../constants/actionTypes'; |
| 67 | 3 |
import UserRecord from '../store/userRecord'; |
| 91 | 4 |
import asyncRequest from '../constants/asyncRequest'; |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
5 |
|
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
6 |
export const isAuthenticated = (state = false, action) => { |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
7 |
switch (action.type) { |
|
88
2a861fed6bde
Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
8 |
case types.AUTH_DEAUTHENTICATE: |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
9 |
case types.AUTH_LOGOUT: |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
10 |
return false; |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
11 |
case types.AUTH_LOGIN_SUCCESS: |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
12 |
return true; |
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
13 |
default: |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
14 |
return state; |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
15 |
} |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
16 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
17 |
|
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
18 |
export const currentUser = (state = null, action) => { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
19 |
switch (action.type) { |
|
88
2a861fed6bde
Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
20 |
case types.AUTH_DEAUTHENTICATE: |
| 52 | 21 |
case types.AUTH_LOGOUT: |
22 |
return null; |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
23 |
case types.AUTH_LOGIN_SUCCESS: |
| 67 | 24 |
return new UserRecord(action.user); |
| 53 | 25 |
case types.USER_UPDATE_SETTINGS: |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
26 |
return state.merge({ |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
27 |
first_name: action.firstname, |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
28 |
last_name: action.lastname |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
29 |
}); |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
30 |
default: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
31 |
return state; |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
32 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
33 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
34 |
|
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
35 |
export const token = (state = null, action) => { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
36 |
switch (action.type) { |
|
88
2a861fed6bde
Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
37 |
case types.AUTH_DEAUTHENTICATE: |
| 52 | 38 |
case types.AUTH_LOGOUT: |
39 |
return null; |
|
| 47 | 40 |
case types.AUTH_STORE_TOKEN: |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
41 |
return action.token; |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
42 |
default: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
43 |
return state; |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
44 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
45 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
46 |
|
| 91 | 47 |
export const login = (state = asyncRequest, action) => { |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
48 |
switch (action.type) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
49 |
case types.AUTH_LOGIN_REQUEST: |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
50 |
return Immutable.Map({ |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
51 |
loading: true, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
52 |
success: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
53 |
error: false, |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
54 |
}) |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
55 |
case types.AUTH_LOGIN_SUCCESS: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
56 |
case types.AUTH_LOGIN_ERROR: |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
57 |
return Immutable.Map({ |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
58 |
loading: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
59 |
success: action.type === types.AUTH_LOGIN_SUCCESS, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
60 |
error: action.type === types.AUTH_LOGIN_ERROR, |
| 90 | 61 |
errorMessages: action.type === types.AUTH_LOGIN_ERROR ? Immutable.Map(action.error) : Immutable.Map({}) |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
53
diff
changeset
|
62 |
}) |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
63 |
default: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
64 |
return state |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
65 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
66 |
} |
|
89
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
67 |
|
| 91 | 68 |
export const register = (state = asyncRequest, action) => { |
|
89
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
69 |
switch (action.type) { |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
70 |
case types.AUTH_REGISTER_REQUEST: |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
71 |
return Immutable.Map({ |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
72 |
loading: true, |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
73 |
success: false, |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
74 |
error: false, |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
75 |
}) |
| 90 | 76 |
case types.AUTH_LOGIN_SUCCESS: |
|
89
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
77 |
case types.AUTH_REGISTER_ERROR: |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
78 |
return Immutable.Map({ |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
79 |
loading: false, |
| 90 | 80 |
success: action.type === types.AUTH_LOGIN_SUCCESS, |
|
89
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
81 |
error: action.type === types.AUTH_REGISTER_ERROR, |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
82 |
errorMessages: action.type === types.AUTH_REGISTER_ERROR ? Immutable.Map(action.error) : Immutable.Map({}) |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
83 |
}) |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
84 |
default: |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
85 |
return state |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
86 |
} |
|
06f609adfbf8
Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents:
88
diff
changeset
|
87 |
} |