Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
fix: clear session everytime we get a 401 (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored Feb 14, 2023
1 parent ad416af commit 99aceba
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
21 changes: 20 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,33 @@ const fetcher = async (uri: String) => {
// If the status code is not in the range 200-299,
// we still try to parse and throw it.
if (!res.ok) {
if (res.status === 401) {
window.localStorage.clear();
window.location.reload();
}

const contentType = res.headers.get('content-type');

if (!contentType || !contentType.includes('application/json')) {
const err = new StatusError(
'An unexpected error occurred.',
await res.text(),
res.status
);
console.log(err);
throw err;
}

let info = '';
info = await res.json();

return new StatusError(
const err = new StatusError(
'An error occurred while fetching the data.',
info,
res.status
);
console.log(err);
throw err;
}

return res.json();
Expand Down
56 changes: 34 additions & 22 deletions src/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,23 @@ async function request(method: string, uri: string, body?: any) {

const res = await fetch(uri, req);
if (!res.ok) {
const err = await res.json();
throw new APIError(err.message, res.status);
if (res.status === 401) {
window.localStorage.clear();
window.location.reload();
}

const contentType = res.headers.get('content-type');

if (!contentType || !contentType.includes('application/json')) {
const err = new APIError('An unexpected error occurred.', res.status);
console.log(err);
throw err;
}

let err = await res.json();
err = new APIError(err.message, res.status);
console.log(err);
throw err;
}

return res.json();
Expand Down Expand Up @@ -221,24 +236,11 @@ export async function deleteConstraint(
//
// evaluate
export async function evaluate(flagKey: string, values: any) {
const req = setCsrf({
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: JSON.stringify({
flagKey,
...values
})
});

const res = await fetch(`${apiURL}/evaluate`, req);
if (!res.ok) {
const err = await res.json();
throw new Error(err.message);
}
return res.json();
const body = {
flagKey,
...values
};
return post('/evaluate', body);
}

//
Expand All @@ -254,8 +256,18 @@ export async function getInfo() {

const res = await fetch(`${metaURL}/info`, req);
if (!res.ok) {
const err = await res.json();
throw new APIError(err.message, res.status);
const contentType = res.headers.get('content-type');

if (!contentType || !contentType.includes('application/json')) {
const err = new APIError('An unexpected error occurred.', res.status);
console.log(err);
throw err;
}

let err = await res.json();
err = new APIError(err.message, res.status);
console.log(err);
throw err;
}

const token = res.headers.get(csrfTokenHeaderKey);
Expand Down

0 comments on commit 99aceba

Please sign in to comment.