mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 15:47:48 +01:00
create, update, remove users
This commit is contained in:
25
models/User.js
Normal file
25
models/User.js
Normal 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);
|
||||
Reference in New Issue
Block a user