-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
57 lines (47 loc) · 1.76 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
51
52
53
54
55
56
57
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { i18nRouter } from "next-i18n-router";
import i18nConfig from "./next-i18next.config";
import type { Database } from "./app/lib/database.types";
const PUBLIC_FILE = /\.(.*)$/;
export function middleware(req: NextRequest) {
const res = NextResponse.next();
// Create a Supabase client configured to use cookies
const supabase = createMiddlewareClient<Database>({ req, res });
// Refresh session if expired - required for Server Components
//await supabase.auth.getSession()
//const session = await getSession()
//const {data: {session}} = await supabase.auth.getSession();
if (
req.nextUrl.pathname.startsWith("/_next") ||
req.nextUrl.pathname.includes("/api/") ||
req.nextUrl.pathname.includes("/api/auth/") ||
PUBLIC_FILE.test(req.nextUrl.pathname)
) {
return NextResponse.next();
}
// If there is no session, redirect to the login page
/*if (!session?.user) {
return NextResponse.rewrite(new URL(`/${req.nextUrl.locale}/auth/login`, req.url))
} */
return i18nRouter(req, i18nConfig);
}
// Ensure the middleware is only called for relevant paths.
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!_next/static|_next/image|favicon.ico).*)",
"/auth/login",
"/auth/register",
"/api/auth/callback",
"/reset-password",
"/profile",
"/prueba2",
],
};