From 6628be09fe891475c185e89b65e81d93f1566940 Mon Sep 17 00:00:00 2001 From: TeeGooodGood <122004889+TeeGoood@users.noreply.github.com> Date: Tue, 28 Jan 2025 03:09:09 +0700 Subject: [PATCH 1/2] refactor: protect state --- src/components/protect.tsx | 4 ++-- src/contexts/auth.tsx | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/components/protect.tsx b/src/components/protect.tsx index 892889c..923d8d4 100644 --- a/src/components/protect.tsx +++ b/src/components/protect.tsx @@ -18,10 +18,10 @@ export default function Protect({ children, callBack = '/', }: ProtectProps) { - const { user, isLoggingIn, isInitialized } = useAuth(); + const { user, isInitialized } = useAuth(); const router = useRouter(); - if (isLoggingIn || !isInitialized) { + if (!isInitialized) { return ; } diff --git a/src/contexts/auth.tsx b/src/contexts/auth.tsx index cb04290..4c79949 100644 --- a/src/contexts/auth.tsx +++ b/src/contexts/auth.tsx @@ -73,7 +73,6 @@ export default function AuthProvider({ children }: { children: ReactNode }) { return { success: false, error: error }; } - console.log('start login'); set('isLoggingIn', true); const loginResp = await sendLogin(userId); @@ -139,8 +138,10 @@ export default function AuthProvider({ children }: { children: ReactNode }) { } useEffect(() => { - login(); - set('isInitialized', true); + (async () => { + await login(); + set('isInitialized', true); + })(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); From 5b9b6942aea24b2766f4a70f3b75bf1af9f15cf1 Mon Sep 17 00:00:00 2001 From: TeeGooodGood <122004889+TeeGoood@users.noreply.github.com> Date: Tue, 28 Jan 2025 03:34:43 +0700 Subject: [PATCH 2/2] feat: gateway --- src/app/gateway/page.tsx | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/app/gateway/page.tsx diff --git a/src/app/gateway/page.tsx b/src/app/gateway/page.tsx new file mode 100644 index 0000000..6be0471 --- /dev/null +++ b/src/app/gateway/page.tsx @@ -0,0 +1,46 @@ +'use client'; + +import Load from '@/components/loading/loading'; +import { useAuth } from '@/contexts/auth'; +import { useSearchParams } from 'next/navigation'; +import React from 'react'; + +const pages = { + member: { + home: '/', + qr: '/qr', + profile: '/edit', + }, + staff: { + home: '/', + qr: '/staff/qr', + profile: '/edit', + }, + admin: { + home: '/', + qr: '/staff/qr', + profile: '/admin/dashboard', + }, +}; + +export default function Page() { + const { user, isInitialized } = useAuth(); + const searchParams = useSearchParams(); + const page = searchParams.get('page') || 'home'; + + if (!isInitialized) { + return ; + } + + window.location.href = + pages[user?.role || 'member'][page as 'home' | 'qr' | 'profile']; + return ; +} + +function Loading() { + return ( +
+ +
+ ); +}