Commit a0584e8a by asranov0003

fix: router and protected route logic

parent 0c26337e
...@@ -12,13 +12,17 @@ const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ ...@@ -12,13 +12,17 @@ const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
redirectTo, redirectTo,
}) => { }) => {
const isAuthenticated = !!localStorage.getItem(`token-${TG_USER_ID}`); const isAuthenticated = !!localStorage.getItem(`token-${TG_USER_ID}`);
const pincode = !!sessionStorage.getItem(`pincode-${TG_USER_ID}`); const hasPincode = !!sessionStorage.getItem(`pincode-${TG_USER_ID}`);
if (isAuthenticated && !pincode) { if (!isAuthenticated) {
return <Navigate to={redirectTo} />;
}
if (!hasPincode) {
return <Navigate to="/pincode" />; return <Navigate to="/pincode" />;
} }
return isAuthenticated ? element : <Navigate to={redirectTo} />; return element;
}; };
export default ProtectedRoute; export default ProtectedRoute;
...@@ -22,26 +22,33 @@ const Router: React.FC = () => { ...@@ -22,26 +22,33 @@ const Router: React.FC = () => {
const router = createBrowserRouter([ const router = createBrowserRouter([
{ {
path: "/", path: "/auth",
element: isAuthenticated && <Navigate to={"/home"} />,
children: [ children: [
{ {
path: "/auth/login", index: true,
element: <Login />, element: isAuthenticated ? (
<Navigate to="/home" />
) : (
<Navigate to="/auth/login" />
),
},
{
path: "login",
element: isAuthenticated ? <Navigate to="/home" /> : <Login />,
}, },
{ {
path: "/auth/register", path: "register",
element: <Register />, element: isAuthenticated ? <Navigate to="/home" /> : <Register />,
}, },
{ {
path: "/auth/recover", path: "recover",
element: <Recover />, element: isAuthenticated ? <Navigate to="/home" /> : <Recover />,
}, },
], ],
}, },
{ {
path: "/", path: "/",
element: <ProtectedRoute element={<Layout />} redirectTo="/" />, element: <ProtectedRoute element={<Layout />} redirectTo="/auth/login" />,
children: [ children: [
{ {
path: "home", path: "home",
...@@ -63,7 +70,7 @@ const Router: React.FC = () => { ...@@ -63,7 +70,7 @@ const Router: React.FC = () => {
}, },
{ {
path: "/pincode", path: "/pincode",
element: <Pincode />, element: isAuthenticated ? <Pincode /> : <Navigate to="/auth/login" />,
}, },
{ {
path: "*", path: "*",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment