Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jamakase/fix complete oauth error #8054

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions airbyte-webapp/src/hooks/services/useConnectorAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useFormikContext } from "formik";
import { useCallback, useEffect, useMemo, useRef } from "react";
import { useAsyncFn } from "react-use";
import { useCallback, useMemo, useRef } from "react";
import { useAsyncFn, useEffectOnce, useEvent } from "react-use";
import merge from "lodash.merge";

import {
Expand All @@ -19,6 +19,27 @@ import { RequestMiddleware } from "core/request/RequestMiddleware";
import { isSourceDefinitionSpecification } from "core/domain/connector/source";
import useRouter from "../useRouter";

let windowObjectReference: Window | null = null; // global variable

function openWindow(url: string): void {
if (windowObjectReference == null || windowObjectReference.closed) {
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */

const strWindowFeatures =
"toolbar=no,menubar=no,width=600,height=700,top=100,left=100";
windowObjectReference = window.open(url, "name", strWindowFeatures);
/* then create it. The new window will be created and
will be brought on top of any other window. */
} else {
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
}
}

export function useConnectorAuth() {
const { workspaceId } = useCurrentWorkspace();
const { apiUrl, oauthRedirectUrl } = useConfig();
Expand Down Expand Up @@ -77,27 +98,6 @@ export function useConnectorAuth() {
};
}

let windowObjectReference: Window | null = null; // global variable

function openWindow(url: string): void {
if (windowObjectReference == null || windowObjectReference.closed) {
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */

const strWindowFeatures =
"toolbar=no,menubar=no,width=600,height=700,top=100,left=100";
windowObjectReference = window.open(url, "name", strWindowFeatures);
/* then create it. The new window will be created and
will be brought on top of any other window. */
} else {
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
}
}

export function useRunOauthFlow(
connector: ConnectorDefinitionSpecification
): {
Expand Down Expand Up @@ -152,11 +152,7 @@ export function useRunOauthFlow(
[completeOauth]
);

useEffect(() => {
window.addEventListener("message", onOathGranted, false);

return () => window.removeEventListener("message", onOathGranted);
}, [onOathGranted]);
useEvent("message", onOathGranted);

return {
loading: loadingCompleteOauth || loading,
Expand All @@ -168,8 +164,8 @@ export function useRunOauthFlow(
export function useResolveRedirect(): void {
const { query } = useRouter();

useEffect(() => {
useEffectOnce(() => {
window.opener.postMessage(query);
window.close();
}, [query]);
});
}
15 changes: 10 additions & 5 deletions airbyte-webapp/src/hooks/useRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,26 @@ function useRouter<T = any, L = any>(): {
const location = useLocation<L>();
const history = useHistory();
const match = useRouteMatch();
const query = useMemo<T>(
() =>
({
...queryString.parse(location.search), // Convert string to object
...params,
} as T),
[params, location.search]
);

return useMemo(() => {
return {
push: history.push,
replace: history.replace,
pathname: location.pathname,
query: {
...queryString.parse(location.search), // Convert string to object
...params,
} as T,
query,
match,
location,
history,
};
}, [params, match, location, history]);
}, [match, location, history, query]);
}

export default useRouter;
44 changes: 21 additions & 23 deletions airbyte-webapp/src/packages/cloud/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,27 @@ const MainViewRoutes = () => {
const { currentWorkspaceId } = useWorkspaceService();

return (
<>
<Switch>
<Route path={Routes.AuthFlow}>
<CompleteOauthRequest />
</Route>
<Route>
{currentWorkspaceId ? (
<MainView>
<Suspense fallback={<LoadingPage />}>
<MainRoutes currentWorkspaceId={currentWorkspaceId} />
</Suspense>
</MainView>
) : (
<Switch>
<Route exact path={Routes.SelectWorkspace}>
<WorkspacesPage />
</Route>
<Redirect to={Routes.SelectWorkspace} />
</Switch>
)}
</Route>
</Switch>
</>
<Switch>
<Route path={Routes.AuthFlow}>
<CompleteOauthRequest />
</Route>
<Route>
{currentWorkspaceId ? (
<MainView>
<Suspense fallback={<LoadingPage />}>
<MainRoutes currentWorkspaceId={currentWorkspaceId} />
</Suspense>
</MainView>
) : (
<Switch>
<Route exact path={Routes.SelectWorkspace}>
<WorkspacesPage />
</Route>
<Redirect to={Routes.SelectWorkspace} />
</Switch>
)}
</Route>
</Switch>
);
};

Expand Down
4 changes: 2 additions & 2 deletions airbyte-webapp/src/views/CompleteOauthRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from "react";
import { LoadingPage } from "components";
import { useResolveRedirect } from "hooks/services/useConnectorAuth";

const CompleteOauthRequest: React.FC = () => {
const CompleteOauthRequest: React.FC = React.memo(() => {
useResolveRedirect();

return <LoadingPage />;
};
});

export { CompleteOauthRequest };