Skip to content

Commit 842803c

Browse files
author
thorwebdev
committed
feat: add middleware auth redirect.
1 parent 80be27c commit 842803c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

middleware.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,27 @@ export async function middleware(req: NextRequest) {
1111

1212
// Refresh session if expired - required for Server Components
1313
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
14-
await supabase.auth.getSession()
14+
const {
15+
data: { session }
16+
} = await supabase.auth.getSession()
17+
18+
// OPTIONAL: this forces users to be logged in to use the chatbot.
19+
// If you want to allow anonymous users, simply remove the check below.
20+
if (!session && !req.url.includes('/sign-in'))
21+
return NextResponse.redirect(new URL('/sign-in', req.url))
1522

1623
return res
1724
}
25+
26+
export const config = {
27+
matcher: [
28+
/*
29+
* Match all request paths except for the ones starting with:
30+
* - api (API routes)
31+
* - _next/static (static files)
32+
* - _next/image (image optimization files)
33+
* - favicon.ico (favicon file)
34+
*/
35+
'/((?!api|_next/static|_next/image|favicon.ico).*)'
36+
]
37+
}

0 commit comments

Comments
 (0)