Skip to content

Remove deletion of Anonymous User on logout #1324

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

Merged
merged 2 commits into from
Mar 19, 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
12 changes: 12 additions & 0 deletions src/AnonymousUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ const AnonymousUtils = {
return user.linkWith(provider.getAuthType(), provider.getAuthData(), options);
},

/**
* Returns true if Authentication Provider has been registered for use.
*
* @function isRegistered
* @name Parse.AnonymousUtils.isRegistered
* @returns {boolean}
* @static
*/
isRegistered(): boolean {
return registered;
},

_getAuthProvider() {
const provider = {
restoreAuthentication() {
Expand Down
25 changes: 5 additions & 20 deletions src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @flow
*/

import AnonymousUtils from './AnonymousUtils';
import CoreManager from './CoreManager';
import isRevocableSession from './isRevocableSession';
import ParseError from './ParseError';
Expand Down Expand Up @@ -337,8 +336,7 @@ class ParseUser extends ParseObject {
* @param {string} username
*/
setUsername(username: string) {
// Strip anonymity, even we do not support anonymous user in js SDK, we may
// encounter anonymous user created by android/iOS in cloud code.
// Strip anonymity
const authData = this.get('authData');
if (authData && typeof authData === 'object' && authData.hasOwnProperty('anonymous')) {
// We need to set anonymous to null instead of deleting it in order to remove it from Parse.
Expand Down Expand Up @@ -960,13 +958,7 @@ const DefaultController = {
return Storage.removeItemAsync(path);
},

async setCurrentUser(user) {
const currentUser = await this.currentUserAsync();
if (currentUser && !user.equals(currentUser) && AnonymousUtils.isLinked(currentUser)) {
await currentUser.destroy({
sessionToken: currentUser.getSessionToken(),
});
}
setCurrentUser(user) {
currentUserCache = user;
user._cleanupAuthData();
user._synchronizeAllAuthData();
Expand Down Expand Up @@ -1143,18 +1135,11 @@ const DefaultController = {
const path = Storage.generatePath(CURRENT_USER_KEY);
let promise = Storage.removeItemAsync(path);
if (currentUser !== null) {
const isAnonymous = AnonymousUtils.isLinked(currentUser);
const currentSession = currentUser.getSessionToken();
if (currentSession && isRevocableSession(currentSession)) {
promise = promise
.then(() => {
if (isAnonymous) {
return currentUser.destroy({ sessionToken: currentSession });
}
})
.then(() => {
return RESTController.request('POST', 'logout', {}, { sessionToken: currentSession });
});
promise = promise.then(() => {
return RESTController.request('POST', 'logout', {}, { sessionToken: currentSession });
});
}
currentUser._logOutWithAll();
currentUser._finishFetch({ sessionToken: undefined });
Expand Down
10 changes: 6 additions & 4 deletions src/__tests__/ParseUser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,8 @@ describe('ParseUser', () => {

it('can sync anonymous user with current user', async () => {
const provider = AnonymousUtils._getAuthProvider();
expect(AnonymousUtils.isRegistered()).toBe(true);

jest.spyOn(provider, 'restoreAuthentication');

const object = new ParseUser();
Expand All @@ -1086,7 +1088,7 @@ describe('ParseUser', () => {
spy.mockRestore();
});

it('can destroy anonymous user on logout', async () => {
it('can logout anonymous user', async () => {
ParseUser.enableUnsafeCurrentUser();
ParseUser._clearCache();
CoreManager.setRESTController({
Expand All @@ -1111,7 +1113,7 @@ describe('ParseUser', () => {
ParseUser._setCurrentUserCache(user);

await ParseUser.logOut();
expect(user.destroy).toHaveBeenCalledTimes(1);
expect(ParseUser.current()).toBe(null);
});

it('can unlink', async () => {
Expand All @@ -1138,7 +1140,7 @@ describe('ParseUser', () => {
expect(user.linkWith).toHaveBeenCalledTimes(1);
});

it('can destroy anonymous user when login new user', async () => {
it('can logout anonymous user when login new user', async () => {
ParseUser.enableUnsafeCurrentUser();
ParseUser._clearCache();
CoreManager.setRESTController({
Expand Down Expand Up @@ -1176,7 +1178,7 @@ describe('ParseUser', () => {
ajax() {},
});
await ParseUser.logIn('username', 'password');
expect(user.destroy).toHaveBeenCalledTimes(1);
expect(ParseUser.current().id).not.toBe(user.id);
});

it('strip anonymity when we set username', () => {
Expand Down