Skip to content

Commit d1f7066

Browse files
committed
Fix OAuth issue #56
1 parent 38e06e9 commit d1f7066

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/Umbraco.Forms.Integrations.Crm.Hubspot/App_Plugins/UmbracoForms.Integrations/Crm/Hubspot/hubspotfields.component.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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,14 +79,18 @@ 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 = $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
}
78-
}, false);
88+
};
7989

90+
window.addEventListener("message", (event) => {
91+
receiveMessage(event);
92+
}, false);
93+
8094
function handleAuthorizationCallback(response) {
8195
if (response.success) {
8296
vm.authorizationStatus = "OAuth";
@@ -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,

src/Umbraco.Forms.Integrations.Crm.Hubspot/Umbraco.Forms.Integrations.Crm.Hubspot.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<Authors>Umbraco HQ</Authors>
1616
<Company>Umbraco</Company>
1717
<PackageTags>Umbraco;Umbraco-Marketplace</PackageTags>
18+
<PackageIconUrl>hubspot.png</PackageIconUrl>
1819
</PropertyGroup>
1920

2021
<ItemGroup>
19.7 KB
Loading

0 commit comments

Comments
 (0)