private route go to login page + direct login in dev

This commit is contained in:
hamid zarghami
2026-06-13 10:53:44 +03:30
parent 0245e8d407
commit d573e8dc8d
5 changed files with 16 additions and 17 deletions
+1
View File
@@ -3,6 +3,7 @@ VITE_API_URL=https://dpage-api.danakcorp.com
VITE_TOKEN_NAME=dpage-editor-t VITE_TOKEN_NAME=dpage-editor-t
VITE_REFRESH_TOKEN_NAME=dpage-editor-refresh-t VITE_REFRESH_TOKEN_NAME=dpage-editor-refresh-t
VITE_INVOICE_URL=https://console.danakcorp.com/receipts/ VITE_INVOICE_URL=https://console.danakcorp.com/receipts/
VITE_LOGIN_URL=https://console.danakcorp.com/auth/login
VITE_DESIGN_TIER_1_MAX=10 VITE_DESIGN_TIER_1_MAX=10
VITE_DESIGN_PRICE_TIER_1=700000 VITE_DESIGN_PRICE_TIER_1=700000
VITE_DESIGN_TIER_2_MAX=30 VITE_DESIGN_TIER_2_MAX=30
+2 -1
View File
@@ -10,6 +10,7 @@ import { useCreateCatalog } from "@/pages/catalogue/hooks/useCatalogueData";
import { ArrowLeft, DocumentText } from "iconsax-react"; import { ArrowLeft, DocumentText } from "iconsax-react";
import { useState, type FC } from "react"; import { useState, type FC } from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import DirectLogin from "../auth/components/DirectLogin";
const CATALOG_SIZES = [ const CATALOG_SIZES = [
{ id: "a4", label: "A4" }, { id: "a4", label: "A4" },
@@ -51,7 +52,7 @@ const Home: FC = () => {
<div className="mt-4 w-full"> <div className="mt-4 w-full">
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between"> <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<h1 className="text-base font-light sm:text-lg">ساخت کاتالوگ به کمک ویرایشگر داناک</h1> <h1 className="text-base font-light sm:text-lg">ساخت کاتالوگ به کمک ویرایشگر داناک</h1>
{/* <DirectLogin /> */} {import.meta.env.DEV && <DirectLogin />}
</div> </div>
<div className="mt-8 rounded-2xl bg-white p-4 sm:mt-12 sm:rounded-3xl sm:p-6 lg:mt-20 lg:rounded-4xl lg:p-10"> <div className="mt-8 rounded-2xl bg-white p-4 sm:mt-12 sm:rounded-3xl sm:p-6 lg:mt-20 lg:rounded-4xl lg:p-10">
+4 -4
View File
@@ -44,7 +44,7 @@ const MainRouter = () => {
<Route <Route
path={Paths.home} path={Paths.home}
element={ element={
<PrivateRoute requireAuth={false}> <PrivateRoute requireAuth={true}>
<Home /> <Home />
</PrivateRoute> </PrivateRoute>
} }
@@ -52,7 +52,7 @@ const MainRouter = () => {
<Route <Route
path={Paths.editor + "/:id"} path={Paths.editor + "/:id"}
element={ element={
<PrivateRoute> <PrivateRoute requireAuth={true}>
<Editor /> <Editor />
</PrivateRoute> </PrivateRoute>
} }
@@ -87,7 +87,7 @@ const MainRouter = () => {
<Route <Route
path={Paths.designer.request} path={Paths.designer.request}
element={ element={
<PrivateRoute> <PrivateRoute requireAuth={true}>
<DesignerRequest /> <DesignerRequest />
</PrivateRoute> </PrivateRoute>
} }
@@ -95,7 +95,7 @@ const MainRouter = () => {
<Route <Route
path={Paths.designer.list} path={Paths.designer.list}
element={ element={
<PrivateRoute> <PrivateRoute requireAuth={true}>
<RequestDesignList /> <RequestDesignList />
</PrivateRoute> </PrivateRoute>
} }
+8 -12
View File
@@ -1,29 +1,29 @@
import { useAuth } from '@/context/AuthContext' import { useAuth } from '@/context/AuthContext'
import { Paths } from '@/config/Paths' import { type FC, type ReactNode, useEffect } from 'react'
import { type FC, type ReactNode } from 'react'
import { Navigate, useLocation } from 'react-router-dom'
type PrivateRouteProps = { type PrivateRouteProps = {
children: ReactNode children: ReactNode
/** When true (default), user must be logged in to access the route */ /** When true (default), user must be logged in to access the route */
requireAuth?: boolean requireAuth?: boolean
/** Redirect target when auth is required but user is not logged in */
redirectTo?: string
} }
const PrivateRoute: FC<PrivateRouteProps> = ({ const PrivateRoute: FC<PrivateRouteProps> = ({
children, children,
requireAuth = true, requireAuth = true,
redirectTo = Paths.home,
}) => { }) => {
const { isAuthenticated, isChecking } = useAuth() const { isAuthenticated, isChecking } = useAuth()
const location = useLocation()
useEffect(() => {
if (requireAuth && !isChecking && !isAuthenticated) {
window.location.href = import.meta.env.VITE_LOGIN_URL
}
}, [requireAuth, isChecking, isAuthenticated])
if (!requireAuth) { if (!requireAuth) {
return children return children
} }
if (isChecking) { if (isChecking || !isAuthenticated) {
return ( return (
<div className="flex flex-1 items-center justify-center"> <div className="flex flex-1 items-center justify-center">
<div className="text-gray-600">در حال بارگذاری...</div> <div className="text-gray-600">در حال بارگذاری...</div>
@@ -31,10 +31,6 @@ const PrivateRoute: FC<PrivateRouteProps> = ({
) )
} }
if (!isAuthenticated) {
return <Navigate to={redirectTo} replace state={{ from: location }} />
}
return children return children
} }
+1
View File
@@ -5,6 +5,7 @@ interface ImportMetaEnv {
readonly VITE_TOKEN_NAME: string; readonly VITE_TOKEN_NAME: string;
readonly VITE_REFRESH_TOKEN_NAME: string; readonly VITE_REFRESH_TOKEN_NAME: string;
readonly VITE_INVOICE_URL: string; readonly VITE_INVOICE_URL: string;
readonly VITE_LOGIN_URL: string;
readonly VITE_DESIGN_TIER_1_MAX: string; readonly VITE_DESIGN_TIER_1_MAX: string;
readonly VITE_DESIGN_PRICE_TIER_1: string; readonly VITE_DESIGN_PRICE_TIER_1: string;
readonly VITE_DESIGN_TIER_2_MAX: string; readonly VITE_DESIGN_TIER_2_MAX: string;