From d55881ca3930d3d5ae3b4b258ab250d411ab9ec3 Mon Sep 17 00:00:00 2001 From: psiddharthdesign <107192927+psiddharthdesign@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:22:52 +0530 Subject: [PATCH] add routing logic to middleware --- src/middleware.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/middleware.ts b/src/middleware.ts index 49d5a78d..f7bc47ca 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -37,6 +37,10 @@ const unprotectedPagePrefixes = [ `/waitlist(/.*)?`, ]; +function isLandingPage(pathname: string) { + return pathname === '/'; +} + function isUnprotectedPage(pathname: string) { return unprotectedPagePrefixes.some((prefix) => { const matchPath = match(prefix); @@ -76,6 +80,19 @@ export async function middleware(req: NextRequest) { if (shouldOnboardUser(req.nextUrl.pathname, maybeUser)) { return NextResponse.redirect(toSiteURL('/onboarding')); } + if (isLandingPage(req.nextUrl.pathname)) { + if (maybeUser) { + //user is logged in, lets validate session and redirect on success + const user = await supabase.auth.getUser(); + if (user.error) { + return NextResponse.redirect(toSiteURL('/login')); + } + return NextResponse.redirect(toSiteURL('/dashboard')); + } else { + //user is not logged in, lets redirect to login + return NextResponse.redirect(toSiteURL('/login')); + } + } if (!isUnprotectedPage(req.nextUrl.pathname) && maybeUser) { // user is possibly logged in, but lets validate session const user = await supabase.auth.getUser();