-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
27 lines (26 loc) · 951 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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const isPublicRoute = createRouteMatcher(["/sign-in", "/sign-up"]);
export default clerkMiddleware((auth, request) => {
const { userId, orgId } = auth();
const { pathname } = request.nextUrl;
if (isPublicRoute(request)) {
return NextResponse.next();
}
if (!userId) {
return auth().redirectToSignIn({ returnBackUrl: request.url });
}
if (!orgId && pathname !== "/select-org") {
return NextResponse.redirect(new URL("/select-org", request.url));
}
if (orgId && pathname === "/select-org") {
return NextResponse.redirect(new URL(`/organization/${orgId}`, request.url));
}
if(pathname==="/" && orgId){
return NextResponse.redirect(new URL("/select-org", request.url));
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/(api|trpc)(.*)"],
};