init
This commit is contained in:
28
app/settings/actions.ts
Normal file
28
app/settings/actions.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
"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 };
|
||||
}
|
||||
Reference in New Issue
Block a user