-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding multiple middleware and locales
- Loading branch information
Showing
16 changed files
with
203 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const i18n = { | ||
defaultLocale: 'en', | ||
locales: ['en', 'fr'] | ||
} as const | ||
|
||
export type Locale = (typeof i18n)['locales'][number] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"login": { | ||
"title": "Login", | ||
"email": "Email", | ||
"password": "Password" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"login": { | ||
"title": "Connexion", | ||
"email": "Email", | ||
"password": "Mot de passe" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'server-only' | ||
import {Locale} from "../../i18n.config"; | ||
|
||
const dictionaries = { | ||
en: async () => await import('@/i18n/en.json').then(module => module.default), | ||
fr: async () => await import('@/i18n/fr.json').then(module => module.default) | ||
} | ||
|
||
export const getLocale = async (locale: Locale) => await dictionaries[locale]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,12 @@ | ||
import {NextRequest, NextResponse} from "next/server"; | ||
import {stackMiddlewares} from "@/middleware/stackMiddleware"; | ||
import {withAuth} from "@/middleware/auth.middleware"; | ||
import {withLocale} from "@/middleware/locale.middleware"; | ||
|
||
export default async function middleware(request: NextRequest) { | ||
const cookie = request.cookies.get('Authorization') | ||
if (!cookie) return NextResponse.redirect(new URL("/", request.url)) | ||
try { | ||
const res = await fetch(`${process.env.BACKEND_URL}/api/auth/check-cookie`, { | ||
credentials: 'include', | ||
headers: { | ||
cookie: Object.values(cookie).join("=") | ||
} | ||
}) | ||
if (res.status !== 200) { | ||
return NextResponse.redirect(new URL("/", request.url)) | ||
} | ||
return NextResponse.next() | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
const middlewares = [withLocale, withAuth]; | ||
export default stackMiddlewares(middlewares); | ||
|
||
export const config = { | ||
matcher: "/admin/:path*", | ||
}; | ||
// Matcher ignoring `/_next/` and `/api/` | ||
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {NextFetchEvent, NextRequest, NextResponse} from "next/server"; | ||
import {i18n} from "../../i18n.config"; | ||
|
||
const middlewarePath = ["/admin"] | ||
|
||
export const withAuth = (next: any) => { | ||
return async (request: NextRequest, _next: NextFetchEvent) => { | ||
const pathname = getPathWithoutLocale(request.nextUrl.pathname); | ||
|
||
if (middlewarePath?.some((path) => pathname.startsWith(path))) { | ||
const cookie = request.cookies.get('Authorization') | ||
if (!cookie) return NextResponse.redirect(new URL("/", request.url)) | ||
try { | ||
const res = await fetch(`${process.env.BACKEND_URL}/api/auth/check-cookie`, { | ||
credentials: 'include', | ||
headers: { | ||
cookie: Object.values(cookie).join("=") | ||
} | ||
}) | ||
if (res.status !== 200) { | ||
return NextResponse.redirect(new URL("/", request.url)) | ||
} | ||
return NextResponse.next() | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
return next(request, _next); | ||
} | ||
} | ||
|
||
const getPathWithoutLocale = (path: string): string => { | ||
for (const locale of i18n.locales) { | ||
if (path.startsWith(`/${locale}`)) { | ||
return path.slice(locale.length + 1) | ||
} | ||
} | ||
return path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {NextFetchEvent, NextRequest} from 'next/server' | ||
import {NextResponse} from 'next/server' | ||
|
||
import {match} from '@formatjs/intl-localematcher' | ||
import Negotiator from 'negotiator' | ||
import {i18n} from "../../i18n.config"; | ||
|
||
function getLocale(request: NextRequest): string { | ||
const i18nCookie = request.cookies.get("i18n_locale") | ||
if (i18nCookie) { | ||
return i18nCookie.value | ||
} else { | ||
const negotiatorHeaders: Record<string, string> = {} | ||
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value)) | ||
|
||
// @ts-ignore locales are readonly | ||
const locales: string[] = i18n.locales | ||
const languages: string[] = new Negotiator({headers: negotiatorHeaders}).languages() | ||
return match(languages, locales, i18n.defaultLocale) | ||
} | ||
} | ||
|
||
export const withLocale = (next: any) => { | ||
return async (request: NextRequest, _next: NextFetchEvent) => { | ||
const pathname = request.nextUrl.pathname; | ||
const pathnameIsMissingLocale = i18n.locales.every( | ||
locale => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}` | ||
) | ||
// Redirect if there is no locale | ||
if (pathnameIsMissingLocale) { | ||
const locale = getLocale(request) | ||
const url = new URL( | ||
`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`, | ||
request.url | ||
) | ||
const response = NextResponse.redirect(url) | ||
response.cookies.set({ | ||
name: 'i18n_locale', | ||
value: locale, | ||
path: '/', | ||
}) | ||
return response | ||
} | ||
|
||
return next(request, _next); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { | ||
NextMiddleware | ||
} from "next/server"; | ||
|
||
export type MiddlewareFactory = (middleware: NextMiddleware) => NextMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
NextMiddleware, | ||
NextResponse | ||
} from "next/server"; | ||
import {MiddlewareFactory} from "@/middleware/middleware"; | ||
|
||
export function stackMiddlewares(functions: MiddlewareFactory[] = [], index = 0): NextMiddleware { | ||
const current = functions[index]; | ||
if (current) { | ||
const next = stackMiddlewares(functions, index + 1); | ||
return current(next); | ||
} | ||
return () => NextResponse.next(); | ||
} |