@@ -10,14 +10,24 @@ angular
10
10
}
11
11
) ;
12
12
13
- function HubSpotFieldsController ( $routeParams , umbracoFormsIntegrationsCrmHubspotResource , pickerResource , overlayService , notificationsService ) {
13
+ function HubSpotFieldsController ( $scope , $ routeParams, umbracoFormsIntegrationsCrmHubspotResource , pickerResource , overlayService , notificationsService ) {
14
14
var vm = this ;
15
15
16
16
vm . authorizationCode = "" ;
17
17
vm . authenticationUrl = "" ;
18
18
vm . loading = true ;
19
19
vm . authorizationStatus = "Unauthenticated" ;
20
20
21
+ vm . oauthCode = "" ;
22
+ $scope . oauthCountWatcher = 0 ;
23
+ $scope . $watch ( 'oauthCountWatcher' , function ( ) {
24
+ if ( $scope . oauthCountWatcher === 1 ) {
25
+ umbracoFormsIntegrationsCrmHubspotResource . authorize ( vm . oauthCode ) . then ( function ( response ) {
26
+ handleAuthorizationCallback ( response ) ;
27
+ } ) ;
28
+ }
29
+ } ) ;
30
+
21
31
function getFieldsForMapping ( ) {
22
32
23
33
// Get the fields for the form.
@@ -69,12 +79,16 @@ function HubSpotFieldsController($routeParams, umbracoFormsIntegrationsCrmHubspo
69
79
} ;
70
80
71
81
// Setup the post message handler for automatic authentication without having to copy and paste the code from the proxy site.
72
- window . addEventListener ( "message" , function ( event ) {
82
+ const receiveMessage = ( event ) => {
83
+ $scope . oauthCountWatcher += 1 ;
73
84
if ( event . data . type === "hubspot:oauth:success" ) {
74
- umbracoFormsIntegrationsCrmHubspotResource . authorize ( event . data . code ) . then ( function ( response ) {
75
- handleAuthorizationCallback ( response ) ;
76
- } ) ;
85
+ vm . oauthCode = event . data . code ;
86
+ $scope . $apply ( ) ;
77
87
}
88
+ } ;
89
+
90
+ window . addEventListener ( "message" , ( event ) => {
91
+ receiveMessage ( event ) ;
78
92
} , false ) ;
79
93
80
94
function handleAuthorizationCallback ( response ) {
@@ -118,12 +132,18 @@ function HubSpotFieldsController($routeParams, umbracoFormsIntegrationsCrmHubspo
118
132
umbracoFormsIntegrationsCrmHubspotResource . deauthorize ( ) . then ( function ( response ) {
119
133
if ( response . success ) {
120
134
vm . authorizationStatus = "Unauthenticated" ;
135
+ umbracoFormsIntegrationsCrmHubspotResource . getAuthenticationUrl ( ) . then ( function ( response ) {
136
+ vm . authenticationUrl = response ;
137
+ vm . loading = false ;
138
+ } ) ;
121
139
notificationsService . showNotification ( {
122
140
type : 0 ,
123
141
header : "De-authorization succeeded" ,
124
142
message : "Your Umbraco Forms installation is no longer connected to your HubSpot account" ,
125
143
} ) ;
126
144
getFieldsForMapping ( ) ;
145
+ $scope . oauthCountWatcher = 0 ;
146
+ vm . oauthCode = "" ;
127
147
} else {
128
148
notificationsService . showNotification ( {
129
149
type : 2 ,
0 commit comments