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_REFRESH_TOKEN_NAME=dpage-editor-refresh-t
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_PRICE_TIER_1=700000
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 { useState, type FC } from "react";
import { useNavigate } from "react-router-dom";
import DirectLogin from "../auth/components/DirectLogin";
const CATALOG_SIZES = [
{ id: "a4", label: "A4" },
@@ -51,7 +52,7 @@ const Home: FC = () => {
<div className="mt-4 w-full">
<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>
{/* <DirectLogin /> */}
{import.meta.env.DEV && <DirectLogin />}
</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">
+4 -4
View File
@@ -44,7 +44,7 @@ const MainRouter = () => {
<Route
path={Paths.home}
element={
<PrivateRoute requireAuth={false}>
<PrivateRoute requireAuth={true}>
<Home />
</PrivateRoute>
}
@@ -52,7 +52,7 @@ const MainRouter = () => {
<Route
path={Paths.editor + "/:id"}
element={
<PrivateRoute>
<PrivateRoute requireAuth={true}>
<Editor />
</PrivateRoute>
}
@@ -87,7 +87,7 @@ const MainRouter = () => {
<Route
path={Paths.designer.request}
element={
<PrivateRoute>
<PrivateRoute requireAuth={true}>
<DesignerRequest />
</PrivateRoute>
}
@@ -95,7 +95,7 @@ const MainRouter = () => {
<Route
path={Paths.designer.list}
element={
<PrivateRoute>
<PrivateRoute requireAuth={true}>
<RequestDesignList />
</PrivateRoute>
}
+8 -12
View File
@@ -1,29 +1,29 @@
import { useAuth } from '@/context/AuthContext'
import { Paths } from '@/config/Paths'
import { type FC, type ReactNode } from 'react'
import { Navigate, useLocation } from 'react-router-dom'
import { type FC, type ReactNode, useEffect } from 'react'
type PrivateRouteProps = {
children: ReactNode
/** When true (default), user must be logged in to access the route */
requireAuth?: boolean
/** Redirect target when auth is required but user is not logged in */
redirectTo?: string
}
const PrivateRoute: FC<PrivateRouteProps> = ({
children,
requireAuth = true,
redirectTo = Paths.home,
}) => {
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) {
return children
}
if (isChecking) {
if (isChecking || !isAuthenticated) {
return (
<div className="flex flex-1 items-center justify-center">
<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
}
+1
View File
@@ -5,6 +5,7 @@ interface ImportMetaEnv {
readonly VITE_TOKEN_NAME: string;
readonly VITE_REFRESH_TOKEN_NAME: string;
readonly VITE_INVOICE_URL: string;
readonly VITE_LOGIN_URL: string;
readonly VITE_DESIGN_TIER_1_MAX: string;
readonly VITE_DESIGN_PRICE_TIER_1: string;
readonly VITE_DESIGN_TIER_2_MAX: string;