diff options
Diffstat (limited to 'app/client/auth/auth.actions.js')
| -rw-r--r-- | app/client/auth/auth.actions.js | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/app/client/auth/auth.actions.js b/app/client/auth/auth.actions.js index 991a3f9..3171996 100644 --- a/app/client/auth/auth.actions.js +++ b/app/client/auth/auth.actions.js @@ -4,6 +4,9 @@ import { put } from '../api/crud.fetch' export const setToken = (data) => { return { type: types.auth.set_token, data } } +export const setReturnTo = (data) => { + return { type: types.auth.set_return_to, data } +} export const setError = (data) => { return { type: types.auth.set_error, data } } @@ -13,7 +16,10 @@ export const setCurrentUser = (data) => { export function logout() { return { type: types.auth.logout_user } } -export function authLoading() { +export function initialized() { + return { type: types.auth.initialized } +} +export function loading() { return { type: types.auth.loading } } @@ -31,29 +37,27 @@ const api = { export function login(username, password) { return (dispatch) => { - dispatch(authLoading()) + dispatch(loading()) fetch(api.login, put({ username, password })) .then(req => req.json()) .then(data => { + console.log(data) dispatch(setToken(data.token)) dispatch(checkin()) }) .catch(error => { + console.log(error) dispatch(setError(true)) - if (error.response.status === 400) { - throw new InvalidCredentialsException(error) - } - throw error }) } } export function signup(data) { return (dispatch) => { - dispatch(authLoading()) + dispatch(loading()) fetch(api.signup, put(data)) .then(req => req.json()) .then(data => { @@ -62,19 +66,15 @@ export function signup(data) { }) .catch(error => { console.log(error) - if (error.response.status === 400) { - // dispatch(accountError("There was an error creating your account.")) - throw new InvalidCredentialsException(error) - } - throw error + dispatch(initialized()) }) } } -export function checkin() { +export function checkin(history) { return (dispatch) => { - dispatch(authLoading()) - fetch(api.checkin) + dispatch(loading()) + fetch(api.checkin, put({})) .then(req => req.json()) .then(data => { console.log(data) @@ -82,10 +82,9 @@ export function checkin() { console.log('set current user') }) .catch(error => { - if (error.response.status === 400) { - throw new InvalidCredentialsException(error) - } - throw error + console.log(error) + // history.go('/login') + dispatch(initialized(true)) }) } } |
