Skip to content

Commit 081d763

Browse files
committed
fix: linkWith on react-native
1 parent 4caede2 commit 081d763

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/ParseUser.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,16 @@ class ParseUser extends ParseObject {
308308
return !!current && current.id === this.id;
309309
}
310310

311+
/**
312+
* Returns true if <code>current</code> would return this user.
313+
*
314+
* @returns {Promise<boolean>} true if user is cached on disk
315+
*/
316+
async isCurrentAsync(): Promise<boolean> {
317+
const current = await ParseUser.currentAsync();
318+
return !!current && current.id === this.id;
319+
}
320+
311321
/**
312322
* Returns get("username").
313323
*
@@ -458,13 +468,13 @@ class ParseUser extends ParseObject {
458468
* @param {...any} args
459469
* @returns {Promise}
460470
*/
461-
save(...args: Array<any>): Promise<ParseUser> {
462-
return super.save.apply(this, args).then(() => {
463-
if (this.isCurrent()) {
464-
return CoreManager.getUserController().updateUserOnDisk(this);
465-
}
466-
return this;
467-
});
471+
async save(...args: Array<any>): Promise<ParseUser> {
472+
await super.save.apply(this, args);
473+
const current = await this.isCurrentAsync();
474+
if (current) {
475+
return CoreManager.getUserController().updateUserOnDisk(this);
476+
}
477+
return this;
468478
}
469479

470480
/**

src/__tests__/ParseUser-test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,54 @@ describe('ParseUser', () => {
12801280
});
12811281
});
12821282

1283+
it('can linkWith async', async () => {
1284+
const currentStorage = CoreManager.getStorageController();
1285+
CoreManager.setStorageController(mockAsyncStorage);
1286+
ParseUser.enableUnsafeCurrentUser();
1287+
ParseUser._clearCache();
1288+
CoreManager.setRESTController({
1289+
request() {
1290+
return Promise.resolve(
1291+
{
1292+
objectId: 'uid5Async',
1293+
sessionToken: 'r:123abc',
1294+
authData: {
1295+
test: {
1296+
id: 'id',
1297+
access_token: 'access_token',
1298+
},
1299+
},
1300+
},
1301+
200
1302+
);
1303+
},
1304+
ajax() {},
1305+
});
1306+
const provider = {
1307+
authenticate(options) {
1308+
if (options.success) {
1309+
options.success(this, {
1310+
id: 'id',
1311+
access_token: 'access_token',
1312+
});
1313+
}
1314+
},
1315+
restoreAuthentication() {},
1316+
getAuthType() {
1317+
return 'test';
1318+
},
1319+
deauthenticate() {},
1320+
};
1321+
1322+
const user = new ParseUser();
1323+
await user.linkWith(provider, null, { useMasterKey: true });
1324+
1325+
expect(user.get('authData')).toEqual({
1326+
test: { id: 'id', access_token: 'access_token' },
1327+
});
1328+
CoreManager.setStorageController(currentStorage);
1329+
});
1330+
12831331
it('handle linkWith authentication failure', async () => {
12841332
const provider = {
12851333
authenticate(options) {

0 commit comments

Comments
 (0)