Skip to content

Commit 70d3117

Browse files
committed
Merge pull request angular-fullstack#1620 from carragom/canary
refactor(model:user): exclude password info by default
2 parents d6aeacf + b29e9ef commit 70d3117

File tree

6 files changed

+29
-27
lines changed

6 files changed

+29
-27
lines changed

Diff for: app/templates/server/api/user(auth)/user.controller.js

+4-23
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,8 @@ function handleError(res, statusCode) {
2525
* restriction: 'admin'
2626
*/
2727
export function index(req, res) {
28-
<% if (filters.mongooseModels) { %>User.findAsync({}, '-salt -password')<% }
29-
if (filters.sequelizeModels) { %>User.findAll({
30-
attributes: [
31-
'_id',
32-
'name',
33-
'email',
34-
'role',
35-
'provider'
36-
]
37-
})<% } %>
28+
<% if (filters.mongooseModels) { %>User.findAsync()<% }
29+
if (filters.sequelizeModels) { %>User.findAll()<% } %>
3830
.then(users => {
3931
res.status(200).json(users);
4032
})
@@ -132,19 +124,8 @@ export function changePassword(req, res, next) {
132124
export function me(req, res, next) {
133125
var userId = req.user._id;
134126

135-
<% if (filters.mongooseModels) { %>User.findOneAsync({ _id: userId }, '-salt -password')<% }
136-
if (filters.sequelizeModels) { %>User.find({
137-
where: {
138-
_id: userId
139-
},
140-
attributes: [
141-
'_id',
142-
'name',
143-
'email',
144-
'role',
145-
'provider'
146-
]
147-
})<% } %>
127+
<% if (filters.mongooseModels) { %>User.findOneAsync({ _id: userId })<% }
128+
if (filters.sequelizeModels) { %>User.find({ where: { _id: userId } })<% } %>
148129
.then(user => { // don't ever give out the password or salt
149130
if (!user) {
150131
return res.status(401).end();

Diff for: app/templates/server/api/user(auth)/user.model(mongooseModels).js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ var UserSchema = new Schema({
1616
type: String,
1717
default: 'user'
1818
},
19-
password: String,
19+
password: {type: String, select: false},
2020
provider: String,
21-
salt: String<% if (filters.oauth) { %>,<% if (filters.facebookAuth) { %>
21+
salt: {type: String, select: false}<% if (filters.oauth) { %>,<% if (filters.facebookAuth) { %>
2222
facebook: {},<% } %><% if (filters.twitterAuth) { %>
2323
twitter: {},<% } %><% if (filters.googleAuth) { %>
2424
google: {},<% } %>

Diff for: app/templates/server/api/user(auth)/user.model(sequelizeModels).js

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
import _ from 'lodash';
34
import crypto from 'crypto';<% if (filters.oauth) { %>
45
var authTypes = ['github', 'twitter', 'facebook', 'google'];<% } %>
56

@@ -228,6 +229,12 @@ module.exports = function(sequelize, DataTypes) {
228229
} else {
229230
fn(null);
230231
}
232+
},
233+
234+
toJSON: function() {
235+
var excludedAttributes = ['salt', 'password'];
236+
237+
return _.omit(this.dataValues, excludedAttributes);
231238
}
232239
}
233240
});

Diff for: app/templates/server/api/user(auth)/user.model.spec(mongooseModels).js

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ describe('User Model', function() {
5252
return user.saveAsync();
5353
});
5454

55+
it('should exclude salt and hashedPassword by default', function() {
56+
User.find({name: user.name}, function(err, _user) {
57+
_user.should.not.have.property('salt');
58+
_user.should.not.have.property('hashedPassword');
59+
});
60+
});
61+
5562
it('should authenticate user if valid', function() {
5663
<%= expect() %>user.authenticate('password')<%= to() %>.be.true;
5764
});

Diff for: app/templates/server/api/user(auth)/user.model.spec(sequelizeModels).js

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ describe('User Model', function() {
5454
return user.save();
5555
});
5656

57+
it('should exclude salt and hashedPassword by default', function() {
58+
User.find({name: user.name}, function(err, _user) {
59+
_user.should.not.have.property('salt');
60+
_user.should.not.have.property('hashedPassword');
61+
});
62+
});
63+
5764
it('should authenticate user if valid', function() {
5865
<%= expect() %>user.authenticate('password')<%= to() %>.be.true;
5966
});

Diff for: app/templates/server/auth(auth)/local/passport.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {Strategy as LocalStrategy} from 'passport-local';
44
function localAuthenticate(User, email, password, done) {
55
<% if (filters.mongooseModels) { %>User.findOneAsync({
66
email: email.toLowerCase()
7-
})<% }
8-
if (filters.sequelizeModels) { %>User.find({
7+
}, '+salt +hashedPassword')<% }
8+
if (filters.sequelizeModels) { %>User.unscoped().find({
99
where: {
1010
email: email.toLowerCase()
1111
}

0 commit comments

Comments
 (0)