Deepak
Deepak

Reputation: 493

Check session before making oData calls - HANA Cloud Portal

I am working with HCP Portal SAPUI5 apps. I need to check the session before every data call is made to the backend so I can redirect the user back to the logon page.

In the HANA Cloud documentation, the below code is provided:

jQuery(document).ajaxComplete(function(e, jqXHR) {
  if (jqXHR.getResponseHeader("com.sap.cloud.security.login")) {
    alert("Session is expired, page shall be reloaded.");
    jQuery.sap.delayedCall(0, this, function() {
      location.reload(true);
    });
  }
});

But the above code only works for Ajax calls. I am not sure if the same works for odata as well. We want to redirect the user in every scenario after session expiry. Is there a direct method to achieve it both for data calls and Ajax calls?

Upvotes: 2

Views: 2485

Answers (1)

user1364368
user1364368

Reputation: 1564

You can check in the success callback function for the value of the HTTP response header "com.sap.cloud.security.login":

  sap.ui.getCore().getModel().read("/SOME_ENTITYSet", {

     success: function(odata, response) {
       if (response.headers["com.sap.cloud.security.login"] === "login-request") {
          // Timeout handling
       } else {  
          // Process data in argument odata  
       }
     },

     error: function(error) {
       if (response.headers["com.sap.cloud.security.login"] === "login-request") {
          // Timeout handling
       } else {  
          // Show error message (for non-timeout errors)
       }         
     }
  });  

If have seen cases in which upon a timeout the success callback function has been called; but I have also seen cases in which the error callback function has been called; therefore I check in both cases for a timeout.

The timeout handling might be a dialog telling the user that the session has timed out and asking him if he wants to restart the application.

Upvotes: 1

Related Questions