text: import axios from "axios";
  import setAuthToken from "../utils/setAuthToken";
  import jwt_decode from "jwt-decode";
  
  // register user
  import { GET_ERRORS, SET_CURRENT_USER } from "./types";
  export const registerUser = (userData, history) => dispatch => {
    axios
      .post("/api/auth/register", userData)
      .then(res => history.push("/login"))
      .catch(err =>
        dispatch({
          type: GET_ERRORS,
          payload: err.response.data
        })
      );
  };
  
  // login - get user token
  export const loginUser = userData => dispatch => {
    axios
      .post("/api/auth/login", userData)
      .then(res => {
        // save token to local storage
        const { token } = res.data;
        // set token to local storage
        localStorage.setItem("jwtToken", token);
        // set token to auth header
        setAuthToken(token);
        // decode token
        const decoded = jwt_decode(token);
        // set current user
        dispatch(setCurrentUser(decoded));
      })
      .catch(err =>
        dispatch({
          type: GET_ERRORS,
          payload: err.response.data
        })
      );
  };
  
  // set logged in user
  export const setCurrentUser = decoded => {
    return {
      type: SET_CURRENT_USER,
      payload: decoded
    };
  };
  
  // log out user
  export const logoutUser = () => dispatch => {
    // remove token from local storage
    localStorage.removeItem("jwtToken");
    // remove auth header
    setAuthToken(false);
    // set current user to {}, this also should also set 'isAuthenticated' to false
    dispatch(setCurrentUser({}));
  };
  