1
1
import { useCallback , useMemo , useRef } from "react" ;
2
+ import { useIntl } from "react-intl" ;
2
3
import { useAsyncFn , useEffectOnce , useEvent } from "react-use" ;
3
4
4
5
import { useConfig } from "config" ;
@@ -7,10 +8,13 @@ import { DestinationAuthService } from "core/domain/connector/DestinationAuthSer
7
8
import { isSourceDefinitionSpecification } from "core/domain/connector/source" ;
8
9
import { SourceAuthService } from "core/domain/connector/SourceAuthService" ;
9
10
import { DestinationOauthConsentRequest , SourceOauthConsentRequest } from "core/request/AirbyteClient" ;
11
+ import { isCommonRequestError } from "core/request/CommonRequestError" ;
10
12
import { useConnectorForm } from "views/Connector/ConnectorForm/connectorFormContext" ;
11
13
12
14
import { useDefaultRequestMiddlewares } from "../../services/useDefaultRequestMiddlewares" ;
13
15
import { useQuery } from "../useQuery" ;
16
+ import { useAppMonitoringService } from "./AppMonitoringService" ;
17
+ import { useNotificationService } from "./Notification" ;
14
18
import { useCurrentWorkspace } from "./useWorkspace" ;
15
19
16
20
let windowObjectReference : Window | null = null ; // global variable
@@ -46,8 +50,11 @@ export function useConnectorAuth(): {
46
50
queryParams : Record < string , unknown >
47
51
) => Promise < Record < string , unknown > > ;
48
52
} {
53
+ const { formatMessage } = useIntl ( ) ;
54
+ const { trackError } = useAppMonitoringService ( ) ;
49
55
const { workspaceId } = useCurrentWorkspace ( ) ;
50
56
const { apiUrl, oauthRedirectUrl } = useConfig ( ) ;
57
+ const notificationService = useNotificationService ( ) ;
51
58
const { connectorId } = useConnectorForm ( ) ;
52
59
53
60
// TODO: move to separate initFacade and use refs instead
@@ -70,28 +77,55 @@ export function useConnectorAuth(): {
70
77
payload : SourceOauthConsentRequest | DestinationOauthConsentRequest ;
71
78
consentUrl : string ;
72
79
} > => {
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 = {
75
94
workspaceId,
76
- sourceDefinitionId : ConnectorSpecification . id ( connector ) ,
95
+ destinationDefinitionId : ConnectorSpecification . id ( connector ) ,
77
96
redirectUrl : `${ oauthRedirectUrl } /auth_flow` ,
78
97
oAuthInputConfiguration,
79
- sourceId : connectorId ,
98
+ destinationId : connectorId ,
80
99
} ;
81
- const response = await sourceAuthService . getConsentUrl ( payload ) ;
100
+ const response = await destinationAuthService . getConsentUrl ( payload ) ;
82
101
83
102
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 ;
84
128
}
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 } ;
95
129
} ,
96
130
completeOauthRequest : async (
97
131
params : SourceOauthConsentRequest | DestinationOauthConsentRequest ,
0 commit comments