Проблема в том что метод User.singin ничего не возвращает, даже console.log, а опыта еще не хватает у меня(((
и еще не работает в схеме типы Data, Double, и default: Data.now
Подскажите пожалуйста!)
Код модели users:
var crypto = require('crypto');
var async = require('async');
var mongoose = require('../libs/mongoose');
var Schema = mongoose.Schema;
var schema = new Schema({
login: {
type: String,
unique: true
},
phone: {
required: true,
type: String,
unique: true
},
hashPassword: {
required: true,
type: String
},
salt: {
type: String,
required: true
},
balance: {
type: Double,
default: 0.00
},
singupData: {
type: Data,
default: Data.now
}
});
schema.methods.encryptPassword = function(password) {
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
};
schema.virtual('password')
.set(function(password) {
this._plainPassword = password;
this.salt = Math.random() + '';
this.hashPassword = this.encryptPassword(password);
})
.get(function() { return this._plainPassword; });
schema.methods.checkPassword = function (password) {
return this.encryptPassword(password) === this.hashPassword;
};
schema.statics.singup = function (phone, password) {
return phone + ' / ' + password;
};
schema.statics.singin = function (phone, password, callback) {
var User = this;
console.log(phone + ' + ' + password);
async.waterfall([
function (callback) {
User.findOne({phone: phone, hashPassword: this.checkPassword(password)}, callback)
},
function (user, callback) {
if(user) {
callback(null, user);
} else {
callback(message = 'Телефон или пароль не верны!', null);
}
}
], callback);
};
exports.User = mongoose.model('User', schema);
Код роута:
router.post('/singin', function (req, res, next) {
var phone = req.body.phone;
var password = req.body.password;
User.singin(phone, password, function (err, user) {
if(err) {
return next(err);
console.log(err);
}
console.log(user);
req.session.user = user._id;
res.send({});
});
});