-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (22 loc) · 818 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
27 lines (22 loc) · 818 Bytes
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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const isPublicRoute = createRouteMatcher([
"/",
"/sign-in(.*)",
"/sign-up(.*)",
"/api/webhook"
]);
export default clerkMiddleware((auth, req) => {
// If user is logged-in and on the landing page, redirect them
if (auth().userId && req.nextUrl.pathname === "/") {
const orgSelection = new URL("/org-selection", req.url);
return NextResponse.redirect(orgSelection);
}
if (isPublicRoute(req)) return; // if it's a public route, do nothing
// For any other route, require auth
// Also redirects unauthenticated users to the sign-in route automatically
auth().protect();
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};