const validator = require('validator') const mongoose = require('mongoose') const bcrypt = require('bcryptjs') const jwt = require('jsonwebtoken') const ejwt = require('express-jwt') const Joi = require('joi') const userSchema = new mongoose.Schema({ name: { type: String }, email: { type: String, required: true }, password: { type: String, required: true }, tokens: [{ token: { type: String, required: true } }] }) userSchema.statics.joiValidate = async function(obj) { const schema = Joi.object({ name: Joi.string().min(2).required(), password: Joi.string().min(8).regex(/[a-zA-Z0-9]{3,30}/).required(), email: Joi.string().email().required(), }) const validation = schema.validate(obj); return validation.error } // userSchema.pre('save', async function(next) { // const user = this // console.log('pre hash: ' + user.password) // user.password = await bcrypt.hash(user.password, 8) // console.log('posle hash: ' + user.password) // console.log('Middleware before password hash') // next() // }) const User = mongoose.model('User', userSchema) module.exports = User