mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 07:37:49 +01:00
26 lines
653 B
JavaScript
26 lines
653 B
JavaScript
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);
|