Skip to content

Commit

Permalink
Merge pull request #1904 from Dyuti01/signup_bug_zod_validate_passwor…
Browse files Browse the repository at this point in the history
…d_before_sending_otp

Fix: applying zod validation of password before sending OTP in signup.
  • Loading branch information
steven-tey authored Jan 20, 2025
2 parents c7dd958 + 9290b2c commit 17c2a93
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
2 changes: 0 additions & 2 deletions apps/web/lib/actions/create-user-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,4 @@ export const createUserAccountAction = actionClient
},
});
}

return { ok: true };
});
6 changes: 3 additions & 3 deletions apps/web/lib/actions/send-otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { getIP } from "../api/utils";
import { generateOTP } from "../auth";
import { EMAIL_OTP_EXPIRY_IN } from "../auth/constants";
import z from "../zod";
import { emailSchema } from "../zod/schemas/auth";
import { emailSchema, passwordSchema } from "../zod/schemas/auth";
import { throwIfAuthenticated } from "./auth/throw-if-authenticated";
import { actionClient } from "./safe-action";

const schema = z.object({
email: emailSchema,
password: passwordSchema.optional(),
});

// Send OTP to email to verify account
Expand Down Expand Up @@ -83,6 +84,7 @@ export const sendOtpAction = actionClient
expires: new Date(Date.now() + EMAIL_OTP_EXPIRY_IN * 1000),
},
}),

sendEmail({
subject: `${process.env.NEXT_PUBLIC_APP_NAME}: OTP to verify your account`,
email,
Expand All @@ -92,6 +94,4 @@ export const sendOtpAction = actionClient
}),
}),
]);

return { ok: true };
});
61 changes: 31 additions & 30 deletions apps/web/ui/auth/register/signup-email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,40 @@ export const SignUpEmail = () => {
setStep("verify");
},
onError: ({ error }) => {
toast.error(error.serverError);
toast.error(
error.serverError ||
error.validationErrors?.email?.[0] ||
error.validationErrors?.password?.[0],
);
},
});

return (
<>
<form
onSubmit={handleSubmit((data) => executeAsync({ email: data.email }))}
>
<div className="flex flex-col space-y-4">
<Input
type="email"
placeholder="Work Email"
autoComplete="email"
required
{...register("email")}
error={errors.email?.message}
/>
<Input
type="password"
placeholder="Password"
required
{...register("password")}
error={errors.password?.message}
/>
<Button
type="submit"
text={isExecuting ? "Submitting..." : "Sign Up"}
disabled={isExecuting}
loading={isExecuting}
/>
</div>
</form>
</>
<form onSubmit={handleSubmit(async (data) => await executeAsync(data))}>
<div className="flex flex-col space-y-4">
<Input
type="email"
placeholder="Work Email"
autoComplete="email"
required
{...register("email")}
error={errors.email?.message}
/>
<Input
type="password"
placeholder="Password"
required
{...register("password")}
error={errors.password?.message}
minLength={8}
/>
<Button
type="submit"
text={isExecuting ? "Submitting..." : "Sign Up"}
disabled={isExecuting}
loading={isExecuting}
/>
</div>
</form>
);
};

0 comments on commit 17c2a93

Please sign in to comment.