diff --git a/package-lock.json b/package-lock.json index efc5202..2746c3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "mongodb-intro-workshop", "version": "1.0.0", "license": "MIT", "dependencies": { @@ -41,8 +42,6 @@ "integrity": "sha512-+y4ZnePpvWs1fc/LhZRTHkTesbXkyBYuOB+5CyodZqrEuETXi3zOVfpAQIdgC3lXbHLTDG9dQosxR9BhvLKDLQ==", "dev": true, "dependencies": { - "@nicolo-ribaudo/chokidar-2": "2.1.8-no-fsevents", - "chokidar": "^3.4.0", "commander": "^4.0.1", "convert-source-map": "^1.1.0", "fs-readdir-recursive": "^1.1.0", @@ -1718,7 +1717,6 @@ "jest-resolve": "^26.6.2", "jest-util": "^26.6.2", "jest-worker": "^26.6.2", - "node-notifier": "^8.0.0", "slash": "^3.0.0", "source-map": "^0.6.0", "string-length": "^4.0.1", @@ -3611,7 +3609,6 @@ "dependencies": { "anymatch": "~3.1.1", "braces": "~3.0.2", - "fsevents": "~2.1.2", "glob-parent": "~5.1.0", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", @@ -4568,8 +4565,7 @@ "esprima": "^4.0.1", "estraverse": "^4.2.0", "esutils": "^2.0.2", - "optionator": "^0.8.1", - "source-map": "~0.6.1" + "optionator": "^0.8.1" }, "bin": { "escodegen": "bin/escodegen.js", @@ -7176,7 +7172,6 @@ "@types/node": "*", "anymatch": "^3.0.3", "fb-watchman": "^2.0.0", - "fsevents": "^2.1.2", "graceful-fs": "^4.2.4", "jest-regex-util": "^26.0.0", "jest-serializer": "^26.6.2", @@ -9115,7 +9110,6 @@ "resolved": "https://registry.npmjs.org/minipass-fetch/-/minipass-fetch-1.3.2.tgz", "integrity": "sha512-/i4fX1ss+Dtwyk++OsAI6SEV+eE1dvI6W+0hORdjfruQ7VD5uYTetJIHcEMjWiEiszWjn2aAtP1CB/Q4KfeoYA==", "dependencies": { - "encoding": "^0.1.12", "minipass": "^3.1.0", "minipass-sized": "^1.0.3", "minizlib": "^2.0.0" @@ -9294,8 +9288,7 @@ "bson": "^1.1.4", "denque": "^1.4.1", "require_optional": "^1.0.1", - "safe-buffer": "^5.1.2", - "saslprep": "^1.0.0" + "safe-buffer": "^5.1.2" }, "engines": { "node": ">=4" @@ -9351,7 +9344,6 @@ "lockfile": "^1.0.4", "md5-file": "^5.0.0", "mkdirp": "^1.0.4", - "mongodb": "3.6.2", "semver": "^7.3.2", "tar-stream": "^2.1.4", "tmp": "^0.2.1", @@ -9400,8 +9392,7 @@ "bson": "^1.1.4", "denque": "^1.4.1", "require_optional": "^1.0.1", - "safe-buffer": "^5.1.2", - "saslprep": "^1.0.0" + "safe-buffer": "^5.1.2" }, "engines": { "node": ">=4" diff --git a/src/db/connect.js b/src/db/connect.js index 0b79b1d..85ccecf 100644 --- a/src/db/connect.js +++ b/src/db/connect.js @@ -15,6 +15,11 @@ const mongoose = require("mongoose"); * useUnifiedTopology: true, * } */ -function connect() {} +function connect() { + return mongoose.connect("mongodb://localhost:27017/myApp", { + useNewUrlParser: true, + useUnifiedTopology: true, + }); +} module.exports = connect; diff --git a/src/models/user-model.js b/src/models/user-model.js index 22122f6..37c370b 100644 --- a/src/models/user-model.js +++ b/src/models/user-model.js @@ -37,7 +37,49 @@ const bcrypt = require("bcrypt"); * 2.6 with the "createdAt" and "updatedAt" properties that are created automatically */ -const UserSchema = new mongoose.Schema({}); +const UserSchema = new mongoose.Schema( + { + firstName: { + type: String, + required: [true, "The first name is required"], + trim: true, + }, + lastName: { + type: String, + required: [true, "The last name is required"], + trim: true, + }, + email: { + type: String, + required: [true, "The email is required"], + trim: true, + unique: true, + validate: { + validator: (value) => validator.isEmail(value), + message: (props) => `The email ${props.value} is not valid`, + }, + }, + password: { + type: String, + required: [true, "The password is required"], + minlength: [8, "The password is too short"], + }, + speaks: [ + { + type: String, + enum: [ + "english", + "spanish", + "catalan", + "german", + "italian", + "javascript", + ], + }, + ], + }, + { timestamps: true }, +); /** * 3. encrypt the password before storing it in the database @@ -50,7 +92,22 @@ const UserSchema = new mongoose.Schema({}); * * The `comparePassword` method should return a `bcrypt.compare` function call */ +UserSchema.pre("save", async function userPreSaveHook(next) { + if (!this.isModified("password")) return next(); + try { + const hash = await bcrypt.hash(this.password, 12); + + this.password = hash; + + return next(); + } catch (error) { + return next(error); + } +}); +UserSchema.methods.comparePassword = function (candidate) { + return bcrypt.compare(candidate, this.password); +}; const UserModel = new mongoose.model("user", UserSchema); module.exports = UserModel;