text: import axios from "axios";
  import { returnErrors } from "./messages";
  
  import {
    USER_LOADED,
    USER_LOADING,
    AUTH_ERROR,
    LOGIN_SUCCESS,
    LOGIN_FAIL,
    LOGOUT_SUCCESS,
    REGISTER_SUCCESS,
    REGISTER_FAIL,
  } from "./types";
  
  // CHECK TOKEN & LOAD USER
  export const loadUser = () => (dispatch, getState) => {
    // User Loading
    dispatch({ type: USER_LOADING });
  
    axios
      .get("/api/auth/user/", tokenConfig(getState))
      .then((res) => {
        dispatch({
          type: USER_LOADED,
          payload: res.data,
        });
      })
      .catch((err) => {
        dispatch(returnErrors(err.response.data, err.response.status));
        dispatch({
          type: AUTH_ERROR,
        });
      });
  };
  
  // LOGIN USER
  export const login = (email, password, history) => (dispatch) => {
    // Headers
    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };
  
    // Request Body
    const body = JSON.stringify({ email, password });
  
    axios
      .post("/api/auth/login/", body, config)
      .then((res) => {
        dispatch(
          {
            type: LOGIN_SUCCESS,
            payload: res.data,
          },
          history.push("/")
        );
      })
      .catch((err) => {
        dispatch(returnErrors(err.response.data, err.response.status));
        dispatch({
          type: LOGIN_FAIL,
        });
      });
  };
  
  // REGISTER USER
  export const register =
    (email, password, fname, lname, history) => (dispatch) => {
      let full_name = `${fname} ${lname}`;
      // Headers
      const config = {
        headers: {
          "Content-Type": "application/json",
        },
      };
  
      // Request Body
      const body = JSON.stringify({ email, full_name, password });
  
      axios
        .post("/api/auth/register/", body, config)
        .then((res) => {
          dispatch(
            {
              type: REGISTER_SUCCESS,
              payload: res.data,
            },
            history.push("/login")
          );
        })
        .catch((err) => {
          dispatch(returnErrors(err.response.data, err.response.status));
          dispatch({
            type: REGISTER_FAIL,
          });
        });
    };
  
  // LOGOUT USER
  export const logout = () => (dispatch, getState) => {
    axios
      .post("/api/auth/logout/", null, tokenConfig(getState))
      .then((res) => {
        dispatch({
          type: LOGOUT_SUCCESS,
        });
      })
      .catch((err) => {
        if (err.response.status === 401) {
          dispatch({
            type: AUTH_ERROR,
          });
        }
        dispatch(returnErrors(err.response.data, err.response.status));
      });
  };
  
  // Setup config with token - helper function
  export const tokenConfig = (getState) => {
    // Get token from state
    const token = getState().auth.token;
  
    // Headers
    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };
  
    // If token, add to headers config
    if (token) {
      config.headers["Authorization"] = `Token ${token}`;
    }
  
    return config;
  };
  