Proyectos de Subversion LeadersLinked - SPA

Rev

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