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 password strength meter #260

Merged
merged 5 commits into from
Oct 13, 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
3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"react-router-dom": "^6.24.1",
"split-type": "^0.3.4",
"tailwind-merge": "^2.5.2",
"tailwindcss": "^3.4.4"
"tailwindcss": "^3.4.4",
"zxcvbn": "^4.4.2"
},
"devDependencies": {
"@types/react": "^18.3.3",
Expand Down
27 changes: 26 additions & 1 deletion frontend/src/components/Pages/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { useNavigate } from "react-router-dom";
import { Link } from "react-router-dom";
import { FaEye } from "react-icons/fa";
import { FaEyeSlash } from "react-icons/fa6";
import zxcvbn from "zxcvbn"; // Import zxcvbn for password strength check

const Signup = () => {
const API_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000';
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const [passwordStrength, setPasswordStrength] = useState(0); // State to track password strength
const [data, setData] = useState({
name: '',
email: '',
Expand All @@ -20,12 +22,16 @@ const Signup = () => {

const handleChange = (e) => {
setData({ ...data, [e.target.name]: e.target.value });

if (e.target.name === "password") {
const result = zxcvbn(e.target.value);
setPasswordStrength(result.score); // Update password strength score
}
};

const handleSubmit = async (e) => {
e.preventDefault();
setIsLoading(true);
// Add input validation
if (!data.email || !data.password || !data.name) {
setError('Please fill in all fields');
setIsLoading(false);
Expand Down Expand Up @@ -79,6 +85,16 @@ const Signup = () => {
window.scrollTo(0, 0);
}, []);

const getPasswordStrengthColor = (score) => {
const colors = ["#ff4d4d", "#ff944d", "#ffd24d", "#d2ff4d", "#4dff88"];
return colors[score];
};

const getPasswordStrengthText = (score) => {
const strengthLevels = ["Very Weak", "Weak", "Okay", "Good", "Strong"];
return strengthLevels[score];
};

return (
<div className="w-screen h-screen flex items-center justify-center pt-10">
<img src={photo} alt="login" loading="lazy" className=" w-3/4 absolute" />
Expand Down Expand Up @@ -125,6 +141,15 @@ const Signup = () => {
{hidden ? <FaEyeSlash/> : <FaEye/>}
</button>
</div>

{/* Password Strength Meter */}
<div className="w-full mt-2">
<div className="h-2 rounded-full" style={{ backgroundColor: getPasswordStrengthColor(passwordStrength), width: `${(passwordStrength + 1) * 20}%` }}></div>
<p className="text-sm text-[#666] mt-1">
Strength: {getPasswordStrengthText(passwordStrength)}
</p>
</div>

{error && (
<div className="w-full p-2 bg-red-100 text-red-700 border border-red-400 rounded-md">
{error}
Expand Down
Loading