Skip to content

Commit 1481999

Browse files
committed
Remove user from local storage on destroy
1 parent 0c2dae1 commit 1481999

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/ParseUser.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,19 @@ export default class ParseUser extends ParseObject {
419419
});
420420
}
421421

422+
/**
423+
* Wrap the default destroy behavior with functionality that logs out
424+
* the current user when it is destroyed
425+
*/
426+
destroy(...args: Array<any>): ParsePromise {
427+
return super.destroy.apply(this, args).then(() => {
428+
if (this.isCurrent()) {
429+
return CoreManager.getUserController().removeUserFromDisk();
430+
}
431+
return this;
432+
});
433+
}
434+
422435
/**
423436
* Wrap the default fetch behavior with functionality to save to local
424437
* storage if this is current user.
@@ -750,6 +763,13 @@ var DefaultController = {
750763
});
751764
},
752765

766+
removeUserFromDisk() {
767+
let path = Storage.generatePath(CURRENT_USER_KEY);
768+
currentUserCacheMatchesDisk = true;
769+
currentUserCache = null;
770+
return Storage.removeItemAsync(path);
771+
},
772+
753773
setCurrentUser(user) {
754774
currentUserCache = user;
755775
user._cleanupAuthData();

src/__tests__/ParseUser-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,37 @@ describe('ParseUser', () => {
431431
});
432432
}));
433433

434+
it('removes the current user from disk when destroyed', asyncHelper((done) => {
435+
ParseUser.enableUnsafeCurrentUser();
436+
ParseUser._clearCache();
437+
Storage._clear();
438+
CoreManager.setRESTController({
439+
request() {
440+
return ParsePromise.as({
441+
objectId: 'uid9',
442+
}, 201);
443+
},
444+
ajax() {}
445+
});
446+
447+
ParseUser.signUp('destroyed', 'password').then((u) => {
448+
expect(u.isCurrent()).toBe(true);
449+
CoreManager.setRESTController({
450+
request() {
451+
return ParsePromise.as({}, 200);
452+
},
453+
ajax() {}
454+
});
455+
return u.destroy();
456+
}).then((u) => {
457+
expect(ParseUser.current()).toBe(null);
458+
return ParseUser.currentAsync();
459+
}).then((current) => {
460+
expect(current).toBe(null);
461+
done();
462+
});
463+
}));
464+
434465
it('updates the current user on disk when fetched', asyncHelper((done) => {
435466
ParseUser.enableUnsafeCurrentUser();
436467
ParseUser._clearCache();

0 commit comments

Comments
 (0)