Skip to content

Commit

Permalink
fix: perform login redirect automatically (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
schottra authored Jan 12, 2021
1 parent 0e18623 commit 9f09aa2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 96 deletions.
2 changes: 0 additions & 2 deletions src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { debug, debugPrefix } from 'common/log';
import { APIContext, useAPIState } from 'components/data/apiContext';
import { QueryAuthorizationObserver } from 'components/data/QueryAuthorizationObserver';
import { createQueryClient } from 'components/data/queryCache';
import { LoginExpiredHandler } from 'components/Errors/LoginExpiredHandler';
import { SystemStatusBanner } from 'components/Notifications/SystemStatusBanner';
import { skeletonColor, skeletonHighlightColor } from 'components/Theme';
import { muiTheme } from 'components/Theme/muiTheme';
Expand Down Expand Up @@ -50,7 +49,6 @@ export const AppComponent: React.StatelessComponent<{}> = () => {
<ErrorBoundary fixed={true}>
<NavBarRouter />
<ApplicationRouter />
<LoginExpiredHandler />
</ErrorBoundary>
</Router>
<SystemStatusBanner />
Expand Down
39 changes: 0 additions & 39 deletions src/components/Errors/LoginExpiredHandler.tsx

This file was deleted.

52 changes: 0 additions & 52 deletions src/components/Errors/test/LoginExpiredHandler.test.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions src/components/data/QueryAuthorizationObserver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export const QueryAuthorizationObserver: React.FC = () => {
if (query.state.error instanceof NotAuthorizedError) {
// Stop all in-progress and future requests
onlineManager.setOnline(false);
// TODO: https://github.com/lyft/flyte/issues/525
// Notify user of unauthorized status
// Trigger auth flow
apiContext.loginStatus.setExpired(true);
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/components/data/apiContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getLoginUrl } from 'models/AdminEntity/utils';
import * as CommonAPI from 'models/Common/api';
import * as ExecutionAPI from 'models/Execution/api';
import * as LaunchAPI from 'models/Launch/api';
Expand Down Expand Up @@ -30,7 +31,9 @@ export const defaultAPIContextValue = {
...WorkflowAPI,
loginStatus: {
expired: false,
setExpired: () => {}
setExpired: () => {
// do nothing
}
}
};

Expand All @@ -44,6 +47,13 @@ export const APIContext = React.createContext<APIContextValue>(

function useLoginStatus(): LoginStatus {
const [expired, setExpired] = React.useState(false);

// Whenever we detect expired credentials, trigger a login redirect automatically
React.useEffect(() => {
if (expired) {
window.location.href = getLoginUrl();
}
}, [expired]);
return {
expired,
setExpired
Expand Down
1 change: 1 addition & 0 deletions src/components/hooks/useFetchableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function createFetchFn<T extends object, DataType>({
return mergedValue;
} catch (error) {
if (error instanceof NotAuthorizedError) {
// Trigger auth flow
apiContext.loginStatus.setExpired(true);
}
return Promise.reject(
Expand Down

0 comments on commit 9f09aa2

Please sign in to comment.