Skip to content

Commit

Permalink
fix: 401 redirect exception
Browse files Browse the repository at this point in the history
  • Loading branch information
TinsFox committed Nov 2, 2024
1 parent c884b96 commit 3cbb4b3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
14 changes: 5 additions & 9 deletions src/lib/api-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FetchError, ofetch } from "ofetch"

import { getRedirectToLoginUrl } from "./utils"

export const apiFetch = ofetch.create({
credentials: "include",
retry: false,
Expand Down Expand Up @@ -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) => {
Expand Down
9 changes: 9 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`
}
12 changes: 1 addition & 11 deletions src/pages/(admin)/layout.tsx
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 (
Expand Down
6 changes: 3 additions & 3 deletions src/pages/(external)/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ interface UserAuthFormProps extends React.HTMLAttributes<HTMLDivElement> { }

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()
Expand All @@ -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,
})
Expand Down

0 comments on commit 3cbb4b3

Please sign in to comment.