Skip to content

Commit dbbe5ac

Browse files
authored
Merge pull request #170 from CS3219-AY2425S1/feature/enforce-password
2 parents 3259ce0 + 800f168 commit dbbe5ac

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

frontend/components/auth/reset-password-form.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useState } from "react";
44
import { useRouter } from "next/navigation";
55
import { resetPassword } from "@/lib/reset-password";
6+
import { isPasswordComplex } from "@/lib/password";
67
import { useToast } from "@/components/hooks/use-toast";
78

89
import { Button } from "@/components/ui/button";
@@ -24,14 +25,22 @@ export function ResetPasswordForm({ token }: { token: string }) {
2425

2526
const handleSubmit = async (event: React.FormEvent) => {
2627
event.preventDefault();
27-
// TODO: Add validation for password
2828
if (password !== passwordConfirmation) {
2929
toast({
3030
title: "Password Mismatch",
3131
description: "The passwords you entered do not match",
3232
});
3333
return;
3434
}
35+
if (!isPasswordComplex(passwordConfirmation)) {
36+
toast({
37+
title: "Weak Password",
38+
description:
39+
"Password must be at least 8 characters long, include 1 uppercase letter and 1 special character.",
40+
});
41+
return;
42+
}
43+
3544
const res = await resetPassword(token, password);
3645
if (!res.ok) {
3746
toast({

frontend/components/auth/sign-up-form.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useState } from "react";
55
import { useRouter } from "next/navigation";
66
import { toast } from "@/components/hooks/use-toast";
77
import { signUp } from "@/lib/signup";
8+
import { isPasswordComplex } from "@/lib/password";
89

910
import { Button } from "@/components/ui/button";
1011
import {
@@ -34,6 +35,14 @@ export function SignUpForm() {
3435
});
3536
return;
3637
}
38+
if (!isPasswordComplex(passwordConfirmation)) {
39+
toast({
40+
title: "Weak Password",
41+
description:
42+
"Password must be at least 8 characters long, include 1 uppercase letter and 1 special character.",
43+
});
44+
return;
45+
}
3746
const res = await signUp(username, email, password);
3847
if (!res.ok) {
3948
toast({

frontend/components/user-settings/user-settings.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import LoadingScreen from "@/components/common/loading-screen";
2424
import { useAuth } from "@/app/auth/auth-context";
2525
import { cn } from "@/lib/utils";
2626
import { User, UserSchema } from "@/lib/schemas/user-schema";
27+
import { isPasswordComplex } from "@/lib/password";
2728
import { userServiceUri } from "@/lib/api-uri";
2829

2930
const fetcher = async (url: string): Promise<User> => {
@@ -300,16 +301,6 @@ export default function UserSettings({ userId }: { userId: string }) {
300301
}
301302
}, [newPassword, confirmPassword]);
302303

303-
const isPasswordComplex = (password: string) => {
304-
const minLength = 8;
305-
const hasUpperCase = /[A-Z]/.test(password);
306-
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/.test(
307-
password
308-
);
309-
310-
return password.length >= minLength && hasUpperCase && hasSpecialChar;
311-
};
312-
313304
if (isLoading) {
314305
return <LoadingScreen />;
315306
}

frontend/lib/password.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const isPasswordComplex = (password: string) => {
2+
const minLength = 8;
3+
const hasUpperCase = /[A-Z]/.test(password);
4+
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/.test(
5+
password
6+
);
7+
8+
return password.length >= minLength && hasUpperCase && hasSpecialChar;
9+
};

0 commit comments

Comments
 (0)