Skip to content

Commit

Permalink
update api context provider with auto sign out
Browse files Browse the repository at this point in the history
  • Loading branch information
dancingfrog committed Jul 23, 2024
1 parent d276b94 commit ca37567
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions inst/dist/@cori-risi/cotexts/ApiContextProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface ApiContextType {
apiClient: AxiosInstance;
authenticated: boolean;
authenticated_user: User | null;
autoSignOut: (() => void) | null;
baseURL: string;
token: JWT | null;
data: any;
Expand Down
2 changes: 1 addition & 1 deletion inst/dist/@cori-risi/cotexts/ApiContextProvider.d.ts.map

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

23 changes: 19 additions & 4 deletions inst/dist/@cori-risi/cotexts/ApiContextProvider.js

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

2 changes: 1 addition & 1 deletion inst/dist/@cori-risi/cotexts/ApiContextProvider.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions inst/dist/@cori-risi/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export declare const getMetricText: (data: d3.DSVRowArray<string>, metric: strin
whyItMatters: string;
howToInterpret: string;
};
export declare function autoSignOut(signOut: Function): void;
//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion inst/dist/@cori-risi/utils/index.d.ts.map

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

25 changes: 22 additions & 3 deletions lib/@cori-risi/cotexts/ApiContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import { AuthTokens, JWT } from "@aws-amplify/auth";
// import { AmplifyContext } from "./AmplifyContextProvider";

import "./styles/ApiContextProvider.css";
import {User} from "../models";
import { User } from "../models";
import { autoSignOut } from "../utils";

const BASE_URL = "http://localhost:8000"; // `${import.meta.env.VITE_CORI_DATA_API}`;
// TODO: From now on must pass dev/prod API url in as param to ApiContextProvider because:
Expand Down Expand Up @@ -50,7 +51,7 @@ interface ApiContextType {
apiClient: AxiosInstance;
authenticated: boolean;
authenticated_user: User | null;
// autoSignOut: (() => void) | null;
autoSignOut: (() => void) | null;
baseURL: string;
token: JWT | null;
data: any;
Expand All @@ -68,7 +69,7 @@ const initState: ApiContextType = {
apiClient: apiClient,
authenticated: false,
authenticated_user: null,
// autoSignOut: null,
autoSignOut: null,
baseURL: BASE_URL,
token: null,
data: {},
Expand Down Expand Up @@ -121,6 +122,12 @@ export default function ApiContextProvider (props: {

setState({
...state,
autoSignOut: (!!props.signOut && typeof props.signOut === "function") ? () => {
const { signOut } = props;
(signOut!)();
window.alert("Please refresh this session by clicking the browser's reload button!");
(window as any).location = window.location.protocol + "//" + window.location.host + window.location.pathname;
} : null,
baseURL: (!!props.baseURL) ? props.baseURL : BASE_URL,
setData
});
Expand Down Expand Up @@ -168,6 +175,12 @@ export default function ApiContextProvider (props: {
setState({
...state,
authenticated: true,
autoSignOut: (!!props.signOut && typeof props.signOut === "function") ? () => {
const { signOut } = props;
(signOut!)();
window.alert("Please refresh this session by clicking the browser's reload button!");
(window as any).location = window.location.protocol + "//" + window.location.host + window.location.pathname;
} : null,
baseURL: props.baseURL || BASE_URL,
setData,
token: tokens.idToken!
Expand Down Expand Up @@ -240,6 +253,12 @@ export default function ApiContextProvider (props: {
...state,
authenticated: true,
authenticated_user: u,
autoSignOut: (!!props.signOut && typeof props.signOut === "function") ? () => {
const { signOut } = props;
(signOut!)();
window.alert("Please refresh this session by clicking the browser's reload button!");
(window as any).location = window.location.protocol + "//" + window.location.host + window.location.pathname;
} : null,
baseURL: props.baseURL || BASE_URL,
setData,
token: tokens.idToken!
Expand Down
6 changes: 6 additions & 0 deletions lib/@cori-risi/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,9 @@ export const getMetricText = (data: d3.DSVRowArray<string>, metric: string): {wh
}

}

export function autoSignOut (signOut: Function){
signOut();
window.alert("Please refresh this session by clicking the browser's reload button!");
(window as any).location = window.location.protocol + "//" + window.location.host + window.location.pathname;
}

0 comments on commit ca37567

Please sign in to comment.