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
This commit is contained in:
giancarlo
2024-03-28 23:39:36 +08:00
parent 9011abfca7
commit 8a080eaf78
2 changed files with 12 additions and 2 deletions

View File

@@ -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;

View File

@@ -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",