Skip to content

Commit

Permalink
validator added
Browse files Browse the repository at this point in the history
  • Loading branch information
sakibb77 committed Feb 25, 2023
1 parent 4f88e6d commit 1d134c7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions controllers/projectsController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Project = require("../models/projectModel");
const mongoose = require("mongoose");
const { findOneAndDelete } = require("../models/projectModel");

//get all projects
const getAllProjects = async (req, res) => {
const projects = await Project.find({}).sort({ createdAt: -1 });
Expand Down
38 changes: 37 additions & 1 deletion models/usermodel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const validator = require("validator");

const Schema = mongoose.Schema;

Expand All @@ -15,11 +16,46 @@ const userSchema = new Schema({
},
});

//password requirements massege
const passRequirementsMessage = () => {
return `<div>
<p>
Your password is invalid. For security reasons, please choose a
stronger password that meets the following requirements:
</p>
<ul>
<li>Must be at least 8 characters long</li>
<li>Must contain a combination of uppercase and lowercase letters</li>
<li>
Must include at least one number and one special character (e.g.
!,@,#,$,%)
</li>
</ul>
<p>Please try again with a stronger password.</p>
</div>`;
};

userSchema.statics.signup = async function (email, password) {
//validation
if (!email && !password) {
throw Error("All Fields Must Be Filled");
}

//check if email is valid
if (!validator.isEmail(email)) {
throw Error("Invalid Email ");
}

//lowercase, Uppercase, Number, Symble, 8+ chars
if (!validator.isStrongPassword(password)) {
throw Error(passRequirementsMessage());
}

const exist = await this.findOne({ email });

if (exist) {
throw Error("mail already used");
throw Error("Email Already Used");
}

//encript password
Expand Down
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"express": "^4.18.2",
"i": "^0.3.7",
"mongoose": "^6.9.2",
"npm": "^9.5.1"
"npm": "^9.5.1",
"validator": "^13.9.0"
}
}

0 comments on commit 1d134c7

Please sign in to comment.