Skip to content

Commit

Permalink
Encryption Passwords Using dcrypt package.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadAkthamObeidat committed Jan 17, 2020
1 parent 4774047 commit 651e83a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const mongoose = require('mongoose');
// Import Validator 3rd party Package.
const validator = require('validator');
// Import Bcrypt 3rd party Package.
const bcrypt = require('bcryptjs');
// Create User Schema
const userSchema = new mongoose.Schema({
name: {
Expand Down Expand Up @@ -40,6 +42,18 @@ const userSchema = new mongoose.Schema({
watched_list: []
}
});

// Encryption the password.
// eslint-disable-next-line node/no-unsupported-features/es-syntax
userSchema.pre('save', async function(next) {
// ONly run this function if password was actually modified
if (!this.isModified('password')) {
return next();
}
// Hash the password with cost of 12
this.password = await bcrypt.hash(this.password, 12);
next();
});
// eslint-disable-next-line new-cap
const User = new mongoose.model('User', userSchema);

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"license": "ISC",
"dependencies": {
"axios": "^0.19.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"concurrently": "^5.0.2",
"connect-mongodb-session": "^2.2.0",
Expand Down

0 comments on commit 651e83a

Please sign in to comment.