Repository navigation
Expand file tree
/
Copy pathmiddleware.js
More file actions
61 lines (51 loc) · 2.02 KB
/
Copy pathmiddleware.js
File metadata and controls
61 lines (51 loc) · 2.02 KB
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
58
59
60
61
import { createServerClient } from "@supabase/ssr";
import { NextResponse } from "next/server";
export async function middleware(request) {
let supabaseResponse = NextResponse.next({ request });
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
{
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) =>
request.cookies.set(name, value),
);
supabaseResponse = NextResponse.next({ request });
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options),
);
},
},
},
);
const {
data: { user },
} = await supabase.auth.getUser();
const path = request.nextUrl.pathname; // Mengambil alamat URL saat ini
// 1. DAFTAR HALAMAN YANG DIGEMBOK (Silakan tambah sesuai kebutuhan)
const protectedRoutes = ["/dashboard", "/transaksi", "/buku-kas", "/ai-asisten"];
// 2. Mengecek apakah URL saat ini ada di dalam daftar gembok
const isProtected = protectedRoutes.some((route) => path.startsWith(route));
// 3. Mengecek apakah URL saat ini adalah halaman login/register
const isAuthPage = path.startsWith("/login") || path.startsWith("/register");
// 4. Jika mencoba masuk area gembok TAPI belum login -> Tendang ke Login
if (isProtected && !user) {
return NextResponse.redirect(new URL("/login", request.url));
}
// 5. Jika SUDAH login TAPI iseng buka halaman login/register -> Arahkan ke Dashboard
if (isAuthPage && user) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return supabaseResponse;
}
export const config = {
// Biarkan matcher ini menyapu bersih (mengecek semua halaman)
// KECUALI file statis sistem Next.js dan gambar agar loading tetap ngebut
matcher: [
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};