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

Feature/enforce password strength #170

Merged
merged 2 commits into from
Oct 16, 2024
Merged
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
11 changes: 10 additions & 1 deletion frontend/components/auth/reset-password-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useState } from "react";
import { useRouter } from "next/navigation";
import { resetPassword } from "@/lib/reset-password";
import { isPasswordComplex } from "@/lib/password";
import { useToast } from "@/components/hooks/use-toast";

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

const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
// TODO: Add validation for password
if (password !== passwordConfirmation) {
toast({
title: "Password Mismatch",
description: "The passwords you entered do not match",
});
return;
}
if (!isPasswordComplex(passwordConfirmation)) {
toast({
title: "Weak Password",
description:
"Password must be at least 8 characters long, include 1 uppercase letter and 1 special character.",
});
return;
}

const res = await resetPassword(token, password);
if (!res.ok) {
toast({
Expand Down
9 changes: 9 additions & 0 deletions frontend/components/auth/sign-up-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useState } from "react";
import { useRouter } from "next/navigation";
import { toast } from "@/components/hooks/use-toast";
import { signUp } from "@/lib/signup";
import { isPasswordComplex } from "@/lib/password";

import { Button } from "@/components/ui/button";
import {
Expand Down Expand Up @@ -34,6 +35,14 @@ export function SignUpForm() {
});
return;
}
if (!isPasswordComplex(passwordConfirmation)) {
toast({
title: "Weak Password",
description:
"Password must be at least 8 characters long, include 1 uppercase letter and 1 special character.",
});
return;
}
const res = await signUp(username, email, password);
if (!res.ok) {
toast({
Expand Down
11 changes: 1 addition & 10 deletions frontend/components/user-settings/user-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import LoadingScreen from "@/components/common/loading-screen";
import { useAuth } from "@/app/auth/auth-context";
import { cn } from "@/lib/utils";
import { User, UserSchema } from "@/lib/schemas/user-schema";
import { isPasswordComplex } from "@/lib/password";
import { userServiceUri } from "@/lib/api-uri";

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

const isPasswordComplex = (password: string) => {
const minLength = 8;
const hasUpperCase = /[A-Z]/.test(password);
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/.test(
password
);

return password.length >= minLength && hasUpperCase && hasSpecialChar;
};

if (isLoading) {
return <LoadingScreen />;
}
Expand Down
9 changes: 9 additions & 0 deletions frontend/lib/password.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const isPasswordComplex = (password: string) => {
const minLength = 8;
const hasUpperCase = /[A-Z]/.test(password);
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/.test(
password
);

return password.length >= minLength && hasUpperCase && hasSpecialChar;
};