Skip to content

Commit

Permalink
Handle 401 when making get requests to software-app
Browse files Browse the repository at this point in the history
- Clear the JWT when a 401 is returned since the JWT is no longer valid
  • Loading branch information
pbstriker38 committed Mar 12, 2024
1 parent 82e837e commit d239da9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
17 changes: 11 additions & 6 deletions src/http/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const axios = require('axios');
import {version, window} from 'vscode';
import {api_endpoint, app_url, TWENTY_SEC_TIMEOUT_MILLIS} from '../Constants';
import { version, window } from 'vscode';
import { api_endpoint, app_url, TWENTY_SEC_TIMEOUT_MILLIS } from '../Constants';
import {
logIt,
getPluginId,
Expand All @@ -9,7 +9,8 @@ import {
getOs,
getOffsetSeconds,
getPluginUuid,
getItem
getItem,
setItem
} from '../Util';

// build the axios api base url
Expand All @@ -36,14 +37,18 @@ const headers = {
'X-SWDC-Plugin-Editor-Version': version
};

beApi.defaults.headers.common = {...beApi.defaults.headers.common, ...headers};
appApi.defaults.headers.common = {...appApi.defaults.headers.common, ...headers};
beApi.defaults.headers.common = { ...beApi.defaults.headers.common, ...headers };
appApi.defaults.headers.common = { ...appApi.defaults.headers.common, ...headers };

export async function appGet(api: string, queryParams: any = {}) {
updateOutgoingHeader();

return await appApi.get(api, {params: queryParams}).catch((err: any) => {
return await appApi.get(api, { params: queryParams }).catch((err: any) => {
logIt(`error for GET ${api}, message: ${err.message}`);
if (getResponseStatus(err?.response) === 401) {
// clear the JWT because it is invalid
setItem('jwt', null)
}
return err;
});
}
Expand Down
14 changes: 9 additions & 5 deletions src/sidebar/CodeTimeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
WebviewViewProvider,
WebviewViewResolveContext,
} from 'vscode';
import {appGet, isResponseOk} from '../http/HttpClient';
import {getConnectionErrorHtml} from '../local/404';
import {getBooleanItem, getItem} from '../Util';
import {createAnonymousUser} from '../menu/AccountManager';
import {isStatusBarTextVisible} from '../managers/StatusBarManager';
import { appGet, isResponseOk } from '../http/HttpClient';
import { getConnectionErrorHtml } from '../local/404';
import { getBooleanItem, getItem } from '../Util';
import { createAnonymousUser } from '../menu/AccountManager';
import { isStatusBarTextVisible } from '../managers/StatusBarManager';

export class CodeTimeView implements Disposable, WebviewViewProvider {
private _webview: WebviewView | undefined;
Expand All @@ -29,6 +29,10 @@ export class CodeTimeView implements Disposable, WebviewViewProvider {
// its not available to refresh yet
return;
}
if (!getItem('jwt')) {
await createAnonymousUser();
}

this._webview.webview.html = await this.getHtml();
}

Expand Down

0 comments on commit d239da9

Please sign in to comment.