"use server"; import { revalidatePath } from "next/cache"; import { saveGarminCredentials } from "@/lib/models/garmin-auth"; import { getCurrentUserId } from "@/lib/session"; export type SaveGarminCredentialsState = { error: string } | { success: true } | null; export async function saveGarminCredentialsAction( _prevState: SaveGarminCredentialsState, formData: FormData ): Promise { const email = formData.get("email"); const password = formData.get("password"); if (typeof email !== "string" || !email.includes("@")) { return { error: "Podaj prawidłowy adres e-mail." }; } if (typeof password !== "string" || password.length < 1) { return { error: "Podaj hasło." }; } const userId = await getCurrentUserId(); await saveGarminCredentials(userId, email.trim(), password); revalidatePath("/settings"); return { success: true }; }