Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3736 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import { axios, isAndroidDevice, isIOSDevice, loginUserToNative, logoutUserToNative } from '@utils';
2
import { actionsTypes } from './auth.types';
3
 
3740 stevensc 4
const ENV = import.meta.env.VITE_ENVIROMENT;
5
 
3719 stevensc 6
export const setCrendentials = (crendentials) => ({
7
  type: actionsTypes.SET_CREDENTIALS,
8
  payload: crendentials
9
});
10
 
11
export const startLoading = () => ({
12
  type: actionsTypes.START_LOADING
13
});
14
 
15
export const stopLoading = () => ({
16
  type: actionsTypes.STOP_LOADING
17
});
18
 
19
export const asyncLogout = () => {
20
  return async (dispatch) => {
21
    const response = await axios.get('/signout');
22
 
23
    const { success, data } = response.data;
24
    const isAndroid = isAndroidDevice();
25
    const isIOS = isIOSDevice();
26
 
27
    if (!success) {
28
      throw new Error('Error al cerrar sesión');
29
    }
30
    if (isAndroid || isIOS) logoutUserToNative(data.uuid);
31
 
32
    window.localStorage.removeItem('jwt');
33
    dispatch(logout());
34
 
35
    return data;
36
  };
37
};
38
 
39
export const asyncLogin = (formData) => {
40
  return async (dispatch) => {
3740 stevensc 41
    const url = ENV === 'production' ? '/signin' : '/signin/debug';
42
    const response = await axios.post(url, formData);
3719 stevensc 43
 
44
    const { success, data } = response.data;
45
    const isAndroid = isAndroidDevice();
46
    const isIOS = isIOSDevice();
47
 
48
    if (!success) {
49
      throw new Error('Error al iniciar sesión');
50
    }
51
 
52
    if (isAndroid || isIOS) loginUserToNative(data.uuid);
53
 
54
    dispatch(login());
55
 
56
    const redirectPath = new URL(data.redirect).pathname;
57
    return { ...data, redirect: redirectPath };
58
  };
59
};
60
 
61
export const login = () => ({
62
  type: actionsTypes.LOGIN
63
});
64
 
65
export const logout = () => ({
66
  type: actionsTypes.LOGOUT
67
});