Skip to content

Commit e0e9fff

Browse files
authored
Merge pull request #60 from FullstackAcademy/dunder-db-only
Define user model directly by pulling in _db
2 parents c99989e + 7f4b699 commit e0e9fff

File tree

2 files changed

+45
-50
lines changed

2 files changed

+45
-50
lines changed

generated/server/db/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
var db = require('./_db');
33
module.exports = db;
44

5-
require('./models/user')(db);
5+
var User = require('./models/user');
66

generated/server/db/models/user.js

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,53 @@ var crypto = require('crypto');
33
var _ = require('lodash');
44
var Sequelize = require('sequelize');
55

6-
module.exports = function (db) {
6+
var db = require('../_db');
77

8-
db.define('user', {
9-
email: {
10-
type: Sequelize.STRING
8+
module.exports = db.define('user', {
9+
email: {
10+
type: Sequelize.STRING
11+
},
12+
password: {
13+
type: Sequelize.STRING
14+
},
15+
salt: {
16+
type: Sequelize.STRING
17+
},
18+
twitter_id: {
19+
type: Sequelize.STRING
20+
},
21+
facebook_id: {
22+
type: Sequelize.STRING
23+
},
24+
google_id: {
25+
type: Sequelize.STRING
26+
}
27+
}, {
28+
instanceMethods: {
29+
sanitize: function () {
30+
return _.omit(this.toJSON(), ['password', 'salt']);
1131
},
12-
password: {
13-
type: Sequelize.STRING
14-
},
15-
salt: {
16-
type: Sequelize.STRING
17-
},
18-
twitter_id: {
19-
type: Sequelize.STRING
20-
},
21-
facebook_id: {
22-
type: Sequelize.STRING
23-
},
24-
google_id: {
25-
type: Sequelize.STRING
32+
correctPassword: function (candidatePassword) {
33+
return this.Model.encryptPassword(candidatePassword, this.salt) === this.password;
2634
}
27-
}, {
28-
instanceMethods: {
29-
sanitize: function () {
30-
return _.omit(this.toJSON(), ['password', 'salt']);
31-
},
32-
correctPassword: function (candidatePassword) {
33-
return this.Model.encryptPassword(candidatePassword, this.salt) === this.password;
34-
}
35-
},
36-
classMethods: {
37-
generateSalt: function () {
38-
return crypto.randomBytes(16).toString('base64');
39-
},
40-
encryptPassword: function (plainText, salt) {
41-
var hash = crypto.createHash('sha1');
42-
hash.update(plainText);
43-
hash.update(salt);
44-
return hash.digest('hex');
45-
}
35+
},
36+
classMethods: {
37+
generateSalt: function () {
38+
return crypto.randomBytes(16).toString('base64');
4639
},
47-
hooks: {
48-
beforeValidate: function (user) {
49-
if (user.changed('password')) {
50-
user.salt = user.Model.generateSalt();
51-
user.password = user.Model.encryptPassword(user.password, user.salt);
52-
}
40+
encryptPassword: function (plainText, salt) {
41+
var hash = crypto.createHash('sha1');
42+
hash.update(plainText);
43+
hash.update(salt);
44+
return hash.digest('hex');
45+
}
46+
},
47+
hooks: {
48+
beforeValidate: function (user) {
49+
if (user.changed('password')) {
50+
user.salt = user.Model.generateSalt();
51+
user.password = user.Model.encryptPassword(user.password, user.salt);
5352
}
5453
}
55-
});
56-
57-
58-
59-
};
60-
54+
}
55+
});

0 commit comments

Comments
 (0)