forked from makersacademy/acebook-node-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Loo-Ashworth/emmad
added user validation stuff
- Loading branch information
Showing
10 changed files
with
462 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,48 @@ | ||
const mongoose = require("mongoose"); | ||
const bcrypt = require('bcrypt'); | ||
const validator = require('validator'); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
email: String, | ||
password: String, | ||
email: { | ||
type: String, | ||
unique: true, | ||
required: true, | ||
trim: true, | ||
lowercase: true, | ||
validate(value) { | ||
if (!validator.isEmail(value)) { | ||
throw new Error('Email is invalid') | ||
} | ||
}, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minlength: 8, | ||
validate(value) { | ||
if(value.length < 8) { | ||
throw new Error("Passwords is too short. At least 8 characters.") | ||
} | ||
}}, | ||
username: | ||
{ type: String, | ||
required: true, | ||
unique: true, | ||
trim: true, | ||
maxlength: 25 }, | ||
}); | ||
|
||
const User = mongoose.model("User", UserSchema); | ||
UserSchema.pre('save', async function (next) { | ||
console.log("password: ", this) | ||
const user = this; | ||
if (user.isModified('password')) { | ||
console.log('Hashing password...'); | ||
user.password = await bcrypt.hash(user.password, 8); | ||
console.log('Hashed password:', user.password); | ||
} | ||
next(); | ||
}); | ||
|
||
module.exports = mongoose.model("User", UserSchema); | ||
|
||
|
||
module.exports = User; |
Oops, something went wrong.