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: do not fail to re-login if previous token is wrong #677

Merged
Merged
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
25 changes: 17 additions & 8 deletions src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ function Api({
* @param {string} sessionToken
* @returns {Promise<import('../deserializers/application-token').ApplicationToken>}
*/
this.deleteApplicationToken = async sessionToken =>
agent
.delete(`${this.endpoint()}/api/application-tokens`)
.set(HEADER_FOREST_ORIGIN, 'forest-cli')
.set(HEADER_CONTENT_TYPE, HEADER_CONTENT_TYPE_JSON)
.set(HEADER_USER_AGENT, this.userAgent)
.set('Authorization', `Bearer ${sessionToken}`)
.send();
this.deleteApplicationToken = async sessionToken => {
try {
await agent
.delete(`${this.endpoint()}/api/application-tokens`)
.set(HEADER_FOREST_ORIGIN, 'forest-cli')
.set(HEADER_CONTENT_TYPE, HEADER_CONTENT_TYPE_JSON)
.set(HEADER_USER_AGENT, this.userAgent)
.set('Authorization', `Bearer ${sessionToken}`)
.send();
} catch (error) {
if (error.code === 'ERR_INVALID_CHAR' && error?.message.includes('"Authorization"')) {
// token is wrong. silencing this error since it cannot be used for auth anyway.
} else {
throw error;
}
}
};

this.createProject = async (config, sessionToken, project) => {
let newProject;
Expand Down
15 changes: 15 additions & 0 deletions test/services/api.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,20 @@ describe('services > API', () => {
expect(superagent.set).toHaveBeenCalledWith('Authorization', 'Bearer THE-TOKEN');
expect(superagent.send).toHaveBeenCalledWith();
});
it('should silence ERR_INVALID_CHAR errors relative to auth token', async () => {
expect.assertions(1);
const { superagent, api } = setup();
const error = new Error('Invalid character in header content ["Authorization"]');
error.code = 'ERR_INVALID_CHAR';
superagent.send.mockRejectedValue(error);
await expect(api.deleteApplicationToken('THE-TOKEN')).resolves.not.toThrow();
});
it('should rethrow other errors', async () => {
expect.assertions(1);
const { superagent, api } = setup();
const error = new Error('Some other error');
superagent.send.mockRejectedValue(error);
await expect(api.deleteApplicationToken('THE-TOKEN')).rejects.toBe(error);
});
});
});
Loading