forked from asifahemmed09/e-commerce-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
52 lines (46 loc) · 1.18 KB
/
middleware.ts
File metadata and controls
52 lines (46 loc) · 1.18 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import createMiddleware from 'next-intl/middleware'
import { routing } from './i18n/routing'
import NextAuth from 'next-auth'
import authConfig from './auth.config'
const publicPages = [
'/',
'/search',
'/sign-in',
'/sign-up',
'/cart',
'/cart/(.*)',
'/product/(.*)',
'/page/(.*)',
// (/secret requires auth)
]
const intlMiddleware = createMiddleware(routing)
const { auth } = NextAuth(authConfig)
export default auth((req) => {
const publicPathnameRegex = RegExp(
`^(/(${routing.locales.join('|')}))?(${publicPages
.flatMap((p) => (p === '/' ? ['', '/'] : p))
.join('|')})/?$`,
'i'
)
const isPublicPage = publicPathnameRegex.test(req.nextUrl.pathname)
if (isPublicPage) {
// return NextResponse.next()
return intlMiddleware(req)
} else {
if (!req.auth) {
const newUrl = new URL(
`/sign-in?callbackUrl=${
encodeURIComponent(req.nextUrl.pathname) || '/'
}`,
req.nextUrl.origin
)
return Response.redirect(newUrl)
} else {
return intlMiddleware(req)
}
}
})
export const config = {
// Skip all paths that should not be internationalized
matcher: ['/((?!api|_next|.*\\..*).*)'],
}