Skip to content

Commit 3ce634b

Browse files
committed
Merge pull request #1142 from ParsePlatform/flovilmart.fixes1136
do not override username
2 parents 0e66219 + b30805b commit 3ce634b

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

spec/RestCreate.spec.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,46 @@ describe('rest create', () => {
146146
done();
147147
});
148148
});
149-
149+
150+
it('handles anonymous user signup and upgrade to new user', (done) => {
151+
var data1 = {
152+
authData: {
153+
anonymous: {
154+
id: '00000000-0000-0000-0000-000000000001'
155+
}
156+
}
157+
};
158+
159+
var updatedData = {
160+
authData: { anonymous: null },
161+
username: 'hello',
162+
password: 'world'
163+
}
164+
var username1;
165+
var objectId;
166+
rest.create(config, auth.nobody(config), '_User', data1)
167+
.then((r) => {
168+
expect(typeof r.response.objectId).toEqual('string');
169+
expect(typeof r.response.createdAt).toEqual('string');
170+
expect(typeof r.response.sessionToken).toEqual('string');
171+
objectId = r.response.objectId;
172+
return auth.getAuthForSessionToken({config, sessionToken: r.response.sessionToken })
173+
}).then((sessionAuth) => {
174+
return rest.update(config, sessionAuth, '_User', objectId, updatedData);
175+
}).then((r) => {
176+
return Parse.User.logOut().then(() => {
177+
return Parse.User.logIn('hello', 'world');
178+
})
179+
}).then((r) => {
180+
expect(r.id).toEqual(objectId);
181+
expect(r.get('username')).toEqual('hello');
182+
done();
183+
}).catch((err) => {
184+
fail('should not fail')
185+
done();
186+
})
187+
});
188+
150189
it('handles no anonymous users config', (done) => {
151190
var NoAnnonConfig = Object.assign({}, config);
152191
NoAnnonConfig.authDataManager.setEnableAnonymousUsers(false);

src/RestWrite.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -281,26 +281,26 @@ RestWrite.prototype.handleAuthData = function(authData) {
281281

282282
this.storage['authProvider'] = Object.keys(authData).join(',');
283283

284-
if (results.length == 0) {
285-
this.data.username = cryptoUtils.newToken();
286-
} else if (!this.query) {
287-
// Login with auth data
288-
// Short circuit
289-
delete results[0].password;
290-
// need to set the objectId first otherwise location has trailing undefined
291-
this.data.objectId = results[0].objectId;
292-
this.response = {
293-
response: results[0],
294-
location: this.location()
295-
};
296-
} else if (this.query && this.query.objectId) {
297-
// Trying to update auth data but users
298-
// are different
299-
if (results[0].objectId !== this.query.objectId) {
300-
throw new Parse.Error(Parse.Error.ACCOUNT_ALREADY_LINKED,
301-
'this auth is already used');
284+
if (results.length > 0) {
285+
if (!this.query) {
286+
// Login with auth data
287+
// Short circuit
288+
delete results[0].password;
289+
// need to set the objectId first otherwise location has trailing undefined
290+
this.data.objectId = results[0].objectId;
291+
this.response = {
292+
response: results[0],
293+
location: this.location()
294+
};
295+
} else if (this.query && this.query.objectId) {
296+
// Trying to update auth data but users
297+
// are different
298+
if (results[0].objectId !== this.query.objectId) {
299+
throw new Parse.Error(Parse.Error.ACCOUNT_ALREADY_LINKED,
300+
'this auth is already used');
301+
}
302302
}
303-
}
303+
}
304304
return Promise.resolve();
305305
});
306306
}

0 commit comments

Comments
 (0)