-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware.tsx
30 lines (24 loc) · 1 KB
/
middleware.tsx
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 { NextRequest } from 'next/server'
import { withAuth } from 'next-auth/middleware'
import createIntlMiddleware from 'next-intl/middleware'
export const defaultLocale = 'en'
export const locales = ['en', 'nl'] // Also reflect changes in → src/components/locale/Switcher
const publicPages = ['/', '/login', '/register', '/forgot']
const intlMiddleware = createIntlMiddleware({ locales, defaultLocale })
const authMiddleware = withAuth(
function onSuccess(req) { return intlMiddleware(req) },
{
callbacks: { authorized: ({ token }) => token != null },
pages: {
signIn: '/login'
}
}
)
export default function middleware(req: NextRequest) {
const publicPathnameRegex = RegExp(`^(/(${locales.join('|')}))?(${publicPages.join('|')})?/?$`, 'i')
const isPublicPage = publicPathnameRegex.test(req.nextUrl.pathname)
return isPublicPage ? intlMiddleware(req) : (authMiddleware as any)(req)
}
export const config = {
matcher: ['/((?!api|_next|.*\\..*).*)']
}