Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3395 | Rev 3397 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3395 Rev 3396
Línea 3... Línea 3...
3
 
3
 
4
import { axios } from './utils'
4
import { axios } from './utils'
5
import { useFetch } from '@hooks'
5
import { useFetch } from '@hooks'
6
import { labelsAdapter } from './utils/labels'
6
import { labelsAdapter } from './utils/labels'
7
import { setIntlLabels } from './redux/intl/intl.action'
7
import { setIntlLabels } from './redux/intl/intl.action'
Línea 8... Línea 8...
8
import { logout, setPermissions } from './redux/auth/auth.actions'
8
import { setCrendentials } from './redux/auth/auth.actions'
9
 
-
 
10
import AppRoutes from './routes/routes'
9
 
-
 
10
import AppRoutes from './routes/routes'
Línea 11... Línea 11...
11
import Spinner from './components/UI/Spinner'
11
import ErrorPage from './pages/error/error-page'
Línea 12... Línea 12...
12
import ErrorPage from './pages/error/error-page'
12
import Spinner from './components/UI/Spinner'
13
 
13
 
14
import './styles/globals.scss'
14
import './styles/globals.scss'
15
 
15
 
16
export default function App() {
-
 
17
  const [loading, setLoading] = useState(true)
16
export default function App() {
Línea -... Línea 17...
-
 
17
  const [loading, setLoading] = useState(true)
-
 
18
  const [credentialsError, setCredentialsError] = useState(false)
18
  const [credentialsError, setCredentialsError] = useState(false)
19
  const { data: labels } = useFetch('/language')
19
  const { data: labels } = useFetch('/language')
20
  const dispatch = useDispatch()
20
  const { isAuth, aes } = useSelector(({ auth }) => auth)
21
 
21
  const dispatch = useDispatch()
22
  const is_logged_in = window.localStorage.getItem('is_logged_in')
22
 
23
 
23
  const getCredentials = async () => {
24
  const getCredentials = async () => {
Línea 24... Línea 25...
24
    const response = await axios.get('/signin')
25
    const response = await axios.get('/signin')
25
    const { data, success } = response.data
26
    const { data, success } = response.data
26
    if (!success) throw new Error(data)
-
 
27
    return data
-
 
28
  }
-
 
29
 
-
 
30
  const getUserCredentials = async () => {
-
 
Línea 31... Línea -...
31
    setLoading(true)
-
 
32
    getCredentials()
-
 
33
      .then((credentials) => {
27
    if (!success) throw new Error('Error al obtener las credenciales')
34
        const permissions = structuredClone(credentials)
28
    return data
35
        permissions.isAuth = credentials.is_logged_in
29
  }
36
        permissions.aes = credentials.aes ? credentials.aes : aes
30
 
37
 
31
  useEffect(() => {
38
        window.localStorage.setItem('jwt', credentials.jwt)
32
    setLoading(true)
39
        dispatch(setPermissions(permissions))
33
 
40
      })
34
    getCredentials()
-
 
35
      .then((response) => {
-
 
36
        const crendentials = {
41
      .catch((error) => {
37
          ...response,
-
 
38
          aes: is_logged_in ? window.localStorage.getItem('aes') : response.aes,
42
        if (error.message.includes('sesión')) {
39
          jwt: is_logged_in ? window.localStorage.getItem('jwt') : response.jwt,
43
          dispatch(logout())
-
 
44
          getUserCredentials()
-
 
45
        } else {
-
 
46
          setCredentialsError(true)
-
 
47
        }
40
          aes: is_logged_in ? true : false
Línea 48... Línea 41...
48
      })
41
        }
49
      .finally(() => setLoading(false))
42
 
50
  }
43
        dispatch(setCrendentials(credentials))