Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 66 | Rev 72 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
32 stevensc 1
import { axios } from "../../utils";
2
import { actionsTypes } from "./auth.types";
5 stevensc 3
 
71 stevensc 4
export const validateAuth = () => {
5 stevensc 5
  return (dispatch) => {
71 stevensc 6
    dispatch(startLoading());
7
 
5 stevensc 8
    axios
32 stevensc 9
      .get("/signin", { headers: { "Content-Type": "application/json" } })
38 stevensc 10
      .then((response) => {
63 stevensc 11
        const { success, data } = response.data;
38 stevensc 12
 
63 stevensc 13
        if (!success) {
14
          throw new Error(data);
38 stevensc 15
        }
16
 
63 stevensc 17
        if (data.jwt) {
18
          window.localStorage.setItem("jwt", data.jwt);
19
        }
20
 
71 stevensc 21
        if (data.is_logged_in) {
22
          dispatch(login());
23
        } else {
24
          dispatch(startLogout());
25
        }
26
 
63 stevensc 27
        dispatch(setPermissions(data));
5 stevensc 28
      })
29
      .catch((err) => {
32 stevensc 30
        console.log(err);
31
        throw new Error(err);
5 stevensc 32
      })
32 stevensc 33
      .finally(() => dispatch(stopLoading()));
34
  };
35
};
5 stevensc 36
 
37
const setPermissions = (permissions) => ({
38
  type: actionsTypes.SET_PERMISSIONS,
39
  payload: permissions,
32 stevensc 40
});
5 stevensc 41
 
42
export const startLoading = () => ({
43
  type: actionsTypes.START_LOADING,
32 stevensc 44
});
5 stevensc 45
 
46
export const stopLoading = () => ({
47
  type: actionsTypes.STOP_LOADING,
32 stevensc 48
});
5 stevensc 49
 
66 stevensc 50
export const login = () => ({
51
  type: actionsTypes.LOGIN,
52
});
5 stevensc 53
 
71 stevensc 54
const startLogout = () => {
55
  return (dispatch) => {
56
    window.localStorage.removeItem("jwt");
57
    dispatch(logout());
58
  };
59
};
60
 
5 stevensc 61
export const logout = () => ({
62
  type: actionsTypes.LOGOUT,
32 stevensc 63
});