add client/admin pages, show info and created admin api and server actions
This commit is contained in:
@@ -3,64 +3,56 @@ import NextAuth from 'next-auth'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { defaultLocale, locales } from '@/config/locales'
|
||||
import authConfig from '@/auth.config'
|
||||
import { apiAuthPrefix, AUTH_LOGIN_URL, authRoutesRegEx, DEFAULT_LOGIN_REDIRECT, publicRoutes } from '@/config/routes'
|
||||
import { apiAuthPrefixRegEx, AUTH_LOGIN_URL, authRoutesRegEx, DEFAULT_LOGIN_REDIRECT, publicRoutes } from '@/config/routes'
|
||||
import { testPathnameRegex } from '@/lib/utils'
|
||||
import { createI18nMiddleware } from 'next-international/middleware'
|
||||
import { CSP } from '@/lib/CSP'
|
||||
|
||||
interface AppRouteHandlerFnContext {
|
||||
params?: Record<string, string | string[]>;
|
||||
}
|
||||
|
||||
const I18nMiddleware = createI18nMiddleware({
|
||||
locales, defaultLocale, urlMappingStrategy: 'rewriteDefault',
|
||||
})
|
||||
|
||||
const { auth } = NextAuth(authConfig)
|
||||
|
||||
export const middleware = (request: NextRequest, event: AppRouteHandlerFnContext): NextResponse | null => {
|
||||
return NextAuth(authConfig).auth((request): any => {
|
||||
|
||||
return auth((request): any => {
|
||||
//const csp = new CSP(request, process.env.NODE_ENV === 'production')
|
||||
const csp = new CSP(request, false)
|
||||
const { nextUrl }: { nextUrl: NextURL } = request
|
||||
const isLoggedIn: boolean = !!request.auth
|
||||
const isApiAuthRoute: boolean = nextUrl.pathname.startsWith(apiAuthPrefix)
|
||||
const isPublicRoute: boolean = testPathnameRegex(publicRoutes, nextUrl.pathname)
|
||||
const isAuthRoute: boolean = testPathnameRegex(authRoutesRegEx, nextUrl.pathname)
|
||||
|
||||
if (isApiAuthRoute) {
|
||||
return null
|
||||
if (nextUrl.pathname.match(apiAuthPrefixRegEx)) {
|
||||
return csp.next()
|
||||
}
|
||||
|
||||
const I18nMiddleware = createI18nMiddleware({
|
||||
locales, defaultLocale, urlMappingStrategy: 'rewriteDefault',
|
||||
})
|
||||
const isLoggedIn: boolean = !!request.auth
|
||||
const isPublicRoute: boolean = testPathnameRegex(publicRoutes, nextUrl.pathname)
|
||||
const isAuthRoute: boolean = testPathnameRegex(authRoutesRegEx, nextUrl.pathname)
|
||||
|
||||
if (isAuthRoute) {
|
||||
if (isLoggedIn) {
|
||||
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl))
|
||||
}
|
||||
return I18nMiddleware(request)
|
||||
return csp.next(I18nMiddleware)
|
||||
}
|
||||
|
||||
if (!isLoggedIn && !isPublicRoute) {
|
||||
return NextResponse.redirect(new URL(AUTH_LOGIN_URL, nextUrl))
|
||||
}
|
||||
|
||||
return I18nMiddleware(request)
|
||||
return csp.next(I18nMiddleware)
|
||||
|
||||
})(request, event) as NextResponse
|
||||
}
|
||||
|
||||
// export const config = {
|
||||
// matcher: [
|
||||
// /*
|
||||
// * Match all request paths except for the ones starting with:
|
||||
// * - api (API routes)
|
||||
// * - _next/static (static files)
|
||||
// * - _next/image (image optimization files)
|
||||
// * - favicon.ico (favicon file)
|
||||
// */
|
||||
// {
|
||||
// source: '/((?!.+\\.[\\w]+$|api|_next/image|favicon.ico|robots.txt|trpc).*)', missing: [
|
||||
// { type: 'header', key: 'next-router-prefetch' }, { type: 'header', key: 'purpose', value: 'prefetch' }],
|
||||
// }],
|
||||
// }
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
'/((?!.+\\.[\\w]+$|_next|_next/image|_next/static).*)', '/(api|trpc)(.*)',
|
||||
'/((?!.+\\.[\\w]+$|_next|_next/image|_next/static|favicon.ico|robots.txt).*)',
|
||||
'/',
|
||||
'/(api|trpc)(.*)',
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user