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

added dynamic validation in auth pages #224

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 42 additions & 4 deletions src/app/auth/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export default function Auth() {
const searchParams = useSearchParams();
const callback = searchParams.get("callbackUrl");

const [passwordFeedback, setPasswordFeedback] = useState({
length: false,
uppercase: false,
lowercase: false,
number: false,
specialChar: false,
});

const {
register,
handleSubmit,
Expand All @@ -35,6 +43,16 @@ export default function Auth() {

console.log(callback);

const validatePassword = (password: string) => {
setPasswordFeedback({
length: password.length >= 8,
uppercase: /[A-Z]/.test(password),
lowercase: /[a-z]/.test(password),
number: /\d/.test(password),
specialChar: /[@$!%*?&]/.test(password),
});
};

const onSubmit = async (data: any) => {
if (isSignup) {
try {
Expand Down Expand Up @@ -171,7 +189,7 @@ export default function Auth() {
type={hidden ? "password" : "text"}
placeholder="Password"
disabled={isLoading}
{...register("password")}
{...register("password", {onChange:(e)=>validatePassword(e.target.value)})}
className={styles.input}
/>
<button className={styles.passwordToggle} onClick={(e)=>{
Expand Down Expand Up @@ -242,7 +260,7 @@ export default function Auth() {
<input
type={hidden? "password" : "text"}
placeholder="Password"
{...register("password")}
{...register("password", {onChange:(e)=>validatePassword(e.target.value)})}
className={styles.input}
disabled={isLoading}
/>
Expand All @@ -255,13 +273,33 @@ export default function Auth() {
}
</button>
</div>
{errors.password && (
{/* {errors.password && (
<p className={styles.errors}>
{errors.password.message?.toString()}
</p>
)}
)} */}
</>
)}

{/* Password criteria feedback */}
<div className={styles.passwordCriteria}>
<p style={{ color: passwordFeedback.length ? "green" : "red" }}>
{passwordFeedback.length ? "✔️ At least 8 characters" : "❌ At least 8 characters"}
</p>
<p style={{ color: passwordFeedback.uppercase ? "green" : "red" }}>
{passwordFeedback.uppercase ? "✔️ At least 1 uppercase letter" : "❌ At least 1 uppercase letter"}
</p>
<p style={{ color: passwordFeedback.lowercase ? "green" : "red" }}>
{passwordFeedback.lowercase ? "✔️ At least 1 lowercase letter" : "❌ At least 1 lowercase letter"}
</p>
<p style={{ color: passwordFeedback.number ? "green" : "red" }}>
{passwordFeedback.number ? "✔️ At least 1 number" : "❌ At least 1 number"}
</p>
<p style={{ color: passwordFeedback.specialChar ? "green" : "red" }}>
{passwordFeedback.specialChar ? "✔️ At least 1 special character" : "❌ At least 1 special character"}
</p>
</div>

<button className={styles.button} disabled={isLoading} type="submit">
{isSignup ? "Sign Up" : "Login"}
</button>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/zod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { object, string, z } from "zod";

const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
// const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$/;

export const loginSchema = z.object({
loginIdentifier: z.string().min(1, "Email or Username is required"),
Expand Down