35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import {
|
|
convexAuthNextjsMiddleware,
|
|
createRouteMatcher,
|
|
nextjsMiddlewareRedirect,
|
|
} from '@convex-dev/auth/nextjs/server';
|
|
import { banSuspiciousIPs } from '@/lib/middleware/ban-suspicious-ips';
|
|
|
|
const isSignInPage = createRouteMatcher(['/signin']);
|
|
const isProtectedRoute = createRouteMatcher(['/', '/profile']);
|
|
|
|
export default convexAuthNextjsMiddleware(
|
|
async (request, { convexAuth }) => {
|
|
const banResponse = banSuspiciousIPs(request);
|
|
if (banResponse) return banResponse;
|
|
if (isSignInPage(request) && (await convexAuth.isAuthenticated())) {
|
|
return nextjsMiddlewareRedirect(request, '/');
|
|
}
|
|
if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) {
|
|
return nextjsMiddlewareRedirect(request, '/signin');
|
|
}
|
|
},
|
|
{ cookieConfig: { maxAge: 60 * 60 * 24 * 30 } },
|
|
);
|
|
|
|
export const config = {
|
|
// The following matcher runs middleware on all routes
|
|
// except static assets.
|
|
matcher: [
|
|
'/((?!_next/static|_next/image|favicon.ico|monitoring-tunnel|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
|
|
'/((?!.*\\..*|_next).*)',
|
|
'/',
|
|
'/(api|trpc)(.*)',
|
|
],
|
|
};
|