Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions frontend/src/ts/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PasswordSchema } from "@monkeytype/schemas/users";
import { NewPasswordSchema, PasswordSchema } from "@monkeytype/schemas/users";
import { tryCatch } from "@monkeytype/util/trycatch";
import { FirebaseError } from "firebase/app";
import {
Expand Down Expand Up @@ -578,8 +578,20 @@ export function isUsingAuthenticationReactive(authMethod: AuthMethod): boolean {
return authenticationMemos[authMethod]?.()?.isInUse ?? false;
}

export function getPasswordSchema(): ZodString {
return isDevEnvironment() ? z.string().min(6) : PasswordSchema;
/**
* Returns the Zod schema for password validation.
*
* Set `isNew: true` for registration/creation flows (strict rules).
* Omit it for re-authentication flows (lenient: just non-empty).
*
* @param options - Set `isNew: true` for password creation/registration.
* @returns A Zod string schema.
*/
export function getPasswordSchema(options?: { isNew: boolean }): ZodString {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move the isdevenv guard to the top?

if (isDevEnv) return z.string().min6

if (options?.isNew) {
return isDevEnvironment() ? z.string().min(6) : NewPasswordSchema;
}
return PasswordSchema;
}

export function isUsingPasswordAuthentication(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export function showAddPasswordAuthModal(): void {
schema: z.object({
email: UserEmailSchema,
emailConfirm: UserEmailSchema,
password: getPasswordSchema(),
passConfirm: getPasswordSchema(),
password: getPasswordSchema({ isNew: true }),
passConfirm: getPasswordSchema({ isNew: true }),
}),
inputs: {
email: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export function showUpdatePasswordModal(): void {
showSimpleModal({
title: "Update password",
schema: z.object({
previousPass: z.string().min(1, "Current password is required"),
newPassword: getPasswordSchema(),
newPassConfirm: getPasswordSchema(),
previousPass: getPasswordSchema(),
newPassword: getPasswordSchema({ isNew: true }),
newPassConfirm: getPasswordSchema({ isNew: true }),
}),
inputs: {
previousPass: {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/components/pages/login/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export function Register(): JSXElement {
<form.Field
name="password"
validators={{
onChange: fromSchema(getPasswordSchema()),
onChange: fromSchema(getPasswordSchema({ isNew: true })),
}}
children={(field) => (
<InputField
Expand Down
7 changes: 6 additions & 1 deletion packages/schemas/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ export const ReportUserReasonSchema = z.enum([
]);
export type ReportUserReason = z.infer<typeof ReportUserReasonSchema>;

export const PasswordSchema = z
// stricter schema used while password creation
export const NewPasswordSchema = z
.string()
.min(8, { message: "must be at least 8 characters" })
.max(64, { message: "must be at most 64 characters" })
Expand All @@ -374,6 +375,10 @@ export const PasswordSchema = z
.regex(/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/, {
message: "must contain at least one special character",
});
export type NewPassword = z.infer<typeof NewPasswordSchema>;

// lenient schema for existing passwords
export const PasswordSchema = z.string().min(1, "Required");
export type Password = z.infer<typeof PasswordSchema>;

export const FriendSchema = UserSchema.pick({
Expand Down
Loading