From 082ba3ce80026e88010a218d8c02b6aed6386381 Mon Sep 17 00:00:00 2001 From: Chris Estreich Date: Mon, 19 May 2025 14:51:06 -0700 Subject: [PATCH] Move root redirect to the server (#42) --- src/app/(centered)/authorized/Authorized.tsx | 59 -------------------- src/app/(centered)/authorized/page.tsx | 49 +++++++++++++++- src/app/Home.tsx | 16 ------ src/app/page.tsx | 6 +- 4 files changed, 51 insertions(+), 79 deletions(-) delete mode 100644 src/app/(centered)/authorized/Authorized.tsx delete mode 100644 src/app/Home.tsx diff --git a/src/app/(centered)/authorized/Authorized.tsx b/src/app/(centered)/authorized/Authorized.tsx deleted file mode 100644 index dc1fa67a05..0000000000 --- a/src/app/(centered)/authorized/Authorized.tsx +++ /dev/null @@ -1,59 +0,0 @@ -'use client'; - -import { useState, useRef, useEffect } from 'react'; -import { useSessionStorage } from 'react-use'; -import { useRouter } from 'next/navigation'; -import { useAuth } from '@clerk/nextjs'; -import { LoaderCircle, CircleCheck, CircleX } from 'lucide-react'; - -import { Card, CardContent } from '@/components/ui'; - -export const Authorized = () => { - const { isSignedIn, orgId } = useAuth(); - const [isLoading, setIsLoading] = useState(true); - const isLoadingRef = useRef(true); - const router = useRouter(); - const [state, setState] = useSessionStorage('state'); - - useEffect(() => { - if (typeof isSignedIn !== 'undefined' && isLoadingRef.current) { - isLoadingRef.current = false; - setTimeout(() => setIsLoading(false), 250); - } - }, [isSignedIn]); - - useEffect(() => { - if (!isLoading) { - let path; - - if (!isSignedIn) { - path = state ? `/sign-in?state=${state}` : '/sign-in'; - } else if (!orgId) { - path = state ? `/select-org/${state}` : '/select-org'; - } else { - path = state ? `/extension/sign-in?state=${state}` : '/dashboard'; - } - - setTimeout(() => router.push(path), 1000); - } - }, [router, isLoading, isSignedIn, orgId, state, setState]); - - return ( - - -
-
- Authenticating -
- {isLoading ? ( - - ) : isSignedIn ? ( - - ) : ( - - )} -
-
-
- ); -}; diff --git a/src/app/(centered)/authorized/page.tsx b/src/app/(centered)/authorized/page.tsx index 088db4ce91..245e1b587c 100644 --- a/src/app/(centered)/authorized/page.tsx +++ b/src/app/(centered)/authorized/page.tsx @@ -1,5 +1,50 @@ -import { Authorized } from './Authorized'; +'use client'; + +import { useState, useRef, useEffect } from 'react'; +import { useSessionStorage } from 'react-use'; +import { useRouter } from 'next/navigation'; +import { useAuth } from '@clerk/nextjs'; +import { LoaderCircle, CircleCheck, CircleX } from 'lucide-react'; export default function Page() { - return ; + const { isSignedIn, orgId } = useAuth(); + const [isLoading, setIsLoading] = useState(true); + const isLoadingRef = useRef(true); + const router = useRouter(); + const [state, setState] = useSessionStorage('state'); + + useEffect(() => { + if (typeof isSignedIn !== 'undefined' && isLoadingRef.current) { + isLoadingRef.current = false; + setTimeout(() => setIsLoading(false), 250); + } + }, [isSignedIn]); + + useEffect(() => { + if (!isLoading) { + let path; + + if (!isSignedIn) { + path = state ? `/sign-in?state=${state}` : '/sign-in'; + } else if (!orgId) { + path = state ? `/select-org/${state}` : '/select-org'; + } else { + path = state ? `/extension/sign-in?state=${state}` : '/dashboard'; + } + + setTimeout(() => router.push(path), 1000); + } + }, [router, isLoading, isSignedIn, orgId, state, setState]); + + return ( +
+ {isLoading ? ( + + ) : isSignedIn ? ( + + ) : ( + + )} +
+ ); } diff --git a/src/app/Home.tsx b/src/app/Home.tsx deleted file mode 100644 index 33a6d0145f..0000000000 --- a/src/app/Home.tsx +++ /dev/null @@ -1,16 +0,0 @@ -'use client'; - -import { useEffect } from 'react'; -import { useRouter } from 'next/navigation'; -import { useAuth } from '@clerk/nextjs'; - -export const Home = () => { - const { isSignedIn } = useAuth(); - const router = useRouter(); - - useEffect(() => { - router.push(isSignedIn ? '/dashboard' : '/sign-in'); - }, [router, isSignedIn]); - - return null; -}; diff --git a/src/app/page.tsx b/src/app/page.tsx index 5731c7fbe6..6d10bd35c0 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,5 +1,7 @@ -import { Home } from './Home'; +import { redirect } from 'next/navigation'; +import { currentUser } from '@clerk/nextjs/server'; export default async function Page() { - return ; + const user = await currentUser().catch(() => null); + redirect(user !== null ? '/dashboard' : '/sign-in'); }