Skip to content

Commit

Permalink
add routing logic to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
psiddharthdesign committed Jul 29, 2024
1 parent 05a371d commit d55881c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d55881c

Please sign in to comment.