diff --git a/src/ParseUser.js b/src/ParseUser.js index eb652e046..8b799b1c2 100644 --- a/src/ParseUser.js +++ b/src/ParseUser.js @@ -419,6 +419,19 @@ export default class ParseUser extends ParseObject { }); } + /** + * Wrap the default destroy behavior with functionality that logs out + * the current user when it is destroyed + */ + destroy(...args: Array): ParsePromise { + return super.destroy.apply(this, args).then(() => { + if (this.isCurrent()) { + return CoreManager.getUserController().removeUserFromDisk(); + } + return this; + }); + } + /** * Wrap the default fetch behavior with functionality to save to local * storage if this is current user. @@ -750,6 +763,13 @@ var DefaultController = { }); }, + removeUserFromDisk() { + let path = Storage.generatePath(CURRENT_USER_KEY); + currentUserCacheMatchesDisk = true; + currentUserCache = null; + return Storage.removeItemAsync(path); + }, + setCurrentUser(user) { currentUserCache = user; user._cleanupAuthData(); diff --git a/src/__tests__/ParseUser-test.js b/src/__tests__/ParseUser-test.js index 0d4cdbc44..03987162b 100644 --- a/src/__tests__/ParseUser-test.js +++ b/src/__tests__/ParseUser-test.js @@ -431,6 +431,37 @@ describe('ParseUser', () => { }); })); + it('removes the current user from disk when destroyed', asyncHelper((done) => { + ParseUser.enableUnsafeCurrentUser(); + ParseUser._clearCache(); + Storage._clear(); + CoreManager.setRESTController({ + request() { + return ParsePromise.as({ + objectId: 'uid9', + }, 201); + }, + ajax() {} + }); + + ParseUser.signUp('destroyed', 'password').then((u) => { + expect(u.isCurrent()).toBe(true); + CoreManager.setRESTController({ + request() { + return ParsePromise.as({}, 200); + }, + ajax() {} + }); + return u.destroy(); + }).then((u) => { + expect(ParseUser.current()).toBe(null); + return ParseUser.currentAsync(); + }).then((current) => { + expect(current).toBe(null); + done(); + }); + })); + it('updates the current user on disk when fetched', asyncHelper((done) => { ParseUser.enableUnsafeCurrentUser(); ParseUser._clearCache();