| author | Alexandre Segura <mex.zktk@gmail.com> |
| Fri, 16 Jun 2017 18:36:39 +0200 | |
| changeset 44 | 3b20e2b584fe |
| parent 12 | 48ddaa42b810 |
| child 47 | 64428c7ebc19 |
| permissions | -rw-r--r-- |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
1 |
import * as types from '../constants/actionTypes'; |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
2 |
|
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
3 |
export const isAuthenticated = (state = false, action) => { |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
4 |
switch (action.type) { |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
5 |
default: |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
6 |
return state; |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
7 |
} |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
8 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
9 |
|
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
10 |
export const currentUser = (state = null, action) => { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
11 |
switch (action.type) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
12 |
case types.AUTH_LOGIN_SUCCESS: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
13 |
return action.user; |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
14 |
default: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
15 |
return state; |
|
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 |
|
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
19 |
export const token = (state = null, action) => { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
20 |
switch (action.type) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
21 |
case types.AUTH_LOGIN_SUCCESS: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
22 |
console.log('token', action.token) |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
23 |
return action.token; |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
24 |
default: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
25 |
return state; |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
26 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
27 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
28 |
|
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
29 |
// TODO Use Immutable.Map |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
30 |
const loginState = { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
31 |
loading: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
32 |
success: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
33 |
error: false, |
|
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 |
|
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
36 |
export const login = (state = loginState, action) => { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
37 |
switch (action.type) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
38 |
case types.AUTH_LOGIN_REQUEST: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
39 |
return { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
40 |
loading: true, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
41 |
success: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
42 |
error: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
43 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
44 |
case types.AUTH_LOGIN_SUCCESS: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
45 |
case types.AUTH_LOGIN_ERROR: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
46 |
return { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
47 |
loading: false, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
48 |
success: action.type === types.AUTH_LOGIN_SUCCESS, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
49 |
error: action.type === types.AUTH_LOGIN_ERROR, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
50 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
51 |
default: |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
52 |
return state |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
53 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
12
diff
changeset
|
54 |
} |