Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: User management on React-Native #1298

Merged
merged 2 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ class ParseUser extends ParseObject {
return !!current && current.id === this.id;
}

/**
* Returns true if <code>current</code> would return this user.
*
* @returns {Promise<boolean>} true if user is cached on disk
*/
async isCurrentAsync(): Promise<boolean> {
const current = await ParseUser.currentAsync();
return !!current && current.id === this.id;
}

/**
* Returns get("username").
*
Expand Down Expand Up @@ -458,13 +468,13 @@ class ParseUser extends ParseObject {
* @param {...any} args
* @returns {Promise}
*/
save(...args: Array<any>): Promise<ParseUser> {
return super.save.apply(this, args).then(() => {
if (this.isCurrent()) {
return CoreManager.getUserController().updateUserOnDisk(this);
}
return this;
});
async save(...args: Array<any>): Promise<ParseUser> {
await super.save.apply(this, args);
const current = await this.isCurrentAsync();
if (current) {
return CoreManager.getUserController().updateUserOnDisk(this);
}
return this;
}

/**
Expand All @@ -474,13 +484,13 @@ class ParseUser extends ParseObject {
* @param {...any} args
* @returns {Parse.User}
*/
destroy(...args: Array<any>): Promise<ParseUser> {
return super.destroy.apply(this, args).then(() => {
if (this.isCurrent()) {
return CoreManager.getUserController().removeUserFromDisk();
}
return this;
});
async destroy(...args: Array<any>): Promise<ParseUser> {
await super.destroy.apply(this, args);
const current = await this.isCurrentAsync();
if (current) {
return CoreManager.getUserController().removeUserFromDisk();
}
return this;
}

/**
Expand All @@ -490,13 +500,13 @@ class ParseUser extends ParseObject {
* @param {...any} args
* @returns {Parse.User}
*/
fetch(...args: Array<any>): Promise<ParseUser> {
return super.fetch.apply(this, args).then(() => {
if (this.isCurrent()) {
return CoreManager.getUserController().updateUserOnDisk(this);
}
return this;
});
async fetch(...args: Array<any>): Promise<ParseUser> {
await super.fetch.apply(this, args);
const current = await this.isCurrentAsync();
if (current) {
return CoreManager.getUserController().updateUserOnDisk(this);
}
return this;
}

/**
Expand All @@ -506,13 +516,13 @@ class ParseUser extends ParseObject {
* @param {...any} args
* @returns {Parse.User}
*/
fetchWithInclude(...args: Array<any>): Promise<ParseUser> {
return super.fetchWithInclude.apply(this, args).then(() => {
if (this.isCurrent()) {
return CoreManager.getUserController().updateUserOnDisk(this);
}
return this;
});
async fetchWithInclude(...args: Array<any>): Promise<ParseUser> {
await super.fetchWithInclude.apply(this, args);
const current = await this.isCurrentAsync();
if (current) {
return CoreManager.getUserController().updateUserOnDisk(this);
}
return this;
}

/**
Expand Down Expand Up @@ -1161,7 +1171,7 @@ const DefaultController = {
return RESTController.request('POST', 'requestPasswordReset', { email: email }, options);
},

upgradeToRevocableSession(user: ParseUser, options: RequestOptions) {
async upgradeToRevocableSession(user: ParseUser, options: RequestOptions) {
const token = user.getSessionToken();
if (!token) {
return Promise.reject(
Expand All @@ -1172,15 +1182,15 @@ const DefaultController = {
options.sessionToken = token;

const RESTController = CoreManager.getRESTController();
return RESTController.request('POST', 'upgradeToRevocableSession', {}, options).then(result => {
const session = new ParseSession();
session._finishFetch(result);
user._finishFetch({ sessionToken: session.getSessionToken() });
if (user.isCurrent()) {
return DefaultController.setCurrentUser(user);
}
return Promise.resolve(user);
});
const result = await RESTController.request('POST', 'upgradeToRevocableSession', {}, options);
const session = new ParseSession();
session._finishFetch(result);
user._finishFetch({ sessionToken: session.getSessionToken() });
const current = await user.isCurrentAsync();
if (current) {
return DefaultController.setCurrentUser(user);
}
return Promise.resolve(user);
},

linkWith(user: ParseUser, authData: AuthData, options: FullOptions) {
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/ParseUser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,54 @@ describe('ParseUser', () => {
});
});

it('can linkWith async', async () => {
const currentStorage = CoreManager.getStorageController();
CoreManager.setStorageController(mockAsyncStorage);
ParseUser.enableUnsafeCurrentUser();
ParseUser._clearCache();
CoreManager.setRESTController({
request() {
return Promise.resolve(
{
objectId: 'uid5Async',
sessionToken: 'r:123abc',
authData: {
test: {
id: 'id',
access_token: 'access_token',
},
},
},
200
);
},
ajax() {},
});
const provider = {
authenticate(options) {
if (options.success) {
options.success(this, {
id: 'id',
access_token: 'access_token',
});
}
},
restoreAuthentication() {},
getAuthType() {
return 'test';
},
deauthenticate() {},
};

const user = new ParseUser();
await user.linkWith(provider, null, { useMasterKey: true });

expect(user.get('authData')).toEqual({
test: { id: 'id', access_token: 'access_token' },
});
CoreManager.setStorageController(currentStorage);
});

it('handle linkWith authentication failure', async () => {
const provider = {
authenticate(options) {
Expand Down