added mail service
This commit is contained in:
60
middleware.ts
Normal file
60
middleware.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import NextAuth from 'next-auth'
|
||||
import { createI18nMiddleware } from 'next-international/middleware'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { defaultLocale, locales } from '@/config/locales'
|
||||
import authConfig from '@/auth.config'
|
||||
import {
|
||||
apiAuthPrefix,
|
||||
AUTH_LOGIN_URL,
|
||||
authRoutes,
|
||||
DEFAULT_LOGIN_REDIRECT,
|
||||
publicRoutes,
|
||||
testPathnameRegex,
|
||||
} from '@/config/routes'
|
||||
import { NextURL } from 'next/dist/server/web/next-url'
|
||||
|
||||
interface AppRouteHandlerFnContext {
|
||||
params?: Record<string, string | string[]>;
|
||||
}
|
||||
|
||||
export const middleware = (
|
||||
request: NextRequest,
|
||||
event: AppRouteHandlerFnContext): NextResponse | null => {
|
||||
return NextAuth(authConfig).auth((request): any => {
|
||||
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(authRoutes, nextUrl.pathname)
|
||||
|
||||
if (isApiAuthRoute) {
|
||||
return null
|
||||
}
|
||||
|
||||
const I18nMiddleware = createI18nMiddleware({
|
||||
locales, defaultLocale, urlMappingStrategy: 'rewriteDefault',
|
||||
})
|
||||
|
||||
if (isAuthRoute) {
|
||||
if (isLoggedIn) {
|
||||
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl))
|
||||
}
|
||||
return I18nMiddleware(request)
|
||||
}
|
||||
|
||||
if (!isLoggedIn && !isPublicRoute) {
|
||||
return NextResponse.redirect(new URL(AUTH_LOGIN_URL, nextUrl))
|
||||
}
|
||||
|
||||
return I18nMiddleware(request)
|
||||
|
||||
})(request, event) as NextResponse
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
'/((?!.+\\.[\\w]+$|_next).*)',
|
||||
'/(api|static|trpc)(.*)'],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user