Server Action
A plain form posting to a Server Action that checks the same policy.
← All examples·server-action
Preview
// app/signup/actions.ts
"use server";
import { validatePassword } from "@virastack/password/server";
import type { PasswordIssue } from "@virastack/password/strength";
import { policy } from "@/lib/password-policy";
export type SignupState = { ok: boolean; issues: PasswordIssue[] } | null;
export async function signup(_state: SignupState, formData: FormData): Promise<SignupState> {
// The browser can be skipped; the server checks again with the same policy.
const result = validatePassword(formData.get("password"), policy, {
forbidden: [formData.get("email")],
confirm: formData.get("confirm"),
});
if (!result.valid) return { ok: false, issues: result.issues };
// await createUser(email, await hash(result.password)) — result.password is NFKC-normalized
return { ok: true, issues: [] };
}Details
- validatePassword reads FormData values directly.
- forbidden and confirm are checked on the server too.
- Hash result.password, the NFKC-normalized value.
For the hook API see useViraPassword(), for styling and icons see customization.
