create, update, remove users

This commit is contained in:
2018-05-30 19:24:48 +03:00
parent 00f71bb78d
commit fd142c8710
11 changed files with 1853 additions and 1413 deletions

25
models/User.js Normal file
View File

@@ -0,0 +1,25 @@
const mongoose = require('mongoose');
const passportLocalMongoose = require('passport-local-mongoose');
const jwt = require('jsonwebtoken');
const { secret } = require('../config');
const { Schema } = mongoose;
const UserSchema = Schema({ username: { type: String, required: true } });
UserSchema.plugin(passportLocalMongoose);
UserSchema.methods.generateJwt = function () {
return jwt.sign({ id: this._id, username: this.username }, secret, { expiresIn: '1y' });
};
UserSchema.methods.toAuthJson = function () {
return {
id: this._id,
username: this.username,
jwt: this.generateJwt(),
};
};
mongoose.model('User', UserSchema);