Files
knur-app/app/settings/actions.ts

29 lines
898 B
TypeScript
Raw Normal View History

2026-06-18 11:02:31 +02:00
"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<SaveGarminCredentialsState> {
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 };
}