-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
44 lines (32 loc) · 1.22 KB
/
middleware.ts
File metadata and controls
44 lines (32 loc) · 1.22 KB
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
import { authMiddleware, redirectToSignIn } from "@clerk/nextjs";
import { NextResponse } from "next/server";
export default authMiddleware({
// Routes that can be accessed while signed out
publicRoutes: ["/"],
// Routes that can always be accessed, and have
// no authentication information
ignoredRoutes: ["/no-auth-in-this-route"],
afterAuth:(auth, request) => {
if(auth.userId && auth.isPublicRoute) {
let path = '/select-org';
if(auth.orgId) {
path = `/organization/${auth.orgId}`;
}
const orgSelection = new URL(path, request.url);
return NextResponse.redirect(orgSelection);
}
if(!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: request.url });
}
if(auth.userId && !auth.orgId && request.nextUrl.pathname !== '/select-org') {
const orgSelection = new URL('/select-org', request.url);
return NextResponse.redirect(orgSelection);
}
}
});
export const config = {
// Protects all routes, including api/trpc.
// See https://clerk.com/docs/references/nextjs/auth-middleware
// for more information about configuring your Middleware
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};