auth provider + private route
This commit is contained in:
+10
-59
@@ -1,24 +1,15 @@
|
||||
import FaJson from "@/langs/fa.json";
|
||||
import { AuthProvider } from "@/context/AuthContext";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import i18next from "i18next";
|
||||
import { useEffect, useState, type FC } from "react";
|
||||
import { type FC } from "react";
|
||||
import { I18nextProvider } from "react-i18next";
|
||||
import "react-multi-date-picker/styles/layouts/mobile.css";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
import "./assets/fonts/irancell/style.css";
|
||||
import ToastContainer from "./components/Toast";
|
||||
import { getToken, setRefreshToken, setToken } from "./config/func";
|
||||
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({
|
||||
interpolation: { escapeValue: false },
|
||||
lng: "fa",
|
||||
@@ -32,56 +23,16 @@ i18next.init({
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
const App: FC = () => {
|
||||
const [isLogin, setIsLogin] = useState<"checking" | "isLogin" | "isNotLogin">(
|
||||
"checking",
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
// استخراج توکنها از URL در صورت وجود
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const tokenFromUrl = urlParams.get("token");
|
||||
const refreshTokenFromUrl = urlParams.get("refreshToken");
|
||||
|
||||
if (tokenFromUrl && refreshTokenFromUrl) {
|
||||
// ذخیره توکنها (جایگزین کردن توکنهای قبلی اگر وجود داشته باشند)
|
||||
setToken(tokenFromUrl);
|
||||
setRefreshToken(refreshTokenFromUrl);
|
||||
|
||||
// حذف پارامترها از URL
|
||||
const newUrl = window.location.pathname;
|
||||
window.history.replaceState({}, "", newUrl);
|
||||
|
||||
// تنظیم وضعیت لاگین و هدایت به صفحه اصلی
|
||||
setIsLogin("isLogin");
|
||||
// if (window.location.pathname === Paths.auth.login) {
|
||||
// window.location.href = Paths.home
|
||||
// }
|
||||
return;
|
||||
}
|
||||
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
setIsLogin("isLogin");
|
||||
} else if (isViewerPathname(window.location.pathname)) {
|
||||
setIsLogin("isLogin");
|
||||
} else {
|
||||
setIsLogin("isNotLogin");
|
||||
if (window.location.href.split("auth").length === 1) {
|
||||
// window.location.href = Paths.auth.login
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
console.log("isLogin", isLogin);
|
||||
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<I18nextProvider i18n={i18next}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MainRouter />
|
||||
<ToastContainer />
|
||||
</QueryClientProvider>
|
||||
</I18nextProvider>
|
||||
<AuthProvider>
|
||||
<I18nextProvider i18n={i18next}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MainRouter />
|
||||
<ToastContainer />
|
||||
</QueryClientProvider>
|
||||
</I18nextProvider>
|
||||
</AuthProvider>
|
||||
</BrowserRouter>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user