code_fish
code_fish

Reputation: 3428

Open Authorization code flow popup, get token and use it on same button click

I am working on a ServiceNow integration with a third party system. Using this integration a user can get the data from a third party system and use it to create a Service Request ticket. For this integration, we have created a widget on the Service Portal. This integration uses the authorization code flow for the authentication.

The widget is part of a catalog item which a user can request from the Service Portal. So the flow is like:

  1. User logins to Service Portal and selects the item.

  2. User selects a record in a reference field on the item. This item also has a button, on which when a user clicks he is presented with an authorization popup, where third party login UI appears. He enters his credentials for the third party system. If the authentication is successful, popup closes and tokens are saved in the 'oauth_credential' ServiceNow table.

What happens behind the scene is I am using the ServiceNow 'Get OAuth Token' functionality from the ServiceNow 'REST message' module. See below:

HTML

 <button type="button" ng-click="checkAccess()" class="btn btn-default">Check Access</button>

Client Script

$scope.authorize = function() {
  glideUserSession.loadCurrentUser().then(function(currentUser) {
    var oauthRequestorSysId = currentUser.userID;
    var oauthRequestorContext = 'sys_user';
    var oauthProfileId = '59f05d7ddbf563003ca7da11cf961962';
    var redirect = "https://dev000.service-now.com/oauth_redirect.do";

    var oauth_initiator_url = '/oauth_initiator.do' +
    '?oauth_requestor_context=' + oauthRequestorContext +
    '&oauth_requestor=' + oauthRequestorSysId +
    '&oauth_provider_profile=' + oauthProfileId +
    '&response_type=code';

     window.open(oauth_initiator_url, "", "height=500,width=800");

     // additional code to use token once popup closes
  });
}

I am able to achieve this till now. This popup saves the tokens into the ServiceNow.

Issue:

Once the authorization happens and popup up closes, the page refreshes. I don't want the page to refresh because the earlier selection made by user in the reference field is lost on page refresh. I have more code in the same authorize function, where I will use the token generated by the authorization code pop up. But the issue is, if I add the code after popup, the popup opens up, but then it pauses and the next line of code gets executed. It seems like popup is asynchronous. Additional code functionality uses the token generated by popup to get data from the third party system. I can't create two buttons, one for authorize and other one to use the tokens. I have to do this in a single button click.

Can I stop the page refresh once popup is closed? Can I add some logic after popup window line and this logic should be executed only once popup is closed.

Upvotes: 0

Views: 1123

Answers (2)

code_fish
code_fish

Reputation: 3428

Thanks for suggestions. I got it working on the page load. On page load, I check for the token in the ServiceNow table, and if tokens are available and valid, I am skipping popup creation logic.

Upvotes: -2

Satpal Tanan
Satpal Tanan

Reputation: 1108

This is just a guess as I don't have your code.

Since you said page is reloading when popup is closed, then there must be some code that is reloading the page as it will not happen automatically. The code could be in the 'message' event handler which is triggered using 'postMessage' (https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) from the popup. You can remove the reload logic from there.

Or

You might be using opener.reload in the popup window, you can remove that if used so your page doesn't reload after popup closed.

Upvotes: 0

Related Questions