From b65e1dacc7addebd49001907e41c1c90960d75a1 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Sun, 28 Apr 2024 21:09:12 +0700 Subject: [PATCH] Refactor authentication handling and update loading components The authentication handling logic in the middleware.ts file was refactored to improve clarity and control flow. In addition, the loading component, previously located in the main app directory, has been deleted and recreated specifically for admin use. The list of private path prefixes has been updated in the use-auth-change-listener.ts file to reflect these changes. --- apps/web/app/admin/loading.tsx | 3 +++ apps/web/app/loading.tsx | 5 ----- apps/web/middleware.ts | 11 +++++++---- .../supabase/src/hooks/use-auth-change-listener.ts | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 apps/web/app/admin/loading.tsx delete mode 100644 apps/web/app/loading.tsx diff --git a/apps/web/app/admin/loading.tsx b/apps/web/app/admin/loading.tsx new file mode 100644 index 000000000..4ea53181d --- /dev/null +++ b/apps/web/app/admin/loading.tsx @@ -0,0 +1,3 @@ +import { GlobalLoader } from '@kit/ui/global-loader'; + +export default GlobalLoader; diff --git a/apps/web/app/loading.tsx b/apps/web/app/loading.tsx deleted file mode 100644 index 016865ebd..000000000 --- a/apps/web/app/loading.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import { GlobalLoader } from '@kit/ui/global-loader'; - -export default function Loading() { - return ; -} diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index ebd535b24..11e89e762 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -135,12 +135,15 @@ function getPatterns() { }, { pattern: new URLPattern({ pathname: '/auth*' }), - handler: async (req: NextRequest, res: NextResponse) => { + handler: async ( + req: NextRequest, + res: NextResponse, + userResponse: UserResponse, + ) => { const supabase = createMiddlewareClient(req, res); - const { data: user, error } = await supabase.auth.getUser(); // the user is logged out, so we don't need to do anything - if (error) { + if (!userResponse.data) { await supabase.auth.signOut(); return; @@ -151,7 +154,7 @@ function getPatterns() { // If user is logged in and does not need to verify MFA, // redirect to home page. - if (user && !isVerifyMfa) { + if (!isVerifyMfa) { return NextResponse.redirect( new URL(pathsConfig.app.home, req.nextUrl.origin).href, ); diff --git a/packages/supabase/src/hooks/use-auth-change-listener.ts b/packages/supabase/src/hooks/use-auth-change-listener.ts index a0666b491..3b98c18ea 100644 --- a/packages/supabase/src/hooks/use-auth-change-listener.ts +++ b/packages/supabase/src/hooks/use-auth-change-listener.ts @@ -11,7 +11,7 @@ import { useRevalidateUserSession, useUserSession } from './use-user-session'; * @name PRIVATE_PATH_PREFIXES * @description A list of private path prefixes */ -const PRIVATE_PATH_PREFIXES = ['/home', '/admin', '/update-password']; +const PRIVATE_PATH_PREFIXES = ['/home', '/admin', '/join', '/update-password']; /** * @name useAuthChangeListener