-
Notifications
You must be signed in to change notification settings - Fork 30
Add Password Strength Validation During User Registration #521
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,31 +30,41 @@ const getGoogleClient = () => { | |||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| export const signup = catchAsync(async (req, res) => { | ||||||||||||||||||||||||||||
| const { name, email, password } = req.body; | ||||||||||||||||||||||||||||
| const existing = await User.findOne({ email }); | ||||||||||||||||||||||||||||
| if (existing) { | ||||||||||||||||||||||||||||
| return res.status(409).json({ message: "An account with this email already exists" }); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Secure password hashing with 10 salt rounds | ||||||||||||||||||||||||||||
| const hashedPassword = await bcrypt.hash(password, 10); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Sanitize string attributes before insertion | ||||||||||||||||||||||||||||
| const user = await User.create({ | ||||||||||||||||||||||||||||
| name: name.trim(), | ||||||||||||||||||||||||||||
| email: email.toLowerCase().trim(), | ||||||||||||||||||||||||||||
| password: hashedPassword | ||||||||||||||||||||||||||||
| // Password Strength Validation | ||||||||||||||||||||||||||||
| const passwordRegex = | ||||||||||||||||||||||||||||
| /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&^#()[\]{}\-_=+|\\:;"'<>,./~`]).{8,}$/; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (!passwordRegex.test(password)) { | ||||||||||||||||||||||||||||
| return res.status(400).json({ | ||||||||||||||||||||||||||||
| message: | ||||||||||||||||||||||||||||
| "Password must be at least 8 characters long and contain uppercase, lowercase, number, and special character", | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Set secure HTTP-Only authentication cookie | ||||||||||||||||||||||||||||
| generateTokenAndSetCookie(user._id, res); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| res.status(201).json({ | ||||||||||||||||||||||||||||
| _id: user._id, | ||||||||||||||||||||||||||||
| name: user.name, | ||||||||||||||||||||||||||||
| email: user.email, | ||||||||||||||||||||||||||||
| profilePicture: user.profilePicture, | ||||||||||||||||||||||||||||
| statusMood: user.statusMood || null, | ||||||||||||||||||||||||||||
| const existing = await User.findOne({ email }); | ||||||||||||||||||||||||||||
| if (existing) { | ||||||||||||||||||||||||||||
| return res.status(409).json({ | ||||||||||||||||||||||||||||
| message: "An account with this email already exists", | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const hashedPassword = await bcrypt.hash(password, 10); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const user = await User.create({ | ||||||||||||||||||||||||||||
| name: name.trim(), | ||||||||||||||||||||||||||||
| email: email.toLowerCase().trim(), | ||||||||||||||||||||||||||||
| password: hashedPassword, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| generateTokenAndSetCookie(user._id, res); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| res.status(201).json({ | ||||||||||||||||||||||||||||
| _id: user._id, | ||||||||||||||||||||||||||||
| name: user.name, | ||||||||||||||||||||||||||||
| email: user.email, | ||||||||||||||||||||||||||||
| profilePicture: user.profilePicture, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
Comment on lines
+62
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API contract inconsistency: Response payload omits The Impact: 🔧 Proposed fix for consistency res.status(201).json({
_id: user._id,
name: user.name,
email: user.email,
profilePicture: user.profilePicture,
+ statusMood: user.statusMood || null,
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Email duplicate check must normalize the email consistently with user creation.
The duplicate check queries using the raw
emailparameter, but line 56 creates the user withemail.toLowerCase().trim(). This normalization mismatch allows the duplicate check to pass for emails that differ only in casing (e.g., "Test@Example.com" vs "test@example.com"), which will either:🔧 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents