-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
31 lines (24 loc) · 938 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
29
30
31
import { authMiddleware, redirectToSignIn } from "@clerk/nextjs";
import { NextResponse } from "next/server";
export default authMiddleware({
publicRoutes: ["/", "/api/webhook"],
afterAuth(auth, req) {
if (auth.userId && auth.isPublicRoute) {
const path = auth.orgId
? `/organization/${auth.orgId}`
: `/select-org`;
const url = new URL(path, req.url);
return NextResponse.redirect(url);
}
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url });
}
if (auth.userId && !auth.orgId && (req.nextUrl.pathname !== "/select-org")) {
const selectOrgUrl = new URL("/select-org", req.url);
return NextResponse.redirect(selectOrgUrl);
}
}
});
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};