From 0018081f16f9383f056458f1dc13f115da70b70b Mon Sep 17 00:00:00 2001 From: giancarlo Date: Sun, 28 Apr 2024 21:27:49 +0700 Subject: [PATCH] Refactor middleware to directly use UserResponse The adminMiddleware and other handlers in middleware.ts have been updated to directly use UserResponse, instead of creating and using a supabase middleware client. The retrieval and handling of user data are now more direct and streamlined. The code changes also include minor syntax adjustments for style consistency. --- apps/web/middleware.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index 11e89e762..ed1d784d2 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -95,15 +95,18 @@ function isServerAction(request: NextRequest) { return headers.has(NEXT_ACTION_HEADER); } -async function adminMiddleware(request: NextRequest, response: NextResponse) { +function adminMiddleware( + request: NextRequest, + response: NextResponse, + userResponse: UserResponse, +) { const isAdminPath = request.nextUrl.pathname.startsWith('/admin'); if (!isAdminPath) { return response; } - const supabase = createMiddlewareClient(request, response); - const { data, error } = await supabase.auth.getUser(); + const { data, error } = userResponse; // If user is not logged in, redirect to sign in page. // This should never happen, but just in case. @@ -135,17 +138,15 @@ function getPatterns() { }, { pattern: new URLPattern({ pathname: '/auth*' }), - handler: async ( + handler: ( req: NextRequest, - res: NextResponse, + _: NextResponse, userResponse: UserResponse, ) => { - const supabase = createMiddlewareClient(req, res); + const user = userResponse.data.user; // the user is logged out, so we don't need to do anything - if (!userResponse.data) { - await supabase.auth.signOut(); - + if (!user) { return; } @@ -168,13 +169,15 @@ function getPatterns() { res: NextResponse, userResponse: UserResponse, ) => { - const { data: user, error } = userResponse; + const { + data: { user }, + } = userResponse; const origin = req.nextUrl.origin; const next = req.nextUrl.pathname; // If user is not logged in, redirect to sign in page. - if (!user || error) { + if (!user) { const signIn = pathsConfig.auth.signIn; const redirectPath = `${signIn}?next=${next}`;