2024-02-16 01:49:19 +00:00
|
|
|
import { authMiddleware, redirectToSignIn } from '@clerk/nextjs';
|
|
|
|
import { NextResponse } from 'next/server';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
|
|
|
// 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/references/nextjs/auth-middleware for more information about configuring your Middleware
|
|
|
|
export default authMiddleware({
|
2024-02-16 01:49:19 +00:00
|
|
|
publicRoutes: ['/', '/api/webhook'],
|
2024-02-15 02:30:10 +00:00
|
|
|
afterAuth(auth, req) {
|
|
|
|
if (auth.userId && auth.isPublicRoute) {
|
2024-02-16 01:49:19 +00:00
|
|
|
let path = '/select-org';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
|
|
|
if (auth.orgId) {
|
|
|
|
path = `/organization/${auth.orgId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const orgSelection = new URL(path, req.url);
|
|
|
|
return NextResponse.redirect(orgSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!auth.userId && !auth.isPublicRoute) {
|
|
|
|
return redirectToSignIn({ returnBackUrl: req.url });
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
if (auth.userId && !auth.orgId && req.nextUrl.pathname !== '/select-org') {
|
|
|
|
const orgSelection = new URL('/select-org', req.url);
|
2024-02-15 02:30:10 +00:00
|
|
|
return NextResponse.redirect(orgSelection);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const config = {
|
2024-02-16 01:49:19 +00:00
|
|
|
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
|
2024-02-15 02:30:10 +00:00
|
|
|
};
|