-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
50 lines (43 loc) · 1.64 KB
/
middleware.ts
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
import { type CookieOptions } from "@supabase/ssr";
import { getCookie, setCookie } from "cookies-next";
import { type NextRequest, type NextResponse } from "next/server";
import createIntlMiddleware from "next-intl/middleware";
import { composeDbServerClient } from "@/utils/supabase/compose-db-server-client";
import { locales } from "./navigation";
/**
* Function that returns an object with methods for handling cookies. Can be used as an argument to the createDbServerClient method in server scenarios.
*/
export const composeDbReqResClient = (req: NextRequest, res: NextResponse) => {
return composeDbServerClient({
cookieMethods: () => ({
get(name: string) {
return getCookie(name, { req, res });
},
set(name: string, value: string, options: CookieOptions) {
return setCookie(name, value, { req, res, ...options });
},
remove(name: string, options: CookieOptions) {
return setCookie(name, "", { req, res, ...options });
},
}),
});
};
export default async function middleware(req: NextRequest) {
const handleI18nRouting = createIntlMiddleware({
locales,
defaultLocale: "pt",
});
const res = handleI18nRouting(req);
const { dbServerClient } = composeDbReqResClient(req, res);
await dbServerClient.auth.getSession(); // automatically refreshes the session if expired
return res;
}
export const config = {
// Match only internationalized pathnames
matcher: [
// Match all pathnames except for
// - … if they start with `/api`, `/_next` or `/_vercel`
// - … the ones containing a dot (e.g. `favicon.ico`)
"/((?!api|_next|_vercel|.*\\..*).*)",
],
};