31 lines
1019 B
TypeScript
31 lines
1019 B
TypeScript
import {
|
|
convexAuthNextjsMiddleware,
|
|
createRouteMatcher,
|
|
isAuthenticatedNextjs,
|
|
nextjsMiddlewareRedirect,
|
|
} from "@convex-dev/auth/nextjs/server";
|
|
|
|
const isSignInPage = createRouteMatcher(["/auth"]);
|
|
const isProtectedPage = createRouteMatcher([
|
|
"/dashboard(.*)",
|
|
"/onboarding(.*)",
|
|
"/opportunities(.*)",
|
|
]);
|
|
|
|
export default convexAuthNextjsMiddleware(async (request) => {
|
|
if (isSignInPage(request) && (await isAuthenticatedNextjs())) {
|
|
return nextjsMiddlewareRedirect(request, "/dashboard");
|
|
}
|
|
if (isProtectedPage(request) && !(await isAuthenticatedNextjs())) {
|
|
const nextUrl = new URL("/auth", request.url);
|
|
nextUrl.searchParams.set("next", request.nextUrl.pathname + request.nextUrl.search);
|
|
return nextjsMiddlewareRedirect(request, nextUrl.toString());
|
|
}
|
|
});
|
|
|
|
export const config = {
|
|
// The following matcher runs middleware on all routes
|
|
// except static assets.
|
|
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
|
|
};
|