Created
August 2, 2018 11:12
-
-
Save themojilla/bb0003955f225b93f8f271127ee223c7 to your computer and use it in GitHub Desktop.
auth reducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'isomorphic-fetch'; | |
/** | |
* | |
* Actions */ | |
export const LOGIN_REQUEST = 'LOGIN_REQUEST'; | |
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS'; | |
export const LOGIN_FAILURE = 'LOGIN_FAILURE'; | |
export const LOGOUT_REQUEST = 'LOGOUT_REQUEST'; | |
export const LOGOUT_SUCCESS = 'LOGOUT_SUCCESS'; | |
export const LOGOUT_FAILURE = 'LOGOUT_FAILURE'; | |
/** | |
* Reducer | |
* @param state | |
* @param action | |
* @returns {*} | |
*/ | |
export default function auth( | |
state = { | |
isFetching: false, | |
isAuthenticated: true | |
}, | |
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, { | |
isAuthenticated: false | |
}); | |
default: | |
return state; | |
} | |
} | |
/** | |
* | |
* Action Creators */ | |
export function requestLogin(creds) { | |
return { | |
type: LOGIN_REQUEST, | |
isFetching: true, | |
isAuthenticated: false, | |
creds | |
}; | |
} | |
export function receiveLogin(user) { | |
return { | |
type: LOGIN_SUCCESS, | |
isFetching: false, | |
isAuthenticated: true, | |
id_token: user.id_token | |
}; | |
} | |
export function loginError(message) { | |
return { | |
type: LOGIN_FAILURE, | |
isFetching: false, | |
isAuthenticated: false, | |
message | |
}; | |
} | |
export function requestLogout() { | |
return { | |
type: LOGOUT_REQUEST, | |
isFetching: true, | |
isAuthenticated: true | |
}; | |
} | |
export function receiveLogout() { | |
return { | |
type: LOGOUT_SUCCESS, | |
isFetching: false, | |
isAuthenticated: false | |
}; | |
} | |
/** | |
* | |
* Side effects, thunks */ | |
export function loginUser(creds) { | |
const config = { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
credentials: 'include', | |
body: `login=${creds.login}&password=${creds.password}` | |
}; | |
return dispatch => { | |
// We dispatch requestLogin to kickoff the call to the API | |
dispatch(requestLogin(creds)); | |
return fetch('/login', config) | |
.then(response => response.json().then(user => ({ user, response }))) | |
.then(({ user, response }) => { | |
if (!response.ok) { | |
// If there was a problem, we want to | |
// dispatch the error condition | |
dispatch(loginError(user.message)); | |
return Promise.reject(user); | |
} | |
// in posts create new action and check http status, if malign logout | |
// If login was successful, set the token in local storage | |
localStorage.setItem('id_token', user.id_token); | |
// Dispatch the success action | |
dispatch(receiveLogin(user)); | |
return Promise.resolve(user); | |
}) | |
.catch(err => console.error('Error: ', err)); | |
}; | |
} | |
export function logoutUser() { | |
return dispatch => { | |
dispatch(requestLogout()); | |
localStorage.removeItem('id_token'); | |
document.cookie = 'id_token=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | |
dispatch(receiveLogout()); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment