Skip to content

Commit c66cc8d

Browse files
committed
Merge pull request #341 from steven-supersolid/anonymous
Bugfix: set username to random string if missing in RestWrite
2 parents 8dcae3d + 0012e66 commit c66cc8d

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

spec/RestCreate.spec.js

+44
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,50 @@ describe('rest create', () => {
5757
});
5858
});
5959

60+
it('handles anonymous user signup', (done) => {
61+
var data1 = {
62+
authData: {
63+
anonymous: {
64+
id: '00000000-0000-0000-0000-000000000001'
65+
}
66+
}
67+
};
68+
var data2 = {
69+
authData: {
70+
anonymous: {
71+
id: '00000000-0000-0000-0000-000000000002'
72+
}
73+
}
74+
};
75+
var username1;
76+
rest.create(config, auth.nobody(config), '_User', data1)
77+
.then((r) => {
78+
expect(typeof r.response.objectId).toEqual('string');
79+
expect(typeof r.response.createdAt).toEqual('string');
80+
expect(typeof r.response.sessionToken).toEqual('string');
81+
return rest.create(config, auth.nobody(config), '_User', data1);
82+
}).then((r) => {
83+
expect(typeof r.response.objectId).toEqual('string');
84+
expect(typeof r.response.createdAt).toEqual('string');
85+
expect(typeof r.response.username).toEqual('string');
86+
expect(typeof r.response.updatedAt).toEqual('string');
87+
username1 = r.response.username;
88+
return rest.create(config, auth.nobody(config), '_User', data2);
89+
}).then((r) => {
90+
expect(typeof r.response.objectId).toEqual('string');
91+
expect(typeof r.response.createdAt).toEqual('string');
92+
expect(typeof r.response.sessionToken).toEqual('string');
93+
return rest.create(config, auth.nobody(config), '_User', data2);
94+
}).then((r) => {
95+
expect(typeof r.response.objectId).toEqual('string');
96+
expect(typeof r.response.createdAt).toEqual('string');
97+
expect(typeof r.response.username).toEqual('string');
98+
expect(typeof r.response.updatedAt).toEqual('string');
99+
expect(r.response.username).not.toEqual(username1);
100+
done();
101+
});
102+
});
103+
60104
it('test facebook signup and login', (done) => {
61105
var data = {
62106
authData: {

src/RestWrite.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function RestWrite(config, auth, className, query, data, originalData) {
5656
this.data.updatedAt = this.updatedAt;
5757
if (!this.query) {
5858
this.data.createdAt = this.updatedAt;
59-
this.data.objectId = newObjectId();
59+
this.data.objectId = newStringId(10);
6060
}
6161
}
6262
}
@@ -319,8 +319,7 @@ RestWrite.prototype.transformUser = function() {
319319
// Check for username uniqueness
320320
if (!this.data.username) {
321321
if (!this.query) {
322-
// TODO: what's correct behavior here
323-
this.data.username = '';
322+
this.data.username = newStringId(25);
324323
}
325324
return;
326325
}
@@ -714,13 +713,13 @@ RestWrite.prototype.objectId = function() {
714713
return this.data.objectId || this.query.objectId;
715714
};
716715

717-
// Returns a unique string that's usable as an object id.
718-
function newObjectId() {
716+
// Returns a unique string that's usable as an object or other id.
717+
function newStringId(size) {
719718
var chars = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
720719
'abcdefghijklmnopqrstuvwxyz' +
721720
'0123456789');
722721
var objectId = '';
723-
var bytes = crypto.randomBytes(10);
722+
var bytes = crypto.randomBytes(size);
724723
for (var i = 0; i < bytes.length; ++i) {
725724
// Note: there is a slight modulo bias, because chars length
726725
// of 62 doesn't divide the number of all bytes (256) evenly.

0 commit comments

Comments
 (0)