Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Для базы
- var mongoose = require('mongoose');
- var User = require('./models/User').User;
- //var config = require('./config');
- //mongoose.connect('mongodb://127.0.0.1:27017/test');
- //var db = mongoose.connection.db;
- mongoose.connection.on('open', function () {
- var db = mongoose.connection.db;
- db.dropDatabase(function (err) {
- if (err) throw err;
- var vasya = new User({username:'Вася', password:'supervasya'});
- /*var petya = new User({username:'Петя', password:'123'});
- var admin = new User({username:'admin', password:'thetruehero'});
- vasya.save();
- petya.save();
- admin.save();
- */
- mongoose.disconnect();
- });
- });
- модель
- ar crypto = require('crypto');
- var mongoose = require('mongoose');
- //var mongoose = require('./libs/mongoose');
- mongoose.connect('mongodb://127.0.0.1:27017/test');
- Schema = mongoose.Schema;
- var schema = new Schema({
- username: {
- type: String,
- unique: true,
- required: true
- },
- hashedPassword: {
- type: String,
- require: true
- },
- salt: {
- type: String,
- require: true
- },
- created: {
- type: Date,
- default: Date.now
- }
- }
- );
- schema.methods.encryptPassword = function (password) {
- return crypto.createHmac('shal', this.salt).update(password).digest('hex');
- };
- schema.virtual('password')
- .set(function (password) {
- this._plainPassword = password;
- this.salt = Math.random() + ' ';
- this.hashedPassword = this.encryptPassword(password);
- })
- .get(function () {
- return this._plainPassword;
- });
- schema.methods.checkPassword = function (password) {
- return this.encryptPassword(password) === this.hashedPassword;
- };
- exports.User = mongoose.model('User', schema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement