feat(udt-001): frontend auth UI (Zustand store, TanStack Query, LoginPage, router)
This commit is contained in:
50
src/web/src/router.tsx
Normal file
50
src/web/src/router.tsx
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Navigate, Route, Routes } from 'react-router-dom'
|
||||
import { useAuthStore } from './stores/authStore'
|
||||
import { LoginPage } from './features/auth/pages/LoginPage'
|
||||
import { HomePage } from './pages/HomePage'
|
||||
import { PublicLayout } from './layouts/PublicLayout'
|
||||
import { ProtectedLayout } from './layouts/ProtectedLayout'
|
||||
|
||||
function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
||||
const user = useAuthStore((s) => s.user)
|
||||
if (!user) {
|
||||
return <Navigate to="/login" replace />
|
||||
}
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
function PublicRoute({ children }: { children: React.ReactNode }) {
|
||||
const user = useAuthStore((s) => s.user)
|
||||
if (user) {
|
||||
return <Navigate to="/" replace />
|
||||
}
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
export function AppRoutes() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route
|
||||
path="/login"
|
||||
element={
|
||||
<PublicRoute>
|
||||
<PublicLayout>
|
||||
<LoginPage />
|
||||
</PublicLayout>
|
||||
</PublicRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/"
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<ProtectedLayout>
|
||||
<HomePage />
|
||||
</ProtectedLayout>
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Routes>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user