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