EntropyStarRover

route guard

Dec 3rd, 2021 (edited)
836
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function RequireAuth() {  
  2.     let location = useLocation();
  3.  
  4.     if (!user.username) {
  5.       return <Navigate to="/login" state={{ from: location }} />;
  6.     }  
  7.     return <Outlet />;
  8.   }
  9.  
  10.  
  11.           <Routes>
  12.             <Route path="/*" element={<Home/>} />
  13.             <Route path="/login" element={<Login/>} />            
  14.             <Route path="/register" element={<Register/>} />
  15.  
  16.             <Route element={<RequireAuth />}>
  17.             <Route path="/profile" element={<Profile />} />
  18.             <Route path="/create" element={<CreateRecord/>} />
  19.             </Route>          
  20.            
  21.             <Route path="/update/:beerId" element={<UpdateRecord/>} />
  22.             <Route path="/beers/:beerId" element={<BeerDetails/>} />
  23.           </Routes>
  24.            
  25.  
RAW Paste Data