Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3407 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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