diff --git a/src/AnonymousUtils.js b/src/AnonymousUtils.js index 4c8ce6f81..872f61241 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 69d702fb5..d0303dac3 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 }); diff --git a/src/__tests__/ParseUser-test.js b/src/__tests__/ParseUser-test.js index 828b14b0e..6c0fa5905 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', () => {