{"size": 1448, "ext": "js", "max_stars_repo_path": "src/actions/authActions.js", "max_stars_repo_name": "samorahno/Politico-Client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/actions/authActions.js", "max_issues_repo_name": "samorahno/Politico-Client", "max_issues_repo_issues_event_min_datetime": "2021-03-09T04:25:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T11:03:11.000Z", "max_forks_repo_path": "src/actions/authActions.js", "max_forks_repo_name": "samorahno/Politico-Client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 27.320754717, "max_line_length": 103, "alphanum_fraction": 0.6256906077}
import { AUTH_LOGIN, AUTH_SIGNUP, AUTH_FAILURE, CLEAR_AUTH_ERROR, AUTH_LOGOUT } from '../action-types';
import axios from '../config/axiosConfig';

export const loginSucess = (user, message, token) => ({
    type: AUTH_LOGIN,
    payload: { user, message, token }
});

export const authFailure = (payload) => ({
    type: AUTH_FAILURE,
    payload
});

export const signupSuccess = (payload = {}, message = '') => ({
    type: AUTH_SIGNUP,
    payload,
    message
});

export const logoutSuccess = () => ({
    type: AUTH_LOGOUT
});

export const clearAuthError = () => ({
    type: CLEAR_AUTH_ERROR
});

export const loginUser = (data) => async (dispatch) => {
    try {
        const response = await axios.post('/auth/login', data);
        const { user, message, token } = response.data.data[0];
        return dispatch(loginSucess(user, message, token));
    } catch (error) {
        return dispatch(authFailure(error.response.data));
    }
};

export const signupUser = (data) => async (dispatch) => {
    try {
        const response = await axios.post('/auth/signup', data);
        const { message } = response.data;
        const { user } = response.data.data[0];
        return dispatch(signupSuccess(user, message));
    } catch (error) {
        return dispatch(authFailure(error.response.data));
    }
};

export const logoutUser = () => async (dispatch) => {
    dispatch(logoutSuccess());
    localStorage.removeItem('state');
};
