Guest User

Untitled

a guest
Jun 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { ClientRequest } from 'services/api';
  4. import { getTeamId } from 'store/team/selectors';
  5. import isClient from 'shared/utils/isClient';
  6.  
  7. export const ActionTypes = {
  8. ACCESS_TOKEN_REQUEST: 'auth/access-token/request',
  9. ACCESS_TOKEN_SUCCESS: 'auth/access-token/success',
  10. ACCESS_TOKEN_FAILURE: 'auth/access-token/failure',
  11. ACCESS_TOKEN_INVALIDATE: 'auth/access-token/invalidate',
  12. };
  13.  
  14. export const accessTokenInvalidate = () => (dispatch: Dispatch<*>) => {
  15. dispatch({
  16. type: ActionTypes.ACCESS_TOKEN_INVALIDATE,
  17. });
  18.  
  19. if (isClient()) {
  20. document.cookie = `accessToken=;expires=${new Date(0).toUTCString()};path=/`;
  21. }
  22. };
  23.  
  24. export const accessTokenRequest = () => ({
  25. type: ActionTypes.ACCESS_TOKEN_REQUEST,
  26. });
  27.  
  28. export const accessTokenSuccess = (accessToken: string) => ({
  29. type: ActionTypes.ACCESS_TOKEN_SUCCESS,
  30. payload: accessToken,
  31. });
  32.  
  33. export const accessTokenFailure = () => ({
  34. type: ActionTypes.ACCESS_TOKEN_FAILURE,
  35. });
  36.  
  37. export const fetchAccessToken = ({
  38. email,
  39. password,
  40. }: {
  41. email: string,
  42. password: string,
  43. }) => async (dispatch: Dispatch<*>, getState: () => {}) => {
  44. try {
  45. const teamId = getTeamId(getState());
  46. if (!teamId) {
  47. // TODO: Add proper error handling here
  48. }
  49.  
  50. dispatch(accessTokenRequest());
  51. const { data } = await ClientRequest().post('/oauth2/login', {
  52. email,
  53. password,
  54. teamId,
  55. });
  56.  
  57. if (data.accessToken) {
  58. dispatch(accessTokenSuccess(data.accessToken));
  59.  
  60. if (isClient()) {
  61. document.cookie = `accessToken=${data.accessToken};expires=${new Date(
  62. data.expires
  63. ).toUTCString()};path=/`;
  64. }
  65. }
  66. } catch (err) {
  67. dispatch(accessTokenFailure());
  68. }
  69. };
Add Comment
Please, Sign In to add comment