Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display zod errors on loginWithCredentials page #87

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ import { useRouter } from "next/router";

const email_z = z.string().email().min(1);
const password_z = z.string().min(6);
const createUserSchema_z = z.object({
email: email_z,
password: password_z,
});
type CreateUserSchemaData = z.infer<typeof createUserSchema_z>;

type Props = {
type LoginWithCredentialsProps = {
className?: string;
onError: (e: Error) => void;
setIsLoading: (isLoading: boolean) => void;
};
const LoginWithCredentials = (props: Props) => {
const LoginWithCredentials = (props: LoginWithCredentialsProps) => {
const router = useRouter();
const createUserInput_z = z.object({
email: email_z,
password: password_z,
});

const { register, handleSubmit } = useForm({
resolver: zodResolver(createUserInput_z),
const {
register,
handleSubmit,
formState: { errors },
} = useForm<CreateUserSchemaData>({
resolver: zodResolver(createUserSchema_z),
});

const handleSignIn = handleSubmit(async (formData) => {
Expand All @@ -37,8 +42,8 @@ const LoginWithCredentials = (props: Props) => {
duration: DURATION_TO_LOAD,
});
return setTimeout(() => {
// don't reload the page for a while to show the toast
// reload is necessary to have ctx.session be updated
// Don't reload the page straightaway so that the toast can be seen.
// Reload is necessary to have ctx.session be updated
void router.push("/").then(() => window.location.reload());
}, DURATION_TO_LOAD);
} else {
Expand All @@ -64,6 +69,11 @@ const LoginWithCredentials = (props: Props) => {
placeholder="[email protected]"
{...register("email")}
/>
{errors.email && (
<span className="text-sm lowercase text-red-300">
{errors.email.message}
</span>
)}
</div>
<div>
<label className="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
Expand All @@ -75,6 +85,11 @@ const LoginWithCredentials = (props: Props) => {
placeholder="••••••••"
{...register("password")}
/>
{errors.password && (
<span className="text-sm lowercase text-red-300">
{errors.password.message}
</span>
)}
</div>
<div className="py-1">
<input
Expand Down
Loading