Files
freeplanet_serverside/server/models/user.js
Paolo Arena 06cd4b8f0d Resolved error Unknown modifier: $pushAll
adding this:

 mongoose.plugin(schema => { schema.options.usePushEach = true });
2018-12-27 18:13:43 +01:00

223 lines
4.1 KiB
JavaScript

var bcrypt = require('bcrypt');
const mongoose = require('mongoose');
const validator = require('validator');
const jwt = require('jsonwebtoken');
const _ = require('lodash');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => { schema.options.usePushEach = true });
var UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
trim: true,
minlength: 1,
unique: true,
/*validate: {
validator: validator.isEmail,
message: '{VALUE} is not a valid email'
}*/
},
idapp: {
type: Number,
required: true,
},
username: {
type: String,
required: true,
trim: true,
minlength: 6,
unique: true,
},
password: {
type: String,
require: true,
minlength: 6,
},
lang: {
type: String,
require: true,
},
linkreg: {
type: String,
required: true
},
verified_email: {
type: Boolean,
},
tokens: [{
access: {
type: String,
required: true
},
token: {
type: String,
required: true
}
}],
date_tokenforgot: {
type: Date
},
tokenforgot: {
type: String,
},
});
UserSchema.methods.toJSON = function () {
var user = this;
var userObject = user.toObject();
return _.pick(userObject, ['_id', 'email', 'verified_email', 'username']);
};
UserSchema.methods.generateAuthToken = function () {
console.log("GENERA TOKEN : ");
var user = this;
var access = 'auth';
var token = jwt.sign({ _id: user._id.toHexString(), access }, process.env.SIGNCODE).toString();
// CANCELLA I PRECEDENTI !
user.tokens = [];
user.tokens.push({ access, token });
return user.save()
.then(() => {
//console.log("TOKEN USCITA : " + token)
return token;
})
.catch(err => {
console.log("Error", err.message);
});
};
UserSchema.statics.findByToken = function (token) {
var User = this;
var decoded;
try {
decoded = jwt.verify(token, process.env.SIGNCODE);
} catch (e) {
return Promise.reject();
}
return User.findOne({
'_id': decoded._id,
'tokens.token': token,
'tokens.access': 'auth'
});
};
UserSchema.statics.findByCredentials = function (username, password) {
var User = this;
var pwd = "";
return User.findOne({ username: username }).then((user) => {
if (!user) {
return null;
}
pwd = user.password;
return new Promise((resolve, reject) => {
// Use bcrypt.compare to compare password and user.password
console.log("pwd1 " + password);
console.log("pwd2 " + pwd);
bcrypt.compare(password, pwd, (err, res) => {
if (res) {
resolve(user);
} else {
return resolve(null);
}
});
});
});
};
UserSchema.statics.findByUsername = function (username) {
var User = this;
return User.findOne({
'username': username,
});
};
UserSchema.statics.findByLinkreg = function (idapp, linkreg) {
var User = this;
return User.findOne({
'linkreg': linkreg,
'idapp': idapp,
});
};
UserSchema.statics.findByLinkTokenforgot = function (idapp, email, tokenforgot) {
var User = this;
return User.findOne({
'email': email,
'tokenforgot': tokenforgot,
'date_tokenforgot': { $gte: new Date(ISODate().getTime() - 1000 * 60 * 60 * 4) }, // 4 ore fa!
'idapp': idapp,
});
};
UserSchema.statics.findByEmail = function (email) {
var User = this;
return User.findOne({
'email': email,
});
};
UserSchema.pre('save', function (next) {
var user = this;
/*
if (user.isModified('password')) {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(user.password, salt, (err, hash) => {
user.password = hash;
next();
});
});
} else {
next();
}
*/
next();
});
UserSchema.methods.removeToken = function (token) {
var user = this;
return user.update({
$pull: {
tokens: { token }
}
});
};
var User = mongoose.model('User', UserSchema);
class Hero {
constructor(name, level) {
this.name = name;
this.level = level;
}
// Adding a method to the constructor
greet() {
return `${this.name} says hello.`;
}
}
module.exports = { User, Hero };