Skip to content

Commit b4dfbdb

Browse files
author
Tim Roes
authored
Show an error when OAuth credentials are missing (#19466)
1 parent d97e300 commit b4dfbdb

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

airbyte-webapp/src/hooks/services/useConnectorAuth.tsx

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useCallback, useMemo, useRef } from "react";
2+
import { useIntl } from "react-intl";
23
import { useAsyncFn, useEffectOnce, useEvent } from "react-use";
34

45
import { useConfig } from "config";
@@ -7,10 +8,13 @@ import { DestinationAuthService } from "core/domain/connector/DestinationAuthSer
78
import { isSourceDefinitionSpecification } from "core/domain/connector/source";
89
import { SourceAuthService } from "core/domain/connector/SourceAuthService";
910
import { DestinationOauthConsentRequest, SourceOauthConsentRequest } from "core/request/AirbyteClient";
11+
import { isCommonRequestError } from "core/request/CommonRequestError";
1012
import { useConnectorForm } from "views/Connector/ConnectorForm/connectorFormContext";
1113

1214
import { useDefaultRequestMiddlewares } from "../../services/useDefaultRequestMiddlewares";
1315
import { useQuery } from "../useQuery";
16+
import { useAppMonitoringService } from "./AppMonitoringService";
17+
import { useNotificationService } from "./Notification";
1418
import { useCurrentWorkspace } from "./useWorkspace";
1519

1620
let windowObjectReference: Window | null = null; // global variable
@@ -46,8 +50,11 @@ export function useConnectorAuth(): {
4650
queryParams: Record<string, unknown>
4751
) => Promise<Record<string, unknown>>;
4852
} {
53+
const { formatMessage } = useIntl();
54+
const { trackError } = useAppMonitoringService();
4955
const { workspaceId } = useCurrentWorkspace();
5056
const { apiUrl, oauthRedirectUrl } = useConfig();
57+
const notificationService = useNotificationService();
5158
const { connectorId } = useConnectorForm();
5259

5360
// TODO: move to separate initFacade and use refs instead
@@ -70,28 +77,55 @@ export function useConnectorAuth(): {
7077
payload: SourceOauthConsentRequest | DestinationOauthConsentRequest;
7178
consentUrl: string;
7279
}> => {
73-
if (isSourceDefinitionSpecification(connector)) {
74-
const payload: SourceOauthConsentRequest = {
80+
try {
81+
if (isSourceDefinitionSpecification(connector)) {
82+
const payload: SourceOauthConsentRequest = {
83+
workspaceId,
84+
sourceDefinitionId: ConnectorSpecification.id(connector),
85+
redirectUrl: `${oauthRedirectUrl}/auth_flow`,
86+
oAuthInputConfiguration,
87+
sourceId: connectorId,
88+
};
89+
const response = await sourceAuthService.getConsentUrl(payload);
90+
91+
return { consentUrl: response.consentUrl, payload };
92+
}
93+
const payload: DestinationOauthConsentRequest = {
7594
workspaceId,
76-
sourceDefinitionId: ConnectorSpecification.id(connector),
95+
destinationDefinitionId: ConnectorSpecification.id(connector),
7796
redirectUrl: `${oauthRedirectUrl}/auth_flow`,
7897
oAuthInputConfiguration,
79-
sourceId: connectorId,
98+
destinationId: connectorId,
8099
};
81-
const response = await sourceAuthService.getConsentUrl(payload);
100+
const response = await destinationAuthService.getConsentUrl(payload);
82101

83102
return { consentUrl: response.consentUrl, payload };
103+
} catch (e) {
104+
// If this API returns a 404 the OAuth credentials have not been added to the database.
105+
if (isCommonRequestError(e) && e.status === 404) {
106+
if (process.env.NODE_ENV === "development") {
107+
notificationService.registerNotification({
108+
id: "oauthConnector.credentialsMissing",
109+
// Since it's dev only we don't need i18n on this string
110+
title: "OAuth is not enabled for this connector on this environment.",
111+
});
112+
} else {
113+
// Log error to our monitoring, this should never happen and means OAuth credentials
114+
// where missed
115+
trackError(e, {
116+
id: "oauthConnector.credentialsMissing",
117+
connectorSpecId: ConnectorSpecification.id(connector),
118+
workspaceId,
119+
});
120+
notificationService.registerNotification({
121+
id: "oauthConnector.credentialsMissing",
122+
title: formatMessage({ id: "connector.oauthCredentialsMissing" }),
123+
isError: true,
124+
});
125+
}
126+
}
127+
throw e;
84128
}
85-
const payload: DestinationOauthConsentRequest = {
86-
workspaceId,
87-
destinationDefinitionId: ConnectorSpecification.id(connector),
88-
redirectUrl: `${oauthRedirectUrl}/auth_flow`,
89-
oAuthInputConfiguration,
90-
destinationId: connectorId,
91-
};
92-
const response = await destinationAuthService.getConsentUrl(payload);
93-
94-
return { consentUrl: response.consentUrl, payload };
95129
},
96130
completeOauthRequest: async (
97131
params: SourceOauthConsentRequest | DestinationOauthConsentRequest,

airbyte-webapp/src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@
551551
"connector.setupGuide": "Setup Guide",
552552
"connector.setupGuide.notFound": "No Setup Guide found for this connector.",
553553
"connector.exampleValues": "Example {count, plural, one {value} other {values}}",
554+
"connector.oauthCredentialsMissing": "OAuth login is temporarily unavailable for this connector. Please try again later.",
554555

555556
"credits.credits": "Credits",
556557
"credits.whatAreCredits": "What are credits?",

0 commit comments

Comments
 (0)