Skip to content

Commit ef96cba

Browse files
committed
Skip secret/hash logic if logged in or hash exists
1 parent 70a7103 commit ef96cba

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

lib/user-data.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,24 @@ export default class UserData {
1515
this.settings = settings;
1616
}
1717
json() {
18-
const verificationSecret = this.getVerificationSecret();
19-
const identifier = this.getIdentifier();
20-
if (this.settings.user_hash === undefined) {
21-
this.setUserHash(verificationSecret, identifier);
22-
}
18+
this.setUserHash();
2319
return this.escapedSettings(this.settings);
2420
}
2521
getVerificationSecret() {
2622
const { verificationSecret } = this.settings;
2723
delete this.settings.verificationSecret;
2824
return verificationSecret;
2925
}
30-
getIdentifier() {
31-
if (this.settings.user_id) {
32-
return this.settings.user_id.toString();
33-
} else {
34-
return this.settings.email;
35-
}
36-
}
37-
setUserHash(verificationSecret, identifier) {
38-
if (this.loggedOut) {
26+
setUserHash() {
27+
if (this.loggedOut || this.settings.user_hash !== undefined) {
3928
return;
4029
}
4130

31+
const { verificationSecret } = this.settings;
32+
delete this.settings.verificationSecret;
33+
const identifier = this.settings.user_id ? this.settings.user_id.toString() : this.settings.email;
34+
35+
4236
const userHash = IdentityVerification.userHash({
4337
secretKey: verificationSecret,
4438
identifier

test/user-data.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import UserData from '../lib/user-data';
2+
import { IdentityVerification } from '../lib/index';
23
import assert from 'assert';
34
import sinon from 'sinon';
45

@@ -21,7 +22,13 @@ describe('userData', () => {
2122
2223
};
2324
const userData = new UserData(settings);
24-
assert.equal(userData.getIdentifier(), 1);
25+
const userHash = sinon.spy(IdentityVerification, 'userHash');
26+
userData.json();
27+
userHash.restore();
28+
sinon.assert.calledWith(userHash, {
29+
secretKey: 'abc123',
30+
identifier: '1'
31+
});
2532
});
2633

2734
it('should grab the email as the identifier if no user_id', () => {
@@ -31,7 +38,13 @@ describe('userData', () => {
3138
3239
};
3340
const userData = new UserData(settings);
34-
assert.equal(userData.getIdentifier(), '[email protected]');
41+
const userHash = sinon.spy(IdentityVerification, 'userHash');
42+
userData.json();
43+
userHash.restore();
44+
sinon.assert.calledWith(userHash, {
45+
secretKey: 'abc123',
46+
identifier: '[email protected]'
47+
});
3548
});
3649

3750
it('should throw an error if there\'s no verification secret', () => {
@@ -118,11 +131,11 @@ describe('userData', () => {
118131
user_id: 1
119132
};
120133
const userData = new UserData(settings);
121-
const setUserHash = sinon.spy(userData, 'setUserHash');
134+
const userHash = sinon.spy(IdentityVerification, 'userHash');
122135
userData.json();
123136
userData.json();
124-
setUserHash.restore();
125-
sinon.assert.calledOnce(setUserHash);
137+
userHash.restore();
138+
sinon.assert.calledOnce(userHash);
126139
});
127140

128141
it('should return the userData', () => {

0 commit comments

Comments
 (0)