Created
June 18, 2018 11:39
-
-
Save manuelbieh/88a0499684cc784a3fbaf37e78db9cef to your computer and use it in GitHub Desktop.
request/success/failure
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
// @flow | |
import type { Dispatch } from 'redux'; | |
import { ClientRequest } from 'services/api'; | |
import { getTeamId } from 'store/team/selectors'; | |
import isClient from 'shared/utils/isClient'; | |
export const ActionTypes = { | |
ACCESS_TOKEN_REQUEST: 'auth/access-token/request', | |
ACCESS_TOKEN_SUCCESS: 'auth/access-token/success', | |
ACCESS_TOKEN_FAILURE: 'auth/access-token/failure', | |
ACCESS_TOKEN_INVALIDATE: 'auth/access-token/invalidate', | |
}; | |
export const accessTokenInvalidate = () => (dispatch: Dispatch<*>) => { | |
dispatch({ | |
type: ActionTypes.ACCESS_TOKEN_INVALIDATE, | |
}); | |
if (isClient()) { | |
document.cookie = `accessToken=;expires=${new Date(0).toUTCString()};path=/`; | |
} | |
}; | |
export const accessTokenRequest = () => ({ | |
type: ActionTypes.ACCESS_TOKEN_REQUEST, | |
}); | |
export const accessTokenSuccess = (accessToken: string) => ({ | |
type: ActionTypes.ACCESS_TOKEN_SUCCESS, | |
payload: accessToken, | |
}); | |
export const accessTokenFailure = () => ({ | |
type: ActionTypes.ACCESS_TOKEN_FAILURE, | |
}); | |
export const fetchAccessToken = ({ | |
email, | |
password, | |
}: { | |
email: string, | |
password: string, | |
}) => async (dispatch: Dispatch<*>, getState: () => {}) => { | |
try { | |
const teamId = getTeamId(getState()); | |
if (!teamId) { | |
// TODO: Add proper error handling here | |
} | |
dispatch(accessTokenRequest()); | |
const { data } = await ClientRequest().post('/oauth2/login', { | |
email, | |
password, | |
teamId, | |
}); | |
if (data.accessToken) { | |
dispatch(accessTokenSuccess(data.accessToken)); | |
if (isClient()) { | |
document.cookie = `accessToken=${data.accessToken};expires=${new Date( | |
data.expires | |
).toUTCString()};path=/`; | |
} | |
} | |
} catch (err) { | |
dispatch(accessTokenFailure()); | |
} | |
}; |
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
// @flow | |
import type { ActionT, AuthT } from './types'; | |
import { ActionTypes } from './actions'; | |
export const initialState: AuthT = Object.freeze({ | |
accessToken: null, | |
didInvalidate: false, | |
isLoading: false, | |
}); | |
export default (state: AuthT = initialState, action: ActionT): AuthT => { | |
const { type, payload } = action; | |
switch (type) { | |
case ActionTypes.ACCESS_TOKEN_INVALIDATE: { | |
return { | |
...state, | |
accessToken: null, | |
didInvalidate: true, | |
}; | |
} | |
case ActionTypes.ACCESS_TOKEN_REQUEST: { | |
return { | |
...state, | |
isLoading: true, | |
}; | |
} | |
case ActionTypes.ACCESS_TOKEN_SUCCESS: { | |
return { | |
...state, | |
accessToken: payload, | |
didInvalidate: false, | |
isLoading: false, | |
}; | |
} | |
case ActionTypes.ACCESS_TOKEN_FAILURE: { | |
return { | |
...state, | |
accessToken: null, | |
didInvalidate: true, | |
isLoading: false, | |
}; | |
} | |
} | |
return state; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment