Page 1 of 1

SecurityContext failure to forward to login page

Posted: Sun Apr 06, 2014 11:56 am
by mehtashail

I am using a GenericSecurityContext for my app which I presume gets called with every service call being made if I select it for my service under Settings - SecurityContext.

Is there a way to automatically navigate to login page on failure if the service is not being run asynchronously?

Under GenericSecurityContext Service I see an Events tab that has Authorization Error, Login Success, Login Error. When do these get triggered and what can we do here? Can they be used for redirection?


SecurityContext failure to forward to login page

Posted: Sun Apr 06, 2014 1:36 pm
by Igor

Hello,

You can use navigate to page code on the service success event.
http://docs.appery.io/documentation/r...
http://docs.appery.io/documentation/j...


SecurityContext failure to forward to login page

Posted: Sun Apr 06, 2014 5:04 pm
by mehtashail

I understand that for a normal REST Service but it seems different for the SecurityContext. Screenshot below. Can I specify the navigate script on on login or authorization error? What is the difference between authorization error and login error.

Image


SecurityContext failure to forward to login page

Posted: Mon Apr 07, 2014 3:16 am
by Alena Prykhodko

Hi Mehtashail.

Please clarify what do you mean by "SecurityContext". Please provide us with a screen shot.

The previous screen shot doesnot show the error.
Thus your service datasource will generate success event.

You have two ways to implement "logout" event.

1 Implement eventHandler function in JS asset and invoke it on every service success event.

2 Set up "generic security context" which implements this function described above. That's a more complex way, but more correct.

Also on your js action you should use something like:

pre
if(data.greeing == "Please log in first")
navigateTo("Screen21");/pre

Please let us know what way is preferrable for you.


SecurityContext failure to forward to login page

Posted: Mon Apr 07, 2014 4:09 am
by mehtashail

Hi Alena,
There is no error and you answered part of my question.

I am already implementing a generic security context. JS looks like this

code
Appery.Token = Appery .createClass(SecurityContext, {
invoke: function(service, settings) {
settings.headers['Authorization'] = "Bearer " + localStorage.getItem('access_token');
Appery.Token.$super.invoke.call(this, service, settings);
}

});
/code

If I understand what you are saying - this should be change as:
code
Appery.Token = Appery .createClass(SecurityContext, {
invoke: function(service, settings) {
settings.headers['Authorization'] = "Bearer " + localStorage.getItem('access_token');
Appery.Token.$super.invoke.call(this, service, settings);
if(data.msg == "Please log in first")
navigateTo("Login");

}

});
/code


SecurityContext failure to forward to login page

Posted: Mon Apr 07, 2014 4:59 am
by Alena Prykhodko

Mehtashail,

No, you should use code on success event http://docs.appery.io/documentation/w...

So look at this code:

pre
Appery.Token = Appery.createClass(SecurityContext, {

Code: Select all

 invoke: function(service, settings) { 
     var oldSuccess = settings.success; 
     var onSuccess = function(value, other){ 

         //here you should make your logout logic. For example. Uncomment it to test. 
         //if(value.msg == "Please log in first"){ 
         //    alert("LOGOUT"); 
         //    navigateTo("Login"); 
         //}; 

         oldSuccess.call(this, value, other); 
     }; 

     settings.success = onSuccess; 
     Appery.Token.$super.invoke.call(this, service, settings); 
 } 

});
/pre