From d3e4bb3ed2585859a3adeb7eeff35b7c75ebd840 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sun, 16 Sep 2018 22:40:05 +0200 Subject: auth gate on main app. pull in auth routes from bucky. --- app/client/auth/auth.reducer.js | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 app/client/auth/auth.reducer.js (limited to 'app/client/auth/auth.reducer.js') 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; -- cgit v1.2.3-70-g09d2