Skip to content

Commit

Permalink
Merge pull request #77 from supertokens/areeb/fix/refresh-bug-fix
Browse files Browse the repository at this point in the history
feat: Handled the 401 response properly
  • Loading branch information
rishabhpoddar authored Mar 6, 2023
2 parents 071fe42 + 2d5a98a commit 47e2b92
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [0.4.3] - 2023-03-06

- Fixes an issue where entering an incorrect API key would refresh the page

## [0.4.2] - 2023-02-27

- Removes logic where user's email and phone was obfuscated if the demo core connection uri was used
Expand Down
2 changes: 1 addition & 1 deletion build/static/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/static/js/bundle.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dashboard",
"version": "0.4.2",
"version": "0.4.3",
"private": true,
"dependencies": {
"@babel/core": "^7.16.0",
Expand Down
1 change: 1 addition & 0 deletions src/ui/components/auth/SignInWithApiKeyContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SignInWithApiKeyContent = (props: SignInWithApiKeyContentProps) => {
authorization: `Bearer ${apiKey}`,
},
},
shouldRedirectOnUnauthorised: false,
});

const body = await response.json();
Expand Down
13 changes: 7 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ interface IFetchDataArgs {
method: HttpMethod;
query?: { [key: string]: string };
config?: RequestInit;
shouldRedirect?: boolean;
shouldRedirectOnUnauthorised?: boolean;
}

export const useFetchData = () => {
const [statusCode, setStatusCode] = useState<number>(0);

const fetchData = async ({ url, method, query, config, shouldRedirect = true }: IFetchDataArgs) => {
const fetchData = async ({ url, method, query, config, shouldRedirectOnUnauthorised = true }: IFetchDataArgs) => {
const apiKeyInStorage = localStorageHandler.getItem(StorageKeys.AUTH_KEY);

let additionalHeaders: { [key: string]: string } = {};
Expand All @@ -85,7 +85,7 @@ export const useFetchData = () => {
},
},
});
const logoutAndRedirect = shouldRedirect && HTTPStatusCodes.UNAUTHORIZED === response.status;
const logoutAndRedirect = shouldRedirectOnUnauthorised && HTTPStatusCodes.UNAUTHORIZED === response.status;
if (logoutAndRedirect) {
window.localStorage.removeItem(StorageKeys.AUTH_KEY);
window.location.reload();
Expand All @@ -95,9 +95,10 @@ export const useFetchData = () => {
return response;
};

if (statusCode >= 300) throw Error(`Error: ${statusCode}. Some error Occurred`);

return fetchData;
if (statusCode < 300 || statusCode === HTTPStatusCodes.UNAUTHORIZED) {
return fetchData;
}
throw Error(`Error: ${statusCode}. Some error Occurred`);
};

// Language Utils
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
* under the License.
*/

export const package_version = "0.4.2";
export const package_version = "0.4.3";

0 comments on commit 47e2b92

Please sign in to comment.