-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
50 lines (41 loc) · 1.57 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { clerkMiddleware, createRouteMatcher, } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const isPublicRoute = createRouteMatcher([
"/",
"/sign-in",
"/sign-up",
"/home"
]);
const isPublicApiRoute = createRouteMatcher([
"/api/videos",
]);
export default clerkMiddleware( async(auth, req) => {
const { userId } = await auth();
const currentUrl = new URL(req.url);
const isAccessingDashboard = currentUrl.pathname === "/home";
const isApiRequest = currentUrl.pathname.startsWith("/api");
// Redirect to the home page if the user is signed in and trying to access a public page
if (userId && isPublicRoute(req) && !isAccessingDashboard) {
return NextResponse.redirect(new URL("/home", req.url));
}
// User isn't logged in
if (!userId) {
// User isn't logged in and is trying to access a protected route
if (!isPublicRoute(req) && !isPublicApiRoute(req)) {
return NextResponse.redirect(new URL("/sign-in", req.url));
}
// If req is for a protected API and the user is not logged in
if (isApiRequest && !isPublicApiRoute(req)) {
return NextResponse.redirect(new URL("/sign-in", req.url));
}
}
// If none of the conditions matched, proceed with the request
return NextResponse.next();
});
export const config = {
matcher: [
"/((?!.*\\..*\\..*|_next).*)", // Matches all routes except for files with a dot (like .js, .css, etc.) or _next
"/",
"/(api|trpc)(.*)" // Matches all API routes
],
};