forked from ixartz/Next-js-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
22 lines (19 loc) · 809 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { authMiddleware, redirectToSignIn } from '@clerk/nextjs';
import type { NextRequest } from 'next/server';
export default authMiddleware({
publicRoutes: (req: NextRequest) =>
!req.nextUrl.pathname.startsWith('/dashboard'),
ignoredRoutes: ['/api/guestbook'],
// By default, the middleware will return a 401 response for all routes `/api/*` when the user is signed out.
// But, for `/api/guestbook`, we want unauthenticated users to be able to access it.
// eslint-disable-next-line consistent-return
afterAuth(auth, req) {
// handle users who aren't authenticated
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url });
}
},
});
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};