Repository: parse-community/Parse-SDK-JS
Pull request #1324 (2021-03-19 01:23:55)

Remove deletion of Anonymous User on logout
Based on conversation [here](https://community.parseplatform.org/t/anonymous-user-destroyed-on-logout/1425/6)

* Remove deletion on logout
* Remove deletion on setCurrentUser
* Exposes registered on AnonymousUtils

peril-parse-community[bot]: 
<!--
  0 failure: 
  0 warning: 
  
  1 markdown notices
  DangerID: danger-id-Peril;
-->



Danger run resulted in 1 warning; to find out more, see the [checks page](https://github.com/parse-community/Parse-SDK-JS/runs/2144905419).
<p align="right">
  Generated by :no_entry_sign: <a href="https://danger.systems/js">dangerJS</a>
</p>

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324?src=pr&el=h1) Report
> Merging [#1324](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324?src=pr&el=desc) (4809af0) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/7d289560a9a83e08c3542ee5d532aedf41df241f?el=desc) (7d28956) will **decrease** coverage by `0.05%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 4809af0 differs from pull request most recent head d36a22d. Consider uploading reports for the commit d36a22d to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324/graphs/tree.svg?width=650&height=150&src=pr&token=8nCENM0QHj)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##            master    #1324      +/-   ##
===========================================
- Coverage   100.00%   99.94%   -0.06%     
===========================================
  Files           60       60              
  Lines         5853     5847       -6     
  Branches      1313     1311       -2     
===========================================
- Hits          5853     5844       -9     
- Misses           0        3       +3     
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/AnonymousUtils.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324/diff?src=pr&el=tree#diff-c3JjL0Fub255bW91c1V0aWxzLmpz) | `100.00% <100.00%> (ø)` | |
| [src/ParseUser.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324/diff?src=pr&el=tree#diff-c3JjL1BhcnNlVXNlci5qcw==) | `99.57% <100.00%> (-0.43%)` | :arrow_down: |
| [src/ParseObject.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324/diff?src=pr&el=tree#diff-c3JjL1BhcnNlT2JqZWN0Lmpz) | `99.89% <0.00%> (-0.11%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324?src=pr&el=footer). Last update [7d28956...d36a22d](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1324?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/AnonymousUtils.js b/src/AnonymousUtils.js
index 4c8ce6f..872f612 100644
--- a/src/AnonymousUtils.js
+++ b/src/AnonymousUtils.js
@@ -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() {
diff --git a/src/ParseUser.js b/src/ParseUser.js
index 69d702f..d0303da 100644
--- a/src/ParseUser.js
+++ b/src/ParseUser.js
@@ -9,7 +9,6 @@
  * @flow
  */
 
-import AnonymousUtils from './AnonymousUtils';
 import CoreManager from './CoreManager';
 import isRevocableSession from './isRevocableSession';
 import ParseError from './ParseError';
@@ -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.
@@ -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();
@@ -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 });


Test patch:
diff --git a/src/__tests__/ParseUser-test.js b/src/__tests__/ParseUser-test.js
index 828b14b..6c0fa59 100644
--- a/src/__tests__/ParseUser-test.js
+++ b/src/__tests__/ParseUser-test.js
@@ -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();
@@ -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({
@@ -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 () => {
@@ -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({
@@ -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', () => {