Skip to content

feat: Allow logout with invalid session token #1803

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

Open
wants to merge 18 commits into
base: alpha
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/CoreManager.ts
Original file line number Diff line number Diff line change
@@ -197,7 +197,7 @@ export interface UserController {
loginAs: (user: ParseUser, userId: string) => Promise<ParseUser>;
become: (user: ParseUser, options?: RequestOptions) => Promise<ParseUser>;
hydrate: (user: ParseUser, userJSON: AttributeMap) => Promise<ParseUser>;
logOut: (options?: RequestOptions) => Promise<void>;
logOut: (options?: RequestOptions & { clearSession?: boolean }) => Promise<void>;
me: (user: ParseUser, options?: RequestOptions) => Promise<ParseUser>;
requestPasswordReset: (email: string, options?: RequestOptions) => Promise<void>;
updateUserOnDisk: (user: ParseUser) => Promise<ParseUser>;
23 changes: 17 additions & 6 deletions src/ParseUser.ts
Original file line number Diff line number Diff line change
@@ -812,11 +812,13 @@ class ParseUser<T extends Attributes = Attributes> extends ParseObject<T> {
* <code>current</code> will return <code>null</code>.
*
* @param {object} options
* @param {boolean} [options.clearSession] If true, the session token will be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a better explanation. It's now a rather technical description but not so clear what the purpose of the option is and why / when one should use it.

* removed from the user object when the session token is invalid.
* @static
* @returns {Promise} A promise that is resolved when the session is
* destroyed on the server.
*/
static logOut(options?: RequestOptions): Promise<void> {
static logOut(options?: RequestOptions & { clearSession?: boolean }): Promise<void> {
const controller = CoreManager.getUserController();
return controller.logOut(options);
}
@@ -1205,20 +1207,29 @@ const DefaultController = {
});
},

logOut(options?: RequestOptions): Promise<void> {
logOut(options?: RequestOptions & { clearSession?: boolean }): Promise<void> {
const RESTController = CoreManager.getRESTController();
const promiseCatch = e => {
console.log(e, options);
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 });
56 changes: 56 additions & 0 deletions src/__tests__/ParseUser-test.js
Original file line number Diff line number Diff line change
@@ -1042,6 +1042,62 @@ 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('cannot logout user with invalid session', async () => {
expect.assertions(2);
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);
try {
await ParseUser.logOut({ sessionToken: '1234', clearSession: false });
} catch (e) {
expect(e.code).toBe(ParseError.INVALID_SESSION_TOKEN);
expect(e.message).toBe('Invalid session token.');
}
});

it('can retreive a user with sessionToken (me)', async () => {
ParseUser.disableUnsafeCurrentUser();
ParseUser._clearCache();
4 changes: 3 additions & 1 deletion types/CoreManager.d.ts
Original file line number Diff line number Diff line change
@@ -178,7 +178,9 @@ export interface UserController {
loginAs: (user: ParseUser, userId: string) => Promise<ParseUser>;
become: (user: ParseUser, options?: RequestOptions) => Promise<ParseUser>;
hydrate: (user: ParseUser, userJSON: AttributeMap) => Promise<ParseUser>;
logOut: (options?: RequestOptions) => Promise<void>;
logOut: (options?: RequestOptions & {
clearSession?: boolean;
}) => Promise<void>;
me: (user: ParseUser, options?: RequestOptions) => Promise<ParseUser>;
requestPasswordReset: (email: string, options?: RequestOptions) => Promise<void>;
updateUserOnDisk: (user: ParseUser) => Promise<ParseUser>;
6 changes: 5 additions & 1 deletion types/ParseUser.d.ts
Original file line number Diff line number Diff line change
@@ -392,11 +392,15 @@ declare class ParseUser<T extends Attributes = Attributes> extends ParseObject<T
* <code>current</code> will return <code>null</code>.
*
* @param {object} options
* @param {boolean} [options.clearSession] If true, the session token will be
* removed from the user object when the session token is invalid.
* @static
* @returns {Promise} A promise that is resolved when the session is
* destroyed on the server.
*/
static logOut(options?: RequestOptions): Promise<void>;
static logOut(options?: RequestOptions & {
clearSession?: boolean;
}): Promise<void>;
/**
* Requests a password reset email to be sent to the specified email address
* associated with the user account. This email allows the user to securely