diff --git a/src/lib/api-fetch.ts b/src/lib/api-fetch.ts index 2bbf365..847b9b5 100644 --- a/src/lib/api-fetch.ts +++ b/src/lib/api-fetch.ts @@ -1,5 +1,7 @@ import { FetchError, ofetch } from "ofetch" +import { getRedirectToLoginUrl } from "./utils" + export const apiFetch = ofetch.create({ credentials: "include", retry: false, @@ -27,18 +29,12 @@ export const apiFetch = ofetch.create({ }, }) -function redirectToLogin() { +export function redirectToLogin() { if (window.location.pathname === "/login") { return } - const requestUrl = new URL(window.location.href) - const redirectTo = requestUrl.pathname + requestUrl.search - const loginParams = new URLSearchParams(requestUrl.search) - if (!loginParams.has("redirectTo")) { - loginParams.append("redirectTo", redirectTo) - } - const loginRedirect = `/login?${loginParams.toString()}` - window.location.href = loginRedirect + const redirectUrl = getRedirectToLoginUrl() + window.location.replace(redirectUrl) } export const getFetchErrorMessage = (error: Error) => { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index a783b30..317e739 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -34,3 +34,12 @@ export const getOS = memoize((): OS => { export function delay(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)) } +export function getRedirectToLoginUrl() { + const requestUrl = new URL(window.location.href) + + const loginParams = new URLSearchParams(requestUrl.search) + if (!loginParams.has("redirectTo")) { + loginParams.append("redirectTo", requestUrl.pathname) + } + return `/login?${loginParams.toString()}` +} diff --git a/src/pages/(admin)/layout.tsx b/src/pages/(admin)/layout.tsx index b804d32..bb1ca89 100644 --- a/src/pages/(admin)/layout.tsx +++ b/src/pages/(admin)/layout.tsx @@ -1,4 +1,4 @@ -import { Link, Outlet, redirect } from "react-router-dom" +import { Link, Outlet } from "react-router-dom" import { AppSidebar } from "@/components/app-sidebar" import { Icons } from "@/components/icons" @@ -14,16 +14,6 @@ import { SidebarProvider, SidebarTrigger, } from "@/components/ui/sidebar" -import { queryUser } from "@/hooks/query/use-user" -import { queryClient } from "@/lib/query-client" - -export const loader = async () => { - const userData = await queryClient.ensureQueryData(queryUser()) - if (!userData) { - return redirect("/login") - } - return userData -} export function Component() { return ( diff --git a/src/pages/(external)/login/index.tsx b/src/pages/(external)/login/index.tsx index f0e0d14..cb42292 100644 --- a/src/pages/(external)/login/index.tsx +++ b/src/pages/(external)/login/index.tsx @@ -86,6 +86,9 @@ interface UserAuthFormProps extends React.HTMLAttributes { } function UserAuthForm({ className, ...props }: UserAuthFormProps) { const { t } = useTranslation(["auth", "common", "errors"]) + const redirectUrl = + new URLSearchParams(window.location.search).get("redirectTo") || + "/dashboard" const loginMutation = useUserLoginMutation() const navigate = useNavigate() @@ -102,9 +105,6 @@ function UserAuthForm({ className, ...props }: UserAuthFormProps) { position: "top-center", loading: t("login.loading"), success: () => { - const redirectUrl = - new URLSearchParams(window.location.search).get("redirectTo") || - "/dashboard" navigate(redirectUrl, { replace: true, })