Implemented private routes
This commit is contained in:
parent
4112fd795a
commit
94ab5c3d8f
54
src/App.tsx
54
src/App.tsx
@ -4,38 +4,48 @@ import { BrowserRouter as Router, Switch, Route, Redirect } from "react-router-d
|
|||||||
import NotFound from "./components/NotFound"
|
import NotFound from "./components/NotFound"
|
||||||
import Login from "./components/Login"
|
import Login from "./components/Login"
|
||||||
import { useIsAuthQuery } from "./generated/graphql"
|
import { useIsAuthQuery } from "./generated/graphql"
|
||||||
|
import { useEffect } from "react"
|
||||||
|
|
||||||
const App: React.FC = () => {
|
const App: React.FC = () => {
|
||||||
const {data: isAuth, loading } = useIsAuthQuery()
|
const { data } = useIsAuthQuery()
|
||||||
const [loginSuccessful,setLoginSuccessful] = useState(false)
|
const [isAuth,setIsAuth] = useState(false)
|
||||||
|
const [ready,setReady] = useState(false)
|
||||||
|
|
||||||
const loginRoutes = ()=>{
|
useEffect(()=>{
|
||||||
if (loginSuccessful || isAuth?.authorized){
|
if (data){
|
||||||
// Routes available when logged in
|
setIsAuth(data.authorized)
|
||||||
return <>
|
setReady(true)
|
||||||
<Route path="/f/" component={FileBrowser}/>
|
|
||||||
<Redirect from="/" exact to="/f/" />
|
|
||||||
</>
|
|
||||||
}else{
|
|
||||||
// Routes available when NOT logged in
|
|
||||||
return <>
|
|
||||||
<Route path="/login" exact >
|
|
||||||
<Login onLogin={()=>{
|
|
||||||
setLoginSuccessful(true)
|
|
||||||
}} />
|
|
||||||
</Route>
|
|
||||||
<Redirect from="*" to="/login" />
|
|
||||||
</>
|
|
||||||
}
|
}
|
||||||
}
|
},[data])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="dark:text-gray-300">
|
<div className="dark:text-gray-300">
|
||||||
{
|
{
|
||||||
!loading &&
|
ready&&
|
||||||
<Router>
|
<Router>
|
||||||
<Switch>
|
<Switch>
|
||||||
{loginRoutes()}
|
{/* Private only routes */}
|
||||||
|
{
|
||||||
|
isAuth && [
|
||||||
|
<Route path="/f/" component={FileBrowser} key={null} />,
|
||||||
|
<Redirect from="/" exact to="/f/" key={null} />,
|
||||||
|
<Redirect from="/login" exact to="/f/" key={null} />,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
{/* Public only routes */}
|
||||||
|
{
|
||||||
|
!isAuth && [
|
||||||
|
<Route path="/login" exact key={1} >
|
||||||
|
<Login onLogin={()=>{
|
||||||
|
setIsAuth(true)
|
||||||
|
}} />
|
||||||
|
</Route>,
|
||||||
|
<Redirect from="/f/" to="/login" key={2} />
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
{/* Public & private routes */}
|
||||||
<Route path="*" component={NotFound} />
|
<Route path="*" component={NotFound} />
|
||||||
</Switch>
|
</Switch>
|
||||||
</Router>
|
</Router>
|
||||||
|
Loading…
Reference in New Issue
Block a user