b
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
"use server";
|
||||
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export type ActionState = { error: string } | null;
|
||||
|
||||
export async function login(prevState: ActionState, formData: FormData): Promise<ActionState> {
|
||||
const username = formData.get("username");
|
||||
const password = formData.get("password");
|
||||
|
||||
if (
|
||||
username === process.env.ADMIN_USERNAME &&
|
||||
password === process.env.ADMIN_PASSWORD
|
||||
) {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set("admin_session", "true", {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
maxAge: 60 * 60 * 24 * 7, // 1 week
|
||||
path: "/",
|
||||
});
|
||||
} else {
|
||||
return { error: "Geçersiz kullanıcı adı veya şifre" };
|
||||
}
|
||||
|
||||
redirect("/admin");
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.delete("admin_session");
|
||||
redirect("/admin/login");
|
||||
}
|
||||
Reference in New Issue
Block a user