PDA

View Full Version : Setting/getting cookies for login page workaround



aonjavadev
7 Feb 2011, 1:42 PM
I needed a way to set & get a user's username and/or password to have it preset for the login screen. I guess with handheld devices it is a bigger inconvenience to not have that feature.

This workaround utilizes the HTML5 localStorage feature. I found a useful article here (http://mobile.tutsplus.com/tutorials/mobile-web-apps/browser-storage/)that helped me discover this.

in the onSuccess block of your login action add this little if statement


success: function(response){
if( ('localStorage' in window) && window['localStorage'] !== null ){
localStorage.setItem('user_name', loginForm.getValues().username);
localStorage.setItem('password', loginForm.getValues().password);
}
...
}



Then at the bottom of your login.js file (after the formPanel has been created) add this if statement.



if( ('localStorage' in window) && window['localStorage'] !== null ){

if( localStorage.getItem('username') !== null){
loginForm.setValues({username: localStorage.getItem('username')});
}

if( localStorage.getItem('password') !== null){
loginForm.setValues({password: localStorage.getItem('password')});
}
}


Now I am new to HTML5 and Sencha Touch, so I am not sure how much I trust leaving the password in local storage, but at least you can if you want now.

Any thoughts on this?