Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // @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());
- }
- };
Add Comment
Please, Sign In to add comment