fix: Node 26 uyumsuzluğunu çöz — node-appwrite -> appwrite SDK
Sorun:
- node-appwrite paketi 'node-fetch-native-with-agent' polyfill'i kullanıyor
- Node.js 26'nın undici implementation'ı ile uyumsuz
- 'fetch failed / InvalidArgumentError: invalid onError method' hatası
- Login dahil tüm Appwrite çağrıları başarısız
Çözüm:
- Tüm node-appwrite kullanımını browser SDK 'appwrite'a geçir
- Browser SDK native fetch kullanıyor, Node 26 uyumlu
- API key tabanlı admin client yerine session cookie tabanlı user client
- Public reads (read('any')): publicDB (auth'suz client)
- Admin CRUD: userDB(sessionSecret) (cookie'deki session)
- Storage upload doğrudan File objesi alıyor (InputFile.fromBuffer gerekmez)
Etkilenen dosyalar:
- lib/appwrite-server.ts: publicClient + sessionClient
- lib/auth.ts: requireSessionSecret eklendi
- lib/admin-actions.ts: tüm action'lar sessionClient kullanıyor
- app/actions.ts: publicDB
- lib/data.ts: publicDB
- app/admin/login/actions.ts: appwrite SDK
- app/admin/(protected)/page.tsx, medya/page.tsx: userDB/userStorage
End-to-end test edildi:
✓ Login (401 doğru hata)
✓ Public read (services)
✓ Anonim create (contact form)
✓ npm run build 23 route
This commit is contained in:
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { ID } from "node-appwrite";
|
import { ID } from "appwrite";
|
||||||
import { adminDB, DATABASE_ID, TABLES } from "@/lib/appwrite-server";
|
import { publicDB, DATABASE_ID, TABLES } from "@/lib/appwrite-server";
|
||||||
|
|
||||||
export type ContactFormState = {
|
export type ContactFormState = {
|
||||||
ok: boolean;
|
ok: boolean;
|
||||||
@@ -34,7 +34,7 @@ export async function submitContact(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await adminDB.createRow({
|
await publicDB.createRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.contactMessages,
|
tableId: TABLES.contactMessages,
|
||||||
rowId: ID.unique(),
|
rowId: ID.unique(),
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
import { PageHeader } from "@/components/admin/form";
|
import { PageHeader } from "@/components/admin/form";
|
||||||
import { adminStorage, MEDIA_BUCKET_ID } from "@/lib/appwrite-server";
|
import { MEDIA_BUCKET_ID, userStorage } from "@/lib/appwrite-server";
|
||||||
import { Query } from "node-appwrite";
|
import { requireSessionSecret } from "@/lib/auth";
|
||||||
|
import { Query } from "appwrite";
|
||||||
import { Upload } from "lucide-react";
|
import { Upload } from "lucide-react";
|
||||||
import { DeleteButton } from "@/components/admin/delete-button";
|
import { DeleteButton } from "@/components/admin/delete-button";
|
||||||
import { uploadMedia, deleteMediaFile } from "@/lib/admin-actions";
|
import { uploadMedia, deleteMediaFile } from "@/lib/admin-actions";
|
||||||
|
|
||||||
async function listFiles() {
|
async function listFiles() {
|
||||||
try {
|
try {
|
||||||
const res = await adminStorage.listFiles({
|
const secret = await requireSessionSecret();
|
||||||
|
const res = await userStorage(secret).listFiles({
|
||||||
bucketId: MEDIA_BUCKET_ID,
|
bucketId: MEDIA_BUCKET_ID,
|
||||||
queries: [Query.orderDesc("$createdAt"), Query.limit(100)],
|
queries: [Query.orderDesc("$createdAt"), Query.limit(100)],
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { ArrowRight } from "lucide-react";
|
import { ArrowRight } from "lucide-react";
|
||||||
import { adminDB, DATABASE_ID, TABLES } from "@/lib/appwrite-server";
|
import { Query } from "appwrite";
|
||||||
import { Query } from "node-appwrite";
|
import { DATABASE_ID, TABLES, userDB } from "@/lib/appwrite-server";
|
||||||
|
import { requireSessionSecret } from "@/lib/auth";
|
||||||
|
|
||||||
async function safeCount(tableId: string, queries: string[] = []) {
|
async function safeCount(tableId: string, queries: string[] = []) {
|
||||||
try {
|
try {
|
||||||
const res = await adminDB.listRows({
|
const secret = await requireSessionSecret();
|
||||||
|
const res = await userDB(secret).listRows({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId,
|
tableId,
|
||||||
queries: [...queries, Query.limit(1)],
|
queries: [...queries, Query.limit(1)],
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { Account, Client } from "node-appwrite";
|
import { Account, Client } from "appwrite";
|
||||||
import { SESSION_COOKIE } from "@/lib/auth";
|
import { SESSION_COOKIE } from "@/lib/auth";
|
||||||
|
|
||||||
const endpoint = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!;
|
const endpoint = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!;
|
||||||
|
|||||||
+48
-46
@@ -1,20 +1,23 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
import { ID } from "node-appwrite";
|
import { ID } from "appwrite";
|
||||||
import { InputFile } from "node-appwrite/file";
|
|
||||||
import {
|
import {
|
||||||
adminDB,
|
|
||||||
adminStorage,
|
|
||||||
DATABASE_ID,
|
DATABASE_ID,
|
||||||
MEDIA_BUCKET_ID,
|
MEDIA_BUCKET_ID,
|
||||||
TABLES,
|
TABLES,
|
||||||
|
userDB,
|
||||||
|
userStorage,
|
||||||
} from "@/lib/appwrite-server";
|
} from "@/lib/appwrite-server";
|
||||||
import { getCurrentUser } from "@/lib/auth";
|
import { requireSessionSecret } from "@/lib/auth";
|
||||||
|
|
||||||
async function gate() {
|
async function db() {
|
||||||
const user = await getCurrentUser();
|
const secret = await requireSessionSecret();
|
||||||
if (!user) throw new Error("Yetkisiz");
|
return userDB(secret);
|
||||||
|
}
|
||||||
|
async function storage() {
|
||||||
|
const secret = await requireSessionSecret();
|
||||||
|
return userStorage(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
function slugify(s: string) {
|
function slugify(s: string) {
|
||||||
@@ -55,30 +58,29 @@ function strArr(v: FormDataEntryValue | null) {
|
|||||||
// ─── Media upload ────────────────────────────────────────────────
|
// ─── Media upload ────────────────────────────────────────────────
|
||||||
|
|
||||||
export async function uploadMedia(formData: FormData): Promise<void> {
|
export async function uploadMedia(formData: FormData): Promise<void> {
|
||||||
await gate();
|
|
||||||
const file = formData.get("file");
|
const file = formData.get("file");
|
||||||
if (!(file instanceof File) || file.size === 0) {
|
if (!(file instanceof File) || file.size === 0) {
|
||||||
throw new Error("Dosya seçilmedi");
|
throw new Error("Dosya seçilmedi");
|
||||||
}
|
}
|
||||||
const bytes = Buffer.from(await file.arrayBuffer());
|
const s = await storage();
|
||||||
await adminStorage.createFile({
|
await s.createFile({
|
||||||
bucketId: MEDIA_BUCKET_ID,
|
bucketId: MEDIA_BUCKET_ID,
|
||||||
fileId: ID.unique(),
|
fileId: ID.unique(),
|
||||||
file: InputFile.fromBuffer(bytes, file.name),
|
file,
|
||||||
});
|
});
|
||||||
revalidatePath("/admin/medya");
|
revalidatePath("/admin/medya");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteMediaFile(fileId: string) {
|
export async function deleteMediaFile(fileId: string) {
|
||||||
await gate();
|
const s = await storage();
|
||||||
await adminStorage.deleteFile({ bucketId: MEDIA_BUCKET_ID, fileId });
|
await s.deleteFile({ bucketId: MEDIA_BUCKET_ID, fileId });
|
||||||
revalidatePath("/admin/medya");
|
revalidatePath("/admin/medya");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Blog ────────────────────────────────────────────────────────
|
// ─── Blog ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
export async function saveBlogPost(formData: FormData) {
|
export async function saveBlogPost(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = str(formData.get("id"));
|
const id = str(formData.get("id"));
|
||||||
const title = str(formData.get("title"));
|
const title = str(formData.get("title"));
|
||||||
if (!title) throw new Error("Başlık zorunlu");
|
if (!title) throw new Error("Başlık zorunlu");
|
||||||
@@ -105,14 +107,14 @@ export async function saveBlogPost(formData: FormData) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
await adminDB.updateRow({
|
await d.updateRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.blogPosts,
|
tableId: TABLES.blogPosts,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await adminDB.createRow({
|
await d.createRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.blogPosts,
|
tableId: TABLES.blogPosts,
|
||||||
rowId: ID.unique(),
|
rowId: ID.unique(),
|
||||||
@@ -125,9 +127,9 @@ export async function saveBlogPost(formData: FormData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteBlogPost(formData: FormData) {
|
export async function deleteBlogPost(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = String(formData.get("id"));
|
const id = String(formData.get("id"));
|
||||||
await adminDB.deleteRow({
|
await d.deleteRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.blogPosts,
|
tableId: TABLES.blogPosts,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
@@ -139,7 +141,7 @@ export async function deleteBlogPost(formData: FormData) {
|
|||||||
// ─── Services ────────────────────────────────────────────────────
|
// ─── Services ────────────────────────────────────────────────────
|
||||||
|
|
||||||
export async function saveService(formData: FormData) {
|
export async function saveService(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = str(formData.get("id"));
|
const id = str(formData.get("id"));
|
||||||
const title = str(formData.get("title"));
|
const title = str(formData.get("title"));
|
||||||
if (!title) throw new Error("Başlık zorunlu");
|
if (!title) throw new Error("Başlık zorunlu");
|
||||||
@@ -156,14 +158,14 @@ export async function saveService(formData: FormData) {
|
|||||||
featured: bool(formData.get("featured")),
|
featured: bool(formData.get("featured")),
|
||||||
};
|
};
|
||||||
if (id) {
|
if (id) {
|
||||||
await adminDB.updateRow({
|
await d.updateRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.services,
|
tableId: TABLES.services,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await adminDB.createRow({
|
await d.createRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.services,
|
tableId: TABLES.services,
|
||||||
rowId: slug,
|
rowId: slug,
|
||||||
@@ -176,9 +178,9 @@ export async function saveService(formData: FormData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteService(formData: FormData) {
|
export async function deleteService(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = String(formData.get("id"));
|
const id = String(formData.get("id"));
|
||||||
await adminDB.deleteRow({
|
await d.deleteRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.services,
|
tableId: TABLES.services,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
@@ -190,7 +192,7 @@ export async function deleteService(formData: FormData) {
|
|||||||
// ─── Projects ────────────────────────────────────────────────────
|
// ─── Projects ────────────────────────────────────────────────────
|
||||||
|
|
||||||
export async function saveProject(formData: FormData) {
|
export async function saveProject(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = str(formData.get("id"));
|
const id = str(formData.get("id"));
|
||||||
const title = str(formData.get("title"));
|
const title = str(formData.get("title"));
|
||||||
if (!title) throw new Error("Başlık zorunlu");
|
if (!title) throw new Error("Başlık zorunlu");
|
||||||
@@ -211,14 +213,14 @@ export async function saveProject(formData: FormData) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
await adminDB.updateRow({
|
await d.updateRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.projects,
|
tableId: TABLES.projects,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await adminDB.createRow({
|
await d.createRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.projects,
|
tableId: TABLES.projects,
|
||||||
rowId: ID.unique(),
|
rowId: ID.unique(),
|
||||||
@@ -231,9 +233,9 @@ export async function saveProject(formData: FormData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteProject(formData: FormData) {
|
export async function deleteProject(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = String(formData.get("id"));
|
const id = String(formData.get("id"));
|
||||||
await adminDB.deleteRow({
|
await d.deleteRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.projects,
|
tableId: TABLES.projects,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
@@ -245,7 +247,7 @@ export async function deleteProject(formData: FormData) {
|
|||||||
// ─── Testimonials ────────────────────────────────────────────────
|
// ─── Testimonials ────────────────────────────────────────────────
|
||||||
|
|
||||||
export async function saveTestimonial(formData: FormData) {
|
export async function saveTestimonial(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = str(formData.get("id"));
|
const id = str(formData.get("id"));
|
||||||
const name = str(formData.get("name"));
|
const name = str(formData.get("name"));
|
||||||
if (!name) throw new Error("Ad zorunlu");
|
if (!name) throw new Error("Ad zorunlu");
|
||||||
@@ -264,14 +266,14 @@ export async function saveTestimonial(formData: FormData) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
await adminDB.updateRow({
|
await d.updateRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.testimonials,
|
tableId: TABLES.testimonials,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await adminDB.createRow({
|
await d.createRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.testimonials,
|
tableId: TABLES.testimonials,
|
||||||
rowId: ID.unique(),
|
rowId: ID.unique(),
|
||||||
@@ -283,9 +285,9 @@ export async function saveTestimonial(formData: FormData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteTestimonial(formData: FormData) {
|
export async function deleteTestimonial(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = String(formData.get("id"));
|
const id = String(formData.get("id"));
|
||||||
await adminDB.deleteRow({
|
await d.deleteRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.testimonials,
|
tableId: TABLES.testimonials,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
@@ -296,7 +298,7 @@ export async function deleteTestimonial(formData: FormData) {
|
|||||||
// ─── SEO Settings (singleton) ────────────────────────────────────
|
// ─── SEO Settings (singleton) ────────────────────────────────────
|
||||||
|
|
||||||
export async function saveSeoSettings(formData: FormData) {
|
export async function saveSeoSettings(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const data = {
|
const data = {
|
||||||
site_name: str(formData.get("site_name")),
|
site_name: str(formData.get("site_name")),
|
||||||
site_description: str(formData.get("site_description")),
|
site_description: str(formData.get("site_description")),
|
||||||
@@ -309,14 +311,14 @@ export async function saveSeoSettings(formData: FormData) {
|
|||||||
gtm_id: str(formData.get("gtm_id")),
|
gtm_id: str(formData.get("gtm_id")),
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
await adminDB.updateRow({
|
await d.updateRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.seoSettings,
|
tableId: TABLES.seoSettings,
|
||||||
rowId: "global",
|
rowId: "global",
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
} catch {
|
} catch {
|
||||||
await adminDB.createRow({
|
await d.createRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.seoSettings,
|
tableId: TABLES.seoSettings,
|
||||||
rowId: "global",
|
rowId: "global",
|
||||||
@@ -330,7 +332,7 @@ export async function saveSeoSettings(formData: FormData) {
|
|||||||
// ─── SEO Page (per path) ─────────────────────────────────────────
|
// ─── SEO Page (per path) ─────────────────────────────────────────
|
||||||
|
|
||||||
export async function saveSeoPage(formData: FormData) {
|
export async function saveSeoPage(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = str(formData.get("id"));
|
const id = str(formData.get("id"));
|
||||||
const path = str(formData.get("path"));
|
const path = str(formData.get("path"));
|
||||||
if (!path) throw new Error("Path zorunlu");
|
if (!path) throw new Error("Path zorunlu");
|
||||||
@@ -345,14 +347,14 @@ export async function saveSeoPage(formData: FormData) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
await adminDB.updateRow({
|
await d.updateRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.seoPages,
|
tableId: TABLES.seoPages,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await adminDB.createRow({
|
await d.createRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.seoPages,
|
tableId: TABLES.seoPages,
|
||||||
rowId: ID.unique(),
|
rowId: ID.unique(),
|
||||||
@@ -364,9 +366,9 @@ export async function saveSeoPage(formData: FormData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteSeoPage(formData: FormData) {
|
export async function deleteSeoPage(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = String(formData.get("id"));
|
const id = String(formData.get("id"));
|
||||||
await adminDB.deleteRow({
|
await d.deleteRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.seoPages,
|
tableId: TABLES.seoPages,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
@@ -377,14 +379,14 @@ export async function deleteSeoPage(formData: FormData) {
|
|||||||
// ─── Contact messages ────────────────────────────────────────────
|
// ─── Contact messages ────────────────────────────────────────────
|
||||||
|
|
||||||
export async function updateMessageStatus(formData: FormData) {
|
export async function updateMessageStatus(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = String(formData.get("id"));
|
const id = String(formData.get("id"));
|
||||||
const status = String(formData.get("status")) as
|
const status = String(formData.get("status")) as
|
||||||
| "new"
|
| "new"
|
||||||
| "read"
|
| "read"
|
||||||
| "replied"
|
| "replied"
|
||||||
| "archived";
|
| "archived";
|
||||||
await adminDB.updateRow({
|
await d.updateRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.contactMessages,
|
tableId: TABLES.contactMessages,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
@@ -394,9 +396,9 @@ export async function updateMessageStatus(formData: FormData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteMessage(formData: FormData) {
|
export async function deleteMessage(formData: FormData) {
|
||||||
await gate();
|
const d = await db();
|
||||||
const id = String(formData.get("id"));
|
const id = String(formData.get("id"));
|
||||||
await adminDB.deleteRow({
|
await d.deleteRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.contactMessages,
|
tableId: TABLES.contactMessages,
|
||||||
rowId: id,
|
rowId: id,
|
||||||
|
|||||||
+20
-14
@@ -1,9 +1,8 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { Account, Client, Storage, TablesDB } from "node-appwrite";
|
import { Account, Client, Storage, TablesDB } from "appwrite";
|
||||||
|
|
||||||
const endpoint = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!;
|
const endpoint = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!;
|
||||||
const projectId = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID!;
|
const projectId = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID!;
|
||||||
const apiKey = process.env.APPWRITE_API_KEY;
|
|
||||||
|
|
||||||
export const DATABASE_ID = process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID!;
|
export const DATABASE_ID = process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID!;
|
||||||
export const MEDIA_BUCKET_ID =
|
export const MEDIA_BUCKET_ID =
|
||||||
@@ -19,21 +18,28 @@ export const TABLES = {
|
|||||||
seoSettings: "seo_settings",
|
seoSettings: "seo_settings",
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export function adminClient() {
|
function newClient() {
|
||||||
const c = new Client().setEndpoint(endpoint).setProject(projectId);
|
return new Client().setEndpoint(endpoint).setProject(projectId);
|
||||||
if (apiKey) c.setKey(apiKey);
|
}
|
||||||
return c;
|
|
||||||
|
export function publicClient() {
|
||||||
|
return newClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sessionClient(sessionSecret: string) {
|
export function sessionClient(sessionSecret: string) {
|
||||||
return new Client()
|
return newClient().setSession(sessionSecret);
|
||||||
.setEndpoint(endpoint)
|
|
||||||
.setProject(projectId)
|
|
||||||
.setSession(sessionSecret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const adminDB = new TablesDB(adminClient());
|
export const publicDB = new TablesDB(publicClient());
|
||||||
export const adminStorage = new Storage(adminClient());
|
export const publicStorage = new Storage(publicClient());
|
||||||
export const adminAccount = new Account(adminClient());
|
export const publicAccount = new Account(publicClient());
|
||||||
|
|
||||||
export { Account, TablesDB, Storage };
|
export function userDB(secret: string) {
|
||||||
|
return new TablesDB(sessionClient(secret));
|
||||||
|
}
|
||||||
|
export function userStorage(secret: string) {
|
||||||
|
return new Storage(sessionClient(secret));
|
||||||
|
}
|
||||||
|
export function userAccount(secret: string) {
|
||||||
|
return new Account(sessionClient(secret));
|
||||||
|
}
|
||||||
|
|||||||
+14
-6
@@ -1,18 +1,20 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { Account } from "node-appwrite";
|
import { userAccount } from "@/lib/appwrite-server";
|
||||||
import { sessionClient } from "@/lib/appwrite-server";
|
|
||||||
|
|
||||||
export const SESSION_COOKIE = "kovak_session";
|
export const SESSION_COOKIE = "kovak_session";
|
||||||
|
|
||||||
export async function getCurrentUser() {
|
export async function getSessionSecret() {
|
||||||
const store = await cookies();
|
const store = await cookies();
|
||||||
const secret = store.get(SESSION_COOKIE)?.value;
|
return store.get(SESSION_COOKIE)?.value ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCurrentUser() {
|
||||||
|
const secret = await getSessionSecret();
|
||||||
if (!secret) return null;
|
if (!secret) return null;
|
||||||
try {
|
try {
|
||||||
const account = new Account(sessionClient(secret));
|
return await userAccount(secret).get();
|
||||||
return await account.get();
|
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -23,3 +25,9 @@ export async function requireUser() {
|
|||||||
if (!user) redirect("/admin/login");
|
if (!user) redirect("/admin/login");
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function requireSessionSecret() {
|
||||||
|
const secret = await getSessionSecret();
|
||||||
|
if (!secret) redirect("/admin/login");
|
||||||
|
return secret;
|
||||||
|
}
|
||||||
|
|||||||
+11
-10
@@ -1,6 +1,6 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { Query } from "node-appwrite";
|
import { Query } from "appwrite";
|
||||||
import { adminDB, DATABASE_ID, TABLES } from "@/lib/appwrite-server";
|
import { publicDB, DATABASE_ID, TABLES } from "@/lib/appwrite-server";
|
||||||
import type {
|
import type {
|
||||||
BlogPostRow,
|
BlogPostRow,
|
||||||
ContactMessageRow,
|
ContactMessageRow,
|
||||||
@@ -11,12 +11,9 @@ import type {
|
|||||||
TestimonialRow,
|
TestimonialRow,
|
||||||
} from "@/lib/types";
|
} from "@/lib/types";
|
||||||
|
|
||||||
async function safeList<T>(
|
async function safeList<T>(tableId: string, queries: string[]): Promise<T[]> {
|
||||||
tableId: string,
|
|
||||||
queries: string[],
|
|
||||||
): Promise<T[]> {
|
|
||||||
try {
|
try {
|
||||||
const res = await adminDB.listRows<T extends import("appwrite").Models.Row ? T : never>({
|
const res = await publicDB.listRows({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId,
|
tableId,
|
||||||
queries,
|
queries,
|
||||||
@@ -91,11 +88,11 @@ export async function listSeoPages() {
|
|||||||
|
|
||||||
export async function getSeoSettings(): Promise<SeoSettingsRow | null> {
|
export async function getSeoSettings(): Promise<SeoSettingsRow | null> {
|
||||||
try {
|
try {
|
||||||
return await adminDB.getRow<SeoSettingsRow>({
|
return (await publicDB.getRow({
|
||||||
databaseId: DATABASE_ID,
|
databaseId: DATABASE_ID,
|
||||||
tableId: TABLES.seoSettings,
|
tableId: TABLES.seoSettings,
|
||||||
rowId: "global",
|
rowId: "global",
|
||||||
});
|
})) as unknown as SeoSettingsRow;
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -103,7 +100,11 @@ export async function getSeoSettings(): Promise<SeoSettingsRow | null> {
|
|||||||
|
|
||||||
export async function getRow<T>(tableId: string, rowId: string): Promise<T | null> {
|
export async function getRow<T>(tableId: string, rowId: string): Promise<T | null> {
|
||||||
try {
|
try {
|
||||||
return (await adminDB.getRow({ databaseId: DATABASE_ID, tableId, rowId })) as T;
|
return (await publicDB.getRow({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId,
|
||||||
|
rowId,
|
||||||
|
})) as unknown as T;
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
-17
@@ -12,7 +12,6 @@
|
|||||||
"lucide-react": "^1.16.0",
|
"lucide-react": "^1.16.0",
|
||||||
"marked": "^18.0.4",
|
"marked": "^18.0.4",
|
||||||
"next": "16.2.6",
|
"next": "16.2.6",
|
||||||
"node-appwrite": "^25.0.0",
|
|
||||||
"react": "19.2.4",
|
"react": "19.2.4",
|
||||||
"react-dom": "19.2.4"
|
"react-dom": "19.2.4"
|
||||||
},
|
},
|
||||||
@@ -1599,22 +1598,6 @@
|
|||||||
"node": "^10 || ^12 || >=14"
|
"node": "^10 || ^12 || >=14"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/node-appwrite": {
|
|
||||||
"version": "25.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/node-appwrite/-/node-appwrite-25.0.0.tgz",
|
|
||||||
"integrity": "sha512-KpZ/3Ed8euz6r5CwjquElX3wRkNuiRuRQqjROiHK+feZ2ZX8HjjcF5IwrjTJYSNaYrmIwsZoex4L0ezzWjYWFg==",
|
|
||||||
"license": "BSD-3-Clause",
|
|
||||||
"dependencies": {
|
|
||||||
"json-bigint": "1.0.0",
|
|
||||||
"node-fetch-native-with-agent": "1.7.2"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/node-fetch-native-with-agent": {
|
|
||||||
"version": "1.7.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/node-fetch-native-with-agent/-/node-fetch-native-with-agent-1.7.2.tgz",
|
|
||||||
"integrity": "sha512-5MaOOCuJEvcckoz7/tjdx1M6OusOY6Xc5f459IaruGStWnKzlI1qpNgaAwmn4LmFYcsSlj+jBMk84wmmRxfk5g==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/picocolors": {
|
"node_modules/picocolors": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
"lucide-react": "^1.16.0",
|
"lucide-react": "^1.16.0",
|
||||||
"marked": "^18.0.4",
|
"marked": "^18.0.4",
|
||||||
"next": "16.2.6",
|
"next": "16.2.6",
|
||||||
"node-appwrite": "^25.0.0",
|
|
||||||
"react": "19.2.4",
|
"react": "19.2.4",
|
||||||
"react-dom": "19.2.4"
|
"react-dom": "19.2.4"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user