tasko/middleware.ts

32 lines
886 B
TypeScript
Raw Normal View History

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
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
};