-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
middleware.ts
28 lines (22 loc) · 844 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
<ai_context>
Contains middleware for protecting routes, checking user authentication, and redirecting as needed.
</ai_context>
*/
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server"
import { NextResponse } from "next/server"
const isProtectedRoute = createRouteMatcher(["/todo(.*)"])
export default clerkMiddleware(async (auth, req) => {
const { userId, redirectToSignIn } = await auth()
// If the user isn't signed in and the route is private, redirect to sign-in
if (!userId && isProtectedRoute(req)) {
return redirectToSignIn({ returnBackUrl: "/login" })
}
// If the user is logged in and the route is protected, let them view.
if (userId && isProtectedRoute(req)) {
return NextResponse.next()
}
})
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"]
}