Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3400 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import {
  axios,
  isAndroidDevice,
  isIOSDevice,
  loginUserToNative,
  logoutUserToNative
} from '@utils'
import { actionsTypes } from './auth.types'

export const setCrendentials = (crendentials) => ({
  type: actionsTypes.SET_CREDENTIALS,
  payload: crendentials
})

export const startLoading = () => ({
  type: actionsTypes.START_LOADING
})

export const stopLoading = () => ({
  type: actionsTypes.STOP_LOADING
})

export const logout = () => {
  return (dispatch) => {
    dispatch(removeAuth())
  }
}

export const asyncLogout = () => {
  return async (dispatch) => {
    const response = await axios.get('/signout')

    const { success, data } = response.data
    const isAndroid = isAndroidDevice()
    const isIOS = isIOSDevice()

    if (!success) {
      throw new Error('Error al cerrar sesión')
    }
    if (isAndroid || isIOS) logoutUserToNative(data.uuid)

    window.localStorage.removeItem('jwt')
    dispatch(logout())

    return data
  }
}

export const asyncLogin = (formData) => {
  return async (dispatch) => {
    const response = await axios.post('/signin', formData)

    const { success, data } = response.data
    const isAndroid = isAndroidDevice()
    const isIOS = isIOSDevice()

    if (!success) {
      throw new Error('Error al iniciar sesión')
    }

    if (isAndroid || isIOS) loginUserToNative(data.uuid)

    dispatch(login())

    const redirectPath = new URL(data.redirect).pathname
    return { ...data, redirect: redirectPath }
  }
}

export const login = () => ({
  type: actionsTypes.LOGIN
})

export const removeAuth = () => ({
  type: actionsTypes.LOGOUT
})