Ivankooo1

Route Guard

Dec 7th, 2021
624
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.  
  8. return <Outlet />
  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.  
  22.   <Route path='/update/:id' element={<UpdateRecord/>}/>
  23.   <Route path='/beer/:id' element={<BeerDetails />}/>
  24.  
  25. </Routes>
RAW Paste Data