cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement OData based BASIC Authentication using HTML, JavaScript for Mobile Apps using Apache Cordova/PhoneGap and datajs-1.1.1.js library

Former Member
0 Kudos

Hello,

I am currently developing an App using HTML, JavaScript, SAP UI5 framework and Apache Cordova/PhoneGap, with OData NetWeaver Gateway service.

I am facing issues with BASIC Authentication. I have written the code to handle both Valid and Invalid credentials. The code for invalid login scenario work in a way on browser, but not in the iOS App created using Apache Cordova/PhoneGap.

Before going on to the App issue on the mobile device, let me explain how it is executing on the browser, so that you may have a clear picture of the issue I am going through.

While executing or testing the App's code in browser, if I enter invalid login credentials and tap on the Login button, the OData Service related code is fired and as the credentials are invalid, the browser displays an SAP Login prompt as shown in below Screen shot # 1.

When I tap on the Cancel button, in the login prompt, that is when the login failure related code written by me, to display Login Error message is displayed, as shown in below Screen Shot # 2.

As I have to develop an iOS Mobile App, I am using Apache Cordova/PhoneGap and placing my HTML , JavaScript code into the PhoneGap project and building the app.

When I test my App by entering invalid credentials and tap on the Login button, nothing happens. The App appears to be in a sort of stand-still. Logically the app has to display the login error message, as shown in above Screen shot # 2, but it doesn't do so.

Moreover, after waiting for about 30 to 40 seconds and may be even more, when I erase the invalid credentials, enter valid credentials and tap on the Login button, the app continues with the logic written for valid credentials.

I am not able to understand why the app behaves this way on the mobile device i.e. the iPad here in this case. Later on, we will be creating an Android version of the same app, using the same code and Cordova/PhoneGap.

For OData calls, I have used the standard datajs-1.1.1.js library and Base64.js library to encrypt the credentials for security.

It would be of great help, if you could guide me in resolving the issue or provide any reference, which might at least give a lead towards the solution.

FYI :

The overall setup is simple, from the Mobile Device, the App will directly connect to the SAP Backend, over the internet, through OData service written in ABAP.

Awaiting for your valuable inputs.

Thanks and Regards,

Suraj Kumar Midgay

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hello Prathik,

The code which I am using for OData based BASIC Authentication, for my Mobile App is as below.

   var onSuccess = function(data) {

   alert("We are Through"); //Just to check that the OData request was sucessful

   };

  

   var onError = function(err) {

  

   switch(err.response.statusCode) {  

  

   case 403 : {

   window.alert("Error Code - 403, Service unreachable ");

   break;

   }

   case 401 : {

   window.alert("The credentials are incorrect or missing!");

   break;  

   }

   }

   };

 

// dataUserName and dataPassword are the two variables, in which I am storing my Username and Password, respectively.

   var connectionRequest = {

   requestUri: "ODATA SERVICE URL GOES HERE/",

   headers: { Authorization : 'Basic ' + Base64.encode(dataUsername + ":" + dataPassword) },

   method: "POST"

   };

  

   OData.request( connectionRequest, onSuccess, onError);

prathik
Participant
0 Kudos

Hi Suraj,

           

            You can refer this blog, It might help you...

Could you please elaborate as to how are you trying to authenticate the oData service request...may be by posting a code snippet of what you have done..

Regards,
Prathik

Former Member
0 Kudos

Hello Midhun,

Thanks for your suggestion. I ahve done so. Hope to find a solution.

Regards,

Suraj Kumar

midhun_vp
Active Contributor
0 Kudos

You can ask this question to the experts in this space,

Former Member
0 Kudos

Hi Suraj,

Hybrid mobile apps doesn't support basic authentication popups,that is the reason you're not getting basic auth popup. You should look at your login failure logic, are you waiting for 401 authorisation failed response ? it seems the event your waiting for on login failure is not getting triggered.

This post might help you:

Regards,

Vijay.

Former Member
0 Kudos

Hello Vijay,

Thanks for the link.

I tried the link given by you, but could not make anything out of it.

It used the code, but it didnt work for me.

Could you kindly explain me about the code from the link shared by you.

Regards,

Suraj Kumar