Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 65 | Rev 71 | 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
 
63 stevensc 4
const getPermissions = () => {
5 stevensc 5
  return (dispatch) => {
6
    axios
32 stevensc 7
      .get("/signin", { headers: { "Content-Type": "application/json" } })
38 stevensc 8
      .then((response) => {
63 stevensc 9
        const { success, data } = response.data;
38 stevensc 10
 
63 stevensc 11
        if (!success) {
12
          throw new Error(data);
38 stevensc 13
        }
14
 
63 stevensc 15
        if (data.jwt) {
16
          window.localStorage.setItem("jwt", data.jwt);
17
        }
18
 
19
        dispatch(setPermissions(data));
5 stevensc 20
      })
21
      .catch((err) => {
32 stevensc 22
        console.log(err);
23
        throw new Error(err);
5 stevensc 24
      })
32 stevensc 25
      .finally(() => dispatch(stopLoading()));
26
  };
27
};
5 stevensc 28
 
63 stevensc 29
export const validateAuth = () => {
30
  return (dispatch) => {
66 stevensc 31
    dispatch(startLoading());
32
 
63 stevensc 33
    const token = window.localStorage.getItem("jwt");
34
 
35
    if (token) {
65 stevensc 36
      dispatch(login());
66 stevensc 37
      dispatch(stopLoading());
38
    } else {
39
      dispatch(getPermissions());
63 stevensc 40
    }
41
  };
42
};
43
 
5 stevensc 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
 
61
export const logout = () => ({
62
  type: actionsTypes.LOGOUT,
32 stevensc 63
});