@@ -10,14 +10,24 @@ angular
1010 }
1111 ) ;
1212
13- function HubSpotFieldsController ( $routeParams , umbracoFormsIntegrationsCrmHubspotResource , pickerResource , overlayService , notificationsService ) {
13+ function HubSpotFieldsController ( $scope , $ routeParams, umbracoFormsIntegrationsCrmHubspotResource , pickerResource , overlayService , notificationsService ) {
1414 var vm = this ;
1515
1616 vm . authorizationCode = "" ;
1717 vm . authenticationUrl = "" ;
1818 vm . loading = true ;
1919 vm . authorizationStatus = "Unauthenticated" ;
2020
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+
2131 function getFieldsForMapping ( ) {
2232
2333 // Get the fields for the form.
@@ -69,12 +79,16 @@ function HubSpotFieldsController($routeParams, umbracoFormsIntegrationsCrmHubspo
6979 } ;
7080
7181 // 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 ;
7384 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 ( ) ;
7787 }
88+ } ;
89+
90+ window . addEventListener ( "message" , ( event ) => {
91+ receiveMessage ( event ) ;
7892 } , false ) ;
7993
8094 function handleAuthorizationCallback ( response ) {
@@ -118,12 +132,18 @@ function HubSpotFieldsController($routeParams, umbracoFormsIntegrationsCrmHubspo
118132 umbracoFormsIntegrationsCrmHubspotResource . deauthorize ( ) . then ( function ( response ) {
119133 if ( response . success ) {
120134 vm . authorizationStatus = "Unauthenticated" ;
135+ umbracoFormsIntegrationsCrmHubspotResource . getAuthenticationUrl ( ) . then ( function ( response ) {
136+ vm . authenticationUrl = response ;
137+ vm . loading = false ;
138+ } ) ;
121139 notificationsService . showNotification ( {
122140 type : 0 ,
123141 header : "De-authorization succeeded" ,
124142 message : "Your Umbraco Forms installation is no longer connected to your HubSpot account" ,
125143 } ) ;
126144 getFieldsForMapping ( ) ;
145+ $scope . oauthCountWatcher = 0 ;
146+ vm . oauthCode = "" ;
127147 } else {
128148 notificationsService . showNotification ( {
129149 type : 2 ,
0 commit comments