Fix some issues with signing in

This commit is contained in:
2025-09-05 12:41:27 -05:00
parent adef703f92
commit 5ca78bd92c
3 changed files with 40 additions and 25 deletions

View File

@@ -14,6 +14,10 @@ export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
providers: [Password], providers: [Password],
}); });
export const PASSWORD_MIN = 8;
export const PASSWORD_MAX = 100;
export const PASSWORD_REGEX = /^(?=.{8,100}$)(?!.*\s)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\p{P}\p{S}]).*$/u;
export const getUser = query(async (ctx) => { export const getUser = query(async (ctx) => {
const userId = await getAuthUserId(ctx); const userId = await getAuthUserId(ctx);
if (!userId) return null; if (!userId) return null;

View File

@@ -24,16 +24,14 @@ import {
TabsTrigger, TabsTrigger,
} from '@/components/ui'; } from '@/components/ui';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { PASSWORD_MIN, PASSWORD_MAX, PASSWORD_REGEX } from '~/convex/auth';
const signInFormSchema = z.object({ const signInFormSchema = z.object({
email: z.email({ email: z.email({
message: 'Please enter a valid email address.', message: 'Please enter a valid email address.',
}), }),
password: z password: z.string()
.string() .regex(PASSWORD_REGEX, {
.regex(
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\S+$).{8,100}$/,
{
message: 'Incorrect password. Does not meet requirements.', message: 'Incorrect password. Does not meet requirements.',
}, },
), ),
@@ -44,25 +42,35 @@ const signUpFormSchema = z
name: z.string().min(2, { name: z.string().min(2, {
message: 'Name must be at least 2 characters.', message: 'Name must be at least 2 characters.',
}), }),
email: z.email({ email: z.string().email({
message: 'Please enter a valid email address.', message: 'Please enter a valid email address.',
}), }),
password: z password: z
.string() .string()
.min(8, { message: 'Password must be at least 8 characters.' }) .min(PASSWORD_MIN, {
.max(100, { message: 'Password must be no more than 100 characters.' }) message: `Password must be at least ${PASSWORD_MIN} characters.`,
.regex(/[0-9]/, { message: 'Password must contain at least one digit.' }) })
.max(PASSWORD_MAX, {
message:
`Password must be no more than ${PASSWORD_MAX} characters.`,
})
.regex(/^\S+$/, {
message: 'Password must not contain whitespace.',
})
.regex(/[0-9]/, {
message: 'Password must contain at least one digit.',
})
.regex(/[a-z]/, { .regex(/[a-z]/, {
message: 'Password must contain at least one lowercase letter.', message: 'Password must contain at least one lowercase letter.',
}) })
.regex(/[A-Z]/, { .regex(/[A-Z]/, {
message: 'Password must contain at least one uppercase letter.', message: 'Password must contain at least one uppercase letter.',
}) })
.regex(/[@#$%^&+=]/, { .regex(/[\p{P}\p{S}]/u, {
message: 'Password must contain at least one special character.', message: 'Password must contain at least one symbol.',
}), }),
confirmPassword: z.string().min(8, { confirmPassword: z.string().min(PASSWORD_MIN, {
message: 'Password must be at least 8 characters.', message: `Password must be at least ${PASSWORD_MIN} characters.`,
}), }),
}) })
.refine((data) => data.password === data.confirmPassword, { .refine((data) => data.password === data.confirmPassword, {
@@ -70,7 +78,7 @@ const signUpFormSchema = z
path: ['confirmPassword'], path: ['confirmPassword'],
}); });
export default function SignIn() { const SignIn = () => {
const { signIn } = useAuthActions(); const { signIn } = useAuthActions();
const [flow, setFlow] = useState<'signIn' | 'signUp'>('signIn'); const [flow, setFlow] = useState<'signIn' | 'signUp'>('signIn');
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@@ -317,4 +325,5 @@ export default function SignIn() {
</Card> </Card>
</div> </div>
); );
} };
export default SignIn;

View File

@@ -21,32 +21,34 @@ import {
SubmitButton, SubmitButton,
} from '@/components/ui'; } from '@/components/ui';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { PASSWORD_MIN, PASSWORD_MAX, PASSWORD_REGEX } from '~/convex/auth';
const formSchema = z const formSchema = z
.object({ .object({
currentPassword: z currentPassword: z
.string() .string()
.regex( .regex(PASSWORD_REGEX, {
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\S+$).{8,100}$/,
{
message: 'Incorrect current password. Does not meet requirements.', message: 'Incorrect current password. Does not meet requirements.',
}, },
), ),
newPassword: z newPassword: z
.string() .string()
.min(8, { message: 'New password must be at least 8 characters.' }) .min(PASSWORD_MIN, { message: 'New password must be at least 8 characters.' })
.max(100, { message: 'New password must be less than 100 characters.' }) .max(PASSWORD_MAX, { message: 'New password must be less than 100 characters.' })
.regex(/^\S+$/, {
message: 'Password must not contain whitespace.',
})
.regex(/[0-9]/, { .regex(/[0-9]/, {
message: 'New password must contain at least one digit.', message: 'Password must contain at least one digit.',
}) })
.regex(/[a-z]/, { .regex(/[a-z]/, {
message: 'New password must contain at least one lowercase letter.', message: 'Password must contain at least one lowercase letter.',
}) })
.regex(/[A-Z]/, { .regex(/[A-Z]/, {
message: 'New password must contain at least one uppercase letter.', message: 'Password must contain at least one uppercase letter.',
}) })
.regex(/[@#$%^&+=]/, { .regex(/[\p{P}\p{S}]/u, {
message: 'New password must contain at least one special character.', message: 'Password must contain at least one symbol.',
}), }),
confirmPassword: z.string(), confirmPassword: z.string(),
}) })