File size: 855 Bytes
ee1fc35
c5243fe
f1b8b13
8830402
 
ee1fc35
c5243fe
ee1fc35
bf65a6c
 
c5243fe
 
 
 
 
 
ee1fc35
c5243fe
 
 
bf65a6c
 
c5243fe
 
 
bf65a6c
8830402
c5243fe
 
 
 
 
bf65a6c
c5243fe
 
 
 
bf65a6c
c5243fe
 
8830402
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, { useState, useEffect, useContext } from 'react'
import { Outlet, Navigate } from "react-router-dom";
import { UserContext } from './contexts/UserProvider';

function PrivateRoutes() {

  const [isAuth, setIsAuth] = useState(undefined);
  const { user, setUser } = useContext(UserContext);


  const getAuthStatus = async () => {

    const res = await fetch(process.env.REACT_APP_BACKEND_URL + "/session", {
      credentials: "include"
    })

    if (res.ok) { setUser(await res.json()) };
    return res.ok;
  }

  useEffect(() => {
    getAuthStatus().then((status) => {
      console.log(status);
      setIsAuth(status);
    })
  }, [])

  if (isAuth === undefined) {
    return <p>
      Loading ...
    </p>
  }
  else {

    return (
      isAuth ? <Outlet /> : <Navigate to="/login" />
    );

  }

}

export default PrivateRoutes