From 8a080eaf78931a95b0e58995a99af115ec53ee4d Mon Sep 17 00:00:00 2001 From: giancarlo Date: Thu, 28 Mar 2024 23:39:36 +0800 Subject: [PATCH] Update development script and add error handling in authentication middleware The "dev" script in the package.json for apps/web updated to include a "--turbo" flag. Moreover, enhanced error handling logic is added to the authentication --- apps/web/middleware.ts | 12 +++++++++++- apps/web/package.json | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index a3bc7a81c..8e5f15460 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -115,7 +115,14 @@ function getPatterns() { pattern: new URLPattern({ pathname: '/auth*' }), handler: async (req: NextRequest, res: NextResponse) => { const supabase = createMiddlewareClient(req, res); - const { data: user } = await supabase.auth.getUser(); + const { data: user, error } = await supabase.auth.getUser(); + + // the user is logged out, so we don't need to do anything + if (error) { + await supabase.auth.signOut(); + + return; + } // check if we need to verify MFA (user is authenticated but needs to verify MFA) const isVerifyMfa = req.nextUrl.pathname === pathsConfig.auth.verifyMfa; @@ -137,6 +144,9 @@ function getPatterns() { const origin = req.nextUrl.origin; const next = req.nextUrl.pathname; + console.log('home:user', user); + console.log('home:error', error); + // If user is not logged in, redirect to sign in page. if (!user || error) { const signIn = pathsConfig.auth.signIn; diff --git a/apps/web/package.json b/apps/web/package.json index 6185ef84f..8cda1ec93 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -7,7 +7,7 @@ "analyze": "ANALYZE=true pnpm run build", "build": "pnpm with-env next build", "clean": "git clean -xdf .next .turbo node_modules", - "dev": "pnpm with-env next dev", + "dev": "pnpm with-env next dev --turbo", "lint": "next lint", "format": "prettier --check \"**/*.{js,cjs,mjs,ts,tsx,md,json}\"", "start": "pnpm with-env next start",