diff --git a/app/login/actions.ts b/app/login/actions.ts new file mode 100644 index 0000000..84b4fdd --- /dev/null +++ b/app/login/actions.ts @@ -0,0 +1,15 @@ +"use server"; + +import { redirect } from "next/navigation"; +import { getUserByEmail, verifyPassword, createSession } from "../../lib/auth"; + +export async function login(formData: FormData) { + const email = formData.get("email") as string; + const password = formData.get("password") as string; + const user = getUserByEmail(email); + if (!user || !verifyPassword(password, user.password_hash)) { + redirect("/login?error=1"); + } + await createSession({ userId: user.id, email: user.email, name: user.name }); + redirect("/tenants"); +} diff --git a/app/login/page.tsx b/app/login/page.tsx index d37fb66..bb0584c 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -1,8 +1,6 @@ -"use server"; - import { redirect } from "next/navigation"; -import { getUserByEmail, verifyPassword, createSession } from "../../lib/auth"; import { getSession } from "../../lib/auth"; +import { login } from "./actions"; export default async function LoginPage({ searchParams, @@ -21,17 +19,7 @@ export default async function LoginPage({ Rikx Beheer
{ - "use server"; - const email = formData.get("email") as string; - const password = formData.get("password") as string; - const user = getUserByEmail(email); - if (!user || !verifyPassword(password, user.password_hash)) { - redirect("/login?error=1"); - } - await createSession({ userId: user.id, email: user.email, name: user.name }); - redirect("/tenants"); - }} + action={login} className="flex flex-col gap-4 bg-white dark:bg-zinc-900 p-8 rounded-xl border border-zinc-200 dark:border-zinc-800" > {error && ( diff --git a/app/tenants/logout.ts b/app/tenants/logout.ts new file mode 100644 index 0000000..6f31376 --- /dev/null +++ b/app/tenants/logout.ts @@ -0,0 +1,9 @@ +"use server"; + +import { redirect } from "next/navigation"; +import { destroySession } from "../../lib/auth"; + +export async function logout() { + await destroySession(); + redirect("/login"); +} diff --git a/app/tenants/page.tsx b/app/tenants/page.tsx index 07962d5..4634725 100644 --- a/app/tenants/page.tsx +++ b/app/tenants/page.tsx @@ -1,7 +1,8 @@ import { redirect } from "next/navigation"; -import { getSession, destroySession } from "../../lib/auth"; +import { getSession } from "../../lib/auth"; import { listAccounts } from "../../lib/platform-db"; import { createTenant, deleteTenant } from "./actions"; +import { logout } from "./logout"; export default async function TenantsPage({ searchParams, @@ -28,13 +29,7 @@ export default async function TenantsPage({ Rikx Beheer
{session.email} - { - "use server"; - await destroySession(); - redirect("/login"); - }} - > +