Skip to content

Commit 039ec84

Browse files
authored
Remove deletion of Anonymous User on logout (parse-community#1324)
* Remove deletion of Anonymous User on logout * clean up
1 parent 88d96dd commit 039ec84

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/AnonymousUtils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ const AnonymousUtils = {
8989
return user.linkWith(provider.getAuthType(), provider.getAuthData(), options);
9090
},
9191

92+
/**
93+
* Returns true if Authentication Provider has been registered for use.
94+
*
95+
* @function isRegistered
96+
* @name Parse.AnonymousUtils.isRegistered
97+
* @returns {boolean}
98+
* @static
99+
*/
100+
isRegistered(): boolean {
101+
return registered;
102+
},
103+
92104
_getAuthProvider() {
93105
const provider = {
94106
restoreAuthentication() {

src/ParseUser.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* @flow
1010
*/
1111

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

963-
async setCurrentUser(user) {
964-
const currentUser = await this.currentUserAsync();
965-
if (currentUser && !user.equals(currentUser) && AnonymousUtils.isLinked(currentUser)) {
966-
await currentUser.destroy({
967-
sessionToken: currentUser.getSessionToken(),
968-
});
969-
}
961+
setCurrentUser(user) {
970962
currentUserCache = user;
971963
user._cleanupAuthData();
972964
user._synchronizeAllAuthData();
@@ -1143,18 +1135,11 @@ const DefaultController = {
11431135
const path = Storage.generatePath(CURRENT_USER_KEY);
11441136
let promise = Storage.removeItemAsync(path);
11451137
if (currentUser !== null) {
1146-
const isAnonymous = AnonymousUtils.isLinked(currentUser);
11471138
const currentSession = currentUser.getSessionToken();
11481139
if (currentSession && isRevocableSession(currentSession)) {
1149-
promise = promise
1150-
.then(() => {
1151-
if (isAnonymous) {
1152-
return currentUser.destroy({ sessionToken: currentSession });
1153-
}
1154-
})
1155-
.then(() => {
1156-
return RESTController.request('POST', 'logout', {}, { sessionToken: currentSession });
1157-
});
1140+
promise = promise.then(() => {
1141+
return RESTController.request('POST', 'logout', {}, { sessionToken: currentSession });
1142+
});
11581143
}
11591144
currentUser._logOutWithAll();
11601145
currentUser._finishFetch({ sessionToken: undefined });

src/__tests__/ParseUser-test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,8 @@ describe('ParseUser', () => {
10661066

10671067
it('can sync anonymous user with current user', async () => {
10681068
const provider = AnonymousUtils._getAuthProvider();
1069+
expect(AnonymousUtils.isRegistered()).toBe(true);
1070+
10691071
jest.spyOn(provider, 'restoreAuthentication');
10701072

10711073
const object = new ParseUser();
@@ -1086,7 +1088,7 @@ describe('ParseUser', () => {
10861088
spy.mockRestore();
10871089
});
10881090

1089-
it('can destroy anonymous user on logout', async () => {
1091+
it('can logout anonymous user', async () => {
10901092
ParseUser.enableUnsafeCurrentUser();
10911093
ParseUser._clearCache();
10921094
CoreManager.setRESTController({
@@ -1111,7 +1113,7 @@ describe('ParseUser', () => {
11111113
ParseUser._setCurrentUserCache(user);
11121114

11131115
await ParseUser.logOut();
1114-
expect(user.destroy).toHaveBeenCalledTimes(1);
1116+
expect(ParseUser.current()).toBe(null);
11151117
});
11161118

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

1141-
it('can destroy anonymous user when login new user', async () => {
1143+
it('can logout anonymous user when login new user', async () => {
11421144
ParseUser.enableUnsafeCurrentUser();
11431145
ParseUser._clearCache();
11441146
CoreManager.setRESTController({
@@ -1176,7 +1178,7 @@ describe('ParseUser', () => {
11761178
ajax() {},
11771179
});
11781180
await ParseUser.logIn('username', 'password');
1179-
expect(user.destroy).toHaveBeenCalledTimes(1);
1181+
expect(ParseUser.current().id).not.toBe(user.id);
11801182
});
11811183

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

0 commit comments

Comments
 (0)