-
Notifications
You must be signed in to change notification settings - Fork 22
/
clerk.middleware.ts
32 lines (30 loc) · 1.22 KB
/
clerk.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
import { authMiddleware, redirectToSignIn } from "@clerk/nextjs";
import { NextResponse } from "next/server";
import { clerkUrls } from "./src/components/Authentication/Clerk/ClerkAuthSessionChecker";
// This example protects all routes including api/trpc routes
// Please edit this to allow other routes to be public as needed.
// See https://clerk.com/docs/nextjs/middleware for more information about configuring your middleware
export default authMiddleware({
afterAuth(auth, req, evt) {
// handle users who aren't authenticated
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url });
}
// redirect them to organization selection page
if (
auth.userId &&
!auth.orgId &&
req.nextUrl.pathname !== clerkUrls.organizationSwitcher &&
req.nextUrl.pathname !== clerkUrls.createOrganization &&
req.nextUrl.pathname !== clerkUrls.organizationProfile
) {
const orgSelection = new URL(clerkUrls.organizationSwitcher, req.url);
return NextResponse.redirect(orgSelection);
}
},
publicRoutes: ["/", "/login", "/registration"]
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
debug: true
};