cancel
Showing results for 
Search instead for 
Did you mean: 

How to clear the cookie in mobile application when click on logout button?

former_member184238
Participant
0 Kudos

Hi,

I have done one mobile application using SAPUI5 and Gateway service. In that application I have login page. First time when I send the request with wrong credentials  it goes to error function and displaying some error message. second time when I enter valid credentials it goes to success function and connected with back end and I can get the records.

But my problem is third time when I again enter into the application with wrong credentials, it doesn't goes to the error function.It directly goes to the success function and I am getting data from back end.

I used the below code to send the username and password to the back end.

logindialog.setRightButton(new sap.m.Button({

                              text: "Ok",

                              type: "Accept",

                        tap: function(oEvent) {

                                  username = usernameinput.getValue();

                                  password = pwdinput.getValue();

                                  encrypted = btoa(''+username+':'+password+'');

                                     bsydialog.open();

OData.read({

requestUri : "http://192.168.1.25:8000/sap/opu/odata/EAE/PURCHASEORDER_APPROVAL_SRV/PurchaseOrders?$format=json",

headers : {

   Authorization : "Basic "+ encrypted

   } 

}, function(data) {

 

logindialog.close();

var myapp = sap.ui.jsview("view.App");

myapp.placeAt('root');

bsydialog.close();

}, function(err) {

bsydialog.close();

window.alert("Authentication Failed");

});

}

 

}));

I am guessing that the username and password values are stored in cookies.

How can I find those fields and remove those values from cookies in mobile when click on logout button?

Please help me to solve this issue.

Thanks&Regards

Sridevi.G

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Sridevi,

Check out jquery.cookie. It's simple, lightweight jQuery plugin for reading, writing and deleting cookies. There are no problems to combine this plugin with SAPUI5. For deleting a Cookie you can just write the following code in your click-handler:

// Returns true when cookie was found, false when no cookie was found...

$.removeCookie('the_cookie');

Regards Michael

former_member184238
Participant
0 Kudos

Hi,

can you please explain me little bit more.

Hoe could we know the cookie name of username and password to delete.

Thanks&Regards

Sridevi

0 Kudos

You can run your app in a desktop browser like Firefox with FireBug installed and you can go to the "Cookies" tab and see the name of all the cookies and their corresponding values.

Chrome also has this in its Developer Tools under Resources -> Cookies.

Former Member
0 Kudos

Hi Sridevi,

with Kenton's advice you should be able to identify all relevant cookies.

You could also perform following statement to read all cookies in your current domain in one single array.

var cookies = $.cookie(); // => { "the_cookie": "the_value", "...remaining": "cookies" }

After that, you can delete each entry with:

// Returns true when cookie was found, false when no cookie was found...

$.removeCookie('the_cookie');

or...

// Same path as when the cookie was written...

$.removeCookie('the_cookie', { path: '/' });

You can find more details on github.

Regards Michael


former_member184238
Participant
0 Kudos

Hi,

Thanks for your reply . But I am running this application in Android mobile by installing .apk file not in browser. Then how to find those values in mobile.

Please help me.

Thanks&Regards

Sridevi

Former Member
0 Kudos

Hi,

if you have the possibility, please avoid using cookies in hybrid apps. Using HTML5 LocalStorage API instead is a much better way, to store data in (mobile)browsers. Please have a look at this post. You will find some explanations and a simple code example.

The problem of cookie based authentication or storage on hybrid apps is, that you have an inconsistent support of this feature. Besides, Cookies are an archaic feature and deprecated in long term.

Regards

Michael

former_member184238
Participant
0 Kudos

Hi,

I tried like that also.Still I am getting the same problem.

my code is like this

logindialog.setRightButton(new sap.m.Button({

                              text: "Ok",

                              type: "Accept",

                              tap: function(oEvent) {

                                 username = usernameinput.getValue();

                                 password = pwdinput.getValue();

                                 window.localStorage.setItem("username", username);

                                 window.localStorage.setItem("password", password);

                                 encrypted = btoa(''+window.localStorage["username"]+':'+window.localStorage["password"]+'');

                                        bsydialog.open();

                                        var auth = "Basic " + encrypted; 

                       OData.read({requestUri : "https://sapes1.sapdevcenter.com/sap/opu/odata/sap/ZGWSAMPLE_SRV/ProductCollection? $format=json",

                                          headers : {   Authorization : auth} },

                                          function(data) {

                                                      logindialog.close();

                                                      myData.Products = data.results;

                                                      oModel.setData(myData);

                                                      sap.ui.getCore().setModel(oModel);

                                                       bsydialog.close();

                                      }, function(err) {

                                                    bsydialog.close();

                                                    window.alert("Authentication Failed");

                                              });

                                        }

                    }));

and when click on logout button I am using the below code

logouttap : function(){

                              window.localStorage.clear();

                               navigator.app.exitApp();

                    }


still when I am passing wrong credentials second time I am getting the data with out getting any error.

Please suggest me where I am doing wrong

Thanks&Regards

Sridevi

Former Member
0 Kudos

Hmmm, perhaps it's a server-side problem. Have a look at this post, maybe it will help changing the cache control. Besides, have a look at the NetWeaver Gateway Customizing, where you can also edit Gateway-specific cache-settings.

Regards

Michael

Former Member
0 Kudos

Try setting your cookie like this to invalidate it:

                      document.cookie = 'YOURCOOKIE=; max-age=0; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

You cannot un-set it if it's a HttpOnly cookie. And you may need to add a path or domain.