viewer do not need login

This commit is contained in:
hamid zarghami
2026-05-23 16:21:19 +03:30
parent 517c77af77
commit 612deba297
+48 -40
View File
@@ -1,70 +1,78 @@
import { useEffect, useState, type FC } from 'react' import FaJson from "@/langs/fa.json";
import './assets/fonts/irancell/style.css' import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter } from 'react-router-dom' import i18next from "i18next";
import MainRouter from './router/MainRouter' import { useEffect, useState, type FC } from "react";
import i18next from 'i18next' import { I18nextProvider } from "react-i18next";
import FaJson from '@/langs/fa.json' import "react-multi-date-picker/styles/layouts/mobile.css";
import { I18nextProvider } from 'react-i18next' import { BrowserRouter } from "react-router-dom";
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import "./assets/fonts/irancell/style.css";
import ToastContainer from './components/Toast' import ToastContainer from "./components/Toast";
import { getToken, setRefreshToken, setToken } from './config/func' import { getToken, setRefreshToken, setToken } from "./config/func";
import "react-multi-date-picker/styles/layouts/mobile.css" import { Paths } from "./config/Paths";
import MainRouter from "./router/MainRouter";
const isViewerPathname = (pathname: string) => {
const viewerPathPrefix = `${Paths.viewer}/`;
return (
pathname.startsWith(viewerPathPrefix) &&
pathname.length > viewerPathPrefix.length
);
};
i18next.init({ i18next.init({
interpolation: { escapeValue: false }, interpolation: { escapeValue: false },
lng: 'fa', lng: "fa",
resources: { resources: {
fa: { fa: {
global: FaJson global: FaJson,
} },
} },
}) });
const queryClient = new QueryClient() const queryClient = new QueryClient();
const App: FC = () => { const App: FC = () => {
const [isLogin, setIsLogin] = useState<"checking" | "isLogin" | "isNotLogin">(
const [isLogin, setIsLogin] = useState<'checking' | 'isLogin' | 'isNotLogin'>('checking') "checking",
);
useEffect(() => { useEffect(() => {
// استخراج توکن‌ها از URL در صورت وجود // استخراج توکن‌ها از URL در صورت وجود
const urlParams = new URLSearchParams(window.location.search) const urlParams = new URLSearchParams(window.location.search);
const tokenFromUrl = urlParams.get('token') const tokenFromUrl = urlParams.get("token");
const refreshTokenFromUrl = urlParams.get('refreshToken') const refreshTokenFromUrl = urlParams.get("refreshToken");
if (tokenFromUrl && refreshTokenFromUrl) { if (tokenFromUrl && refreshTokenFromUrl) {
// ذخیره توکن‌ها (جایگزین کردن توکن‌های قبلی اگر وجود داشته باشند) // ذخیره توکن‌ها (جایگزین کردن توکن‌های قبلی اگر وجود داشته باشند)
setToken(tokenFromUrl) setToken(tokenFromUrl);
setRefreshToken(refreshTokenFromUrl) setRefreshToken(refreshTokenFromUrl);
// حذف پارامترها از URL // حذف پارامترها از URL
const newUrl = window.location.pathname const newUrl = window.location.pathname;
window.history.replaceState({}, '', newUrl) window.history.replaceState({}, "", newUrl);
// تنظیم وضعیت لاگین و هدایت به صفحه اصلی // تنظیم وضعیت لاگین و هدایت به صفحه اصلی
setIsLogin('isLogin') setIsLogin("isLogin");
// if (window.location.pathname === Paths.auth.login) { // if (window.location.pathname === Paths.auth.login) {
// window.location.href = Paths.home // window.location.href = Paths.home
// } // }
return return;
} }
const token = getToken() const token = getToken();
if (token) { if (token) {
setIsLogin('isLogin') setIsLogin("isLogin");
} else if (isViewerPathname(window.location.pathname)) {
setIsLogin("isLogin");
} else { } else {
setIsLogin('isNotLogin') setIsLogin("isNotLogin");
if (window.location.href.split('auth').length === 1) { if (window.location.href.split("auth").length === 1) {
// window.location.href = Paths.auth.login // window.location.href = Paths.auth.login
} }
} }
}, []);
}, []) console.log("isLogin", isLogin);
console.log('isLogin', isLogin);
return ( return (
<BrowserRouter> <BrowserRouter>
@@ -75,7 +83,7 @@ const App: FC = () => {
</QueryClientProvider> </QueryClientProvider>
</I18nextProvider> </I18nextProvider>
</BrowserRouter> </BrowserRouter>
) );
} };
export default App export default App;