mirror of
https://gitlab.com/little-lines/backend.git
synced 2025-07-07 14:51:06 +00:00
login logout
This commit is contained in:
parent
24e7aa1ad0
commit
f1f614d4e7
6 changed files with 207 additions and 0 deletions
|
@ -0,0 +1,51 @@
|
|||
const mongoose = require('mongoose');
|
||||
const validator = require('validator');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const userSchema = new mongoose.Schema({
|
||||
username: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
password: {
|
||||
type: String,
|
||||
required: true,
|
||||
select : false
|
||||
},
|
||||
imgPath: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
isGoogleAccount: {
|
||||
type: Boolean
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
}
|
||||
});
|
||||
|
||||
userSchema.pre('save', async function (next) {
|
||||
if(!this.isModified('password')) {
|
||||
next();
|
||||
}
|
||||
|
||||
this.password = await bcrypt.hash(this.password, 10);
|
||||
});
|
||||
|
||||
userSchema.methods.comparePassword = async function (enteredPassword) {
|
||||
return await bcrypt.compare(enteredPassword, this.password);
|
||||
};
|
||||
|
||||
userSchema.methods.getJwtToken = function () {
|
||||
return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {
|
||||
expiresIn: process.env.JWT_EXPIRES_TIME
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('user', userSchema);
|
Loading…
Add table
Add a link
Reference in a new issue