summaryrefslogtreecommitdiff
path: root/app/client/auth/auth.reducer.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/client/auth/auth.reducer.js')
-rw-r--r--app/client/auth/auth.reducer.js82
1 files changed, 82 insertions, 0 deletions
diff --git a/app/client/auth/auth.reducer.js b/app/client/auth/auth.reducer.js
new file mode 100644
index 0000000..cacb0d5
--- /dev/null
+++ b/app/client/auth/auth.reducer.js
@@ -0,0 +1,82 @@
+import types from '../types';
+
+const authInitialState = {
+ token: null,
+ user: {},
+ groups: {},
+ loading: false,
+ isAuthenticated: false,
+};
+
+const auth = (state = authInitialState, action) => {
+ switch(action.type) {
+ case types.auth.set_token:
+ return {
+ ...state,
+ token: action.data,
+ isAuthenticated: !!action.data,
+ loading: false,
+ error: null,
+ };
+
+ case types.auth.loading:
+ return {
+ ...state,
+ loading: true,
+ error: null,
+ };
+
+ case types.auth.set_current_user:
+ const groups = {}
+ action.data.groups.forEach(g => groups[g.name.toLowerCase()] = true)
+ if (action.data.is_staff) {
+ groups['staff'] = true
+ }
+ if (action.data.is_superuser) {
+ groups['superuser'] = true
+ }
+ return {
+ ...state,
+ user: action.data,
+ groups,
+ error: null,
+ };
+
+ case types.auth.logout_user:
+ return {
+ ...authInitialState
+ };
+
+ case types.auth.set_error:
+ return {
+ ...state,
+ loading: false,
+ error: action.data,
+ }
+
+ case types.auth.loading:
+ // const initial_state_el = document.querySelector('#initial_state')
+ // if (initial_state_el) {
+ // try {
+ // const initial_state = JSON.parse(initial_state_el.innerHTML)
+ // if (initial_state && initial_state.auth && initial_state.auth.user) {
+ // console.log(initial_state.auth.user)
+ // return {
+ // ...state,
+ // user: {
+ // ...initial_state.auth.user,
+ // }
+ // }
+ // }
+ // } catch (e) {
+ // console.error("error loading initial state")
+ // }
+ // }
+ return state;
+
+ default:
+ return state;
+ }
+}
+
+export default auth;