forked from auth0-blog/redux-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducers.js
76 lines (72 loc) · 1.86 KB
/
reducers.js
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
import { combineReducers } from 'redux'
import {
LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT_SUCCESS,
QUOTE_REQUEST, QUOTE_SUCCESS, QUOTE_FAILURE
} from './actions'
// The auth reducer. The starting state sets authentication
// based on a token being in local storage. In a real app,
// we would also want a util to check if the token is expired.
function auth(state = {
isFetching: false,
isAuthenticated: localStorage.getItem('id_token') ? true : false
}, action) {
switch (action.type) {
case LOGIN_REQUEST:
return Object.assign({}, state, {
isFetching: true,
isAuthenticated: false,
user: action.creds
})
case LOGIN_SUCCESS:
return Object.assign({}, state, {
isFetching: false,
isAuthenticated: true,
errorMessage: ''
})
case LOGIN_FAILURE:
return Object.assign({}, state, {
isFetching: false,
isAuthenticated: false,
errorMessage: action.message
})
case LOGOUT_SUCCESS:
return Object.assign({}, state, {
isFetching: true,
isAuthenticated: false
})
default:
return state
}
}
// The quotes reducer
function quotes(state = {
isFetching: false,
quote: '',
authenticated: false
}, action) {
switch (action.type) {
case QUOTE_REQUEST:
return Object.assign({}, state, {
isFetching: true
})
case QUOTE_SUCCESS:
return Object.assign({}, state, {
isFetching: false,
quote: action.response,
authenticated: action.authenticated || false
})
case QUOTE_FAILURE:
return Object.assign({}, state, {
isFetching: false
})
default:
return state
}
}
// We combine the reducers here so that they
// can be left split apart above
const quotesApp = combineReducers({
auth,
quotes
})
export default quotesApp