2020-11-20 10:02:30 +00:00
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const uniqueValidator = require('mongoose-unique-validator');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const secret = require('../config').secret;
|
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
const schema = new mongoose.Schema(
|
2020-11-20 10:02:30 +00:00
|
|
|
{
|
|
|
|
username: {
|
|
|
|
type: String,
|
|
|
|
lowercase: true,
|
|
|
|
unique: true,
|
|
|
|
required: [true, "can't be blank"],
|
|
|
|
match: [/^[a-zA-Z0-9]+$/, 'is invalid'],
|
|
|
|
index: true,
|
|
|
|
},
|
|
|
|
email: {
|
|
|
|
type: String,
|
|
|
|
lowercase: true,
|
|
|
|
unique: true,
|
|
|
|
required: [true, "can't be blank"],
|
|
|
|
match: [/\S+@\S+\.\S+/, 'is invalid'],
|
|
|
|
index: true,
|
|
|
|
},
|
|
|
|
bio: String,
|
|
|
|
image: String,
|
|
|
|
areTracksVisibleForAll: Boolean,
|
|
|
|
hash: String,
|
|
|
|
salt: String,
|
|
|
|
needsEmailValidation: Boolean,
|
|
|
|
verificationToken: String,
|
|
|
|
resetToken: {
|
|
|
|
token: String,
|
|
|
|
expires: Date,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ timestamps: true },
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
schema.plugin(uniqueValidator, { message: 'ist bereits vergeben. Sorry!' });
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
class User extends mongoose.Model {
|
|
|
|
validPassword(password) {
|
|
|
|
const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
|
|
|
return this.hash === hash;
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
setPassword(password) {
|
|
|
|
this.salt = crypto.randomBytes(16).toString('hex');
|
|
|
|
this.hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
generateJWT() {
|
|
|
|
const today = new Date();
|
|
|
|
const exp = new Date(today);
|
|
|
|
exp.setDate(today.getDate() + 60);
|
|
|
|
|
|
|
|
return jwt.sign(
|
|
|
|
{
|
|
|
|
id: this._id,
|
|
|
|
username: this.username,
|
|
|
|
exp: parseInt(exp.getTime() / 1000),
|
|
|
|
},
|
|
|
|
secret,
|
|
|
|
);
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
toAuthJSON() {
|
|
|
|
return {
|
2020-11-20 10:02:30 +00:00
|
|
|
username: this.username,
|
2020-11-25 14:55:56 +00:00
|
|
|
email: this.email,
|
|
|
|
token: this.generateJWT(),
|
|
|
|
bio: this.bio,
|
|
|
|
image: this.image,
|
|
|
|
areTracksVisibleForAll: this.areTracksVisibleForAll,
|
|
|
|
apiKey: this._id,
|
|
|
|
};
|
2020-04-13 00:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
toProfileJSONFor(user) {
|
|
|
|
return {
|
|
|
|
username: this.username,
|
|
|
|
bio: this.bio,
|
|
|
|
image: this.image || 'https://static.productionready.io/images/smiley-cyrus.jpg',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
mongoose.model(User, schema);
|
2020-11-23 23:29:16 +00:00
|
|
|
|
2020-11-25 14:55:56 +00:00
|
|
|
module.exports = User;
|