|
3 | 3 | import { useEffect } from "react"; |
4 | 4 | import { useRouter } from "next/navigation"; |
5 | 5 | import authClient from "@/lib/authClient"; |
| 6 | +import { safeRedirectPath } from "@/lib/authValidation"; |
6 | 7 | import { getSlugFromHost } from "@/lib/clientAuthUtils"; |
7 | 8 | import { useActiveOrganization } from "@/lib/hooks/useActiveOrganization"; |
8 | 9 |
|
| 10 | +export function getRedirectPath() { |
| 11 | + return safeRedirectPath( |
| 12 | + new URLSearchParams(window.location.search).get("redirect"), |
| 13 | + window.location.origin, |
| 14 | + ); |
| 15 | +} |
| 16 | + |
9 | 17 | /** |
10 | 18 | * Sends someone already signed in to this host's org away from a signed-out |
11 | | - * page (login, signup). `getPath` runs in the browser when redirecting. |
| 19 | + * page (login, signup). |
12 | 20 | */ |
13 | | -export function useRedirectIfSignedIn(getPath: () => string) { |
| 21 | +export function useRedirectIfSignedIn() { |
14 | 22 | const router = useRouter(); |
15 | 23 | const org = useActiveOrganization(); |
16 | 24 |
|
17 | 25 | useEffect(() => { |
18 | 26 | const checkLoggedIn = async () => { |
19 | 27 | const session = await authClient.getSession(); |
20 | | - if (!session?.data?.user) { |
21 | | - return; |
22 | | - } |
| 28 | + if (!session?.data?.user) return; |
23 | 29 |
|
24 | 30 | if (org?.slug === getSlugFromHost(window.location.host)) { |
25 | | - router.replace(getPath()); |
| 31 | + router.replace(getRedirectPath()); |
26 | 32 | } |
27 | 33 | }; |
28 | 34 |
|
29 | 35 | void checkLoggedIn(); |
30 | | - }, [org?.slug, router, getPath]); |
| 36 | + }, [org?.slug, router]); |
31 | 37 | } |
0 commit comments