Skip to content

Commit 4b4f3f6

Browse files
authored
fix: User management on React-Native (#1298)
* fix: linkWith on react-native * async functions get user async
1 parent 4caede2 commit 4b4f3f6

File tree

2 files changed

+96
-38
lines changed

2 files changed

+96
-38
lines changed

src/ParseUser.js

Lines changed: 48 additions & 38 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
/**
@@ -474,13 +484,13 @@ class ParseUser extends ParseObject {
474484
* @param {...any} args
475485
* @returns {Parse.User}
476486
*/
477-
destroy(...args: Array<any>): Promise<ParseUser> {
478-
return super.destroy.apply(this, args).then(() => {
479-
if (this.isCurrent()) {
480-
return CoreManager.getUserController().removeUserFromDisk();
481-
}
482-
return this;
483-
});
487+
async destroy(...args: Array<any>): Promise<ParseUser> {
488+
await super.destroy.apply(this, args);
489+
const current = await this.isCurrentAsync();
490+
if (current) {
491+
return CoreManager.getUserController().removeUserFromDisk();
492+
}
493+
return this;
484494
}
485495

486496
/**
@@ -490,13 +500,13 @@ class ParseUser extends ParseObject {
490500
* @param {...any} args
491501
* @returns {Parse.User}
492502
*/
493-
fetch(...args: Array<any>): Promise<ParseUser> {
494-
return super.fetch.apply(this, args).then(() => {
495-
if (this.isCurrent()) {
496-
return CoreManager.getUserController().updateUserOnDisk(this);
497-
}
498-
return this;
499-
});
503+
async fetch(...args: Array<any>): Promise<ParseUser> {
504+
await super.fetch.apply(this, args);
505+
const current = await this.isCurrentAsync();
506+
if (current) {
507+
return CoreManager.getUserController().updateUserOnDisk(this);
508+
}
509+
return this;
500510
}
501511

502512
/**
@@ -506,13 +516,13 @@ class ParseUser extends ParseObject {
506516
* @param {...any} args
507517
* @returns {Parse.User}
508518
*/
509-
fetchWithInclude(...args: Array<any>): Promise<ParseUser> {
510-
return super.fetchWithInclude.apply(this, args).then(() => {
511-
if (this.isCurrent()) {
512-
return CoreManager.getUserController().updateUserOnDisk(this);
513-
}
514-
return this;
515-
});
519+
async fetchWithInclude(...args: Array<any>): Promise<ParseUser> {
520+
await super.fetchWithInclude.apply(this, args);
521+
const current = await this.isCurrentAsync();
522+
if (current) {
523+
return CoreManager.getUserController().updateUserOnDisk(this);
524+
}
525+
return this;
516526
}
517527

518528
/**
@@ -1161,7 +1171,7 @@ const DefaultController = {
11611171
return RESTController.request('POST', 'requestPasswordReset', { email: email }, options);
11621172
},
11631173

1164-
upgradeToRevocableSession(user: ParseUser, options: RequestOptions) {
1174+
async upgradeToRevocableSession(user: ParseUser, options: RequestOptions) {
11651175
const token = user.getSessionToken();
11661176
if (!token) {
11671177
return Promise.reject(
@@ -1172,15 +1182,15 @@ const DefaultController = {
11721182
options.sessionToken = token;
11731183

11741184
const RESTController = CoreManager.getRESTController();
1175-
return RESTController.request('POST', 'upgradeToRevocableSession', {}, options).then(result => {
1176-
const session = new ParseSession();
1177-
session._finishFetch(result);
1178-
user._finishFetch({ sessionToken: session.getSessionToken() });
1179-
if (user.isCurrent()) {
1180-
return DefaultController.setCurrentUser(user);
1181-
}
1182-
return Promise.resolve(user);
1183-
});
1185+
const result = await RESTController.request('POST', 'upgradeToRevocableSession', {}, options);
1186+
const session = new ParseSession();
1187+
session._finishFetch(result);
1188+
user._finishFetch({ sessionToken: session.getSessionToken() });
1189+
const current = await user.isCurrentAsync();
1190+
if (current) {
1191+
return DefaultController.setCurrentUser(user);
1192+
}
1193+
return Promise.resolve(user);
11841194
},
11851195

11861196
linkWith(user: ParseUser, authData: AuthData, options: FullOptions) {

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)