Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let mongoose = require("mongoose"),
- bcrypt = require('bcrypt-nodejs');
- // SALT_WORK_FACTOR = 10;
- /* Creating Schema for the user */
- let userSchema = new mongoose.Schema({
- username: { type: String, required: true, unique: true},
- password: { type: String, required: true, unique: true },
- createdAt: {type: Date, required: true, default: Date.now}
- });
- /* Checks and validates the password againts the requirements */
- userSchema.path("password").validate(function(password) {
- return password.length >= 8;
- }, "The password must be of minimum length 8 characters.");
- userSchema.pre('save', function(next) {
- let user = this;
- bcrypt.genSalt(10, function(err, salt) {
- if(err) { return next(err); }
- // Using https://www.npmjs.com/package/bcrypt-nodejs
- bcrypt.hash(user.password, salt, null, function(err, hash) {
- if(err) { return next(err); }
- /* Makes the password to hash */
- user.password = hash;
- next();
- });
- });
- });
- let user = mongoose.model('user', userSchema);
- module.exports = user;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement