From e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d Mon Sep 17 00:00:00 2001 From: Piotr Russ Date: Mon, 16 Nov 2020 00:10:28 +0100 Subject: api, login, auth --- server/models/User.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 server/models/User.js (limited to 'server/models') diff --git a/server/models/User.js b/server/models/User.js new file mode 100644 index 0000000..4fa6ffe --- /dev/null +++ b/server/models/User.js @@ -0,0 +1,72 @@ +const mongoose = require("mongoose"); +const bcrypt = require('bcryptjs'); +const jwt = require('jsonwebtoken'); + +const userSchema = new mongoose.Schema({ + email: { + type: String, + trim: true, + lowercase: true, + unique: true, + required: true, + min: 4, + max: 255, + validate(value) { + if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) { + throw new Error('Wrong email address'); + } + } + }, + password: { + type: String, + required: true, + min: 4, + max: 1024, + }, + tokens: [{ + token: { + type: String, + required: true + } + }] +}); + +userSchema.methods.generateAuthToken = async function () { + const user = this; + const token = jwt.sign({ _id: user._id.toString() }, 'replaceThisWithSecretString') + + user.tokens = user.tokens.concat({ token }); + user.save(); + + return token; +} + +userSchema.statics.findByCredentials = async (email, password) => { + const user = await User.findOne({ email }); + + if (!user) { + throw new Error('Unable to login'); + } + + const isMatch = await bcrypt.compare(password, user.password); + + if (!isMatch) { + throw new Error('Unable to login'); + } + + return user; +} + +userSchema.pre('save', async function(next){ + const user = this; + + if (user.isModified('password')) { + user.password = await bcrypt.hash(user.password, 8); + } + + next(); +}) + +const User = mongoose.model('User', userSchema); + +module.exports = User; -- cgit v1.2.3