Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow logout with invalid session token #1803

Open
wants to merge 15 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,18 +1208,26 @@ const DefaultController = {

logOut(options: RequestOptions): Promise<ParseUser> {
const RESTController = CoreManager.getRESTController();
const promiseCatch = e => {
if (e.code === ParseError.INVALID_SESSION_TOKEN && options.clearSession) {
return;
}
throw e;
};
if (options.sessionToken) {
return RESTController.request('POST', 'logout', {}, options);
return RESTController.request('POST', 'logout', {}, options).catch(promiseCatch);
}
return DefaultController.currentUserAsync().then(currentUser => {
const path = Storage.generatePath(CURRENT_USER_KEY);
let promise = Storage.removeItemAsync(path);
if (currentUser !== null) {
const currentSession = currentUser.getSessionToken();
if (currentSession && isRevocableSession(currentSession)) {
promise = promise.then(() => {
return RESTController.request('POST', 'logout', {}, { sessionToken: currentSession });
});
promise = promise
.then(() => {
return RESTController.request('POST', 'logout', {}, { sessionToken: currentSession });
})
.catch(promiseCatch);
}
currentUser._logOutWithAll();
currentUser._finishFetch({ sessionToken: undefined });
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/ParseUser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,40 @@ describe('ParseUser', () => {
});
});

it('can logout user with clear session', async () => {
ParseUser.disableUnsafeCurrentUser();
ParseUser._clearCache();
Storage._clear();
const RESTController = {
request() {
const error = new ParseError(ParseError.INVALID_SESSION_TOKEN, 'Invalid session token.');
return Promise.reject(error);
},
ajax() {},
};
jest.spyOn(RESTController, 'request');
CoreManager.setRESTController(RESTController);

await ParseUser.logOut({ clearSession: true });
});

it('can logout user with clear session and session token', async () => {
ParseUser.disableUnsafeCurrentUser();
ParseUser._clearCache();
Storage._clear();
const RESTController = {
request() {
const error = new ParseError(ParseError.INVALID_SESSION_TOKEN, 'Invalid session token.');
return Promise.reject(error);
},
ajax() {},
};
jest.spyOn(RESTController, 'request');
CoreManager.setRESTController(RESTController);

await ParseUser.logOut({ sessionToken: '1234', clearSession: true });
});

it('can retreive a user with sessionToken (me)', async () => {
ParseUser.disableUnsafeCurrentUser();
ParseUser._clearCache();
Expand Down