-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
30 lines (25 loc) Β· 779 Bytes
/
middleware.ts
File metadata and controls
30 lines (25 loc) Β· 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from '@convex-dev/auth/nextjs/server'
const isPublicRoute = createRouteMatcher([
'/auth',
'/',
'/preview(.*)',
'/api/edgestore(.*)',
])
const isAuthRoute = createRouteMatcher(['/auth'])
export default convexAuthNextjsMiddleware((request, { convexAuth }) => {
if (!isPublicRoute(request) && !convexAuth.isAuthenticated()) {
return nextjsMiddlewareRedirect(request, '/auth')
}
if (isAuthRoute(request) && convexAuth.isAuthenticated()) {
return nextjsMiddlewareRedirect(request, '/')
}
})
export const config = {
// The following matcher runs middleware on all routes
// except static assets.
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
}