Page 1 of 3

How can I save Salesforce.com credentials?

Posted: Tue Jan 28, 2014 3:03 am
by Joseph Dindinger

I built an app that connects to Salesforce.com in exactly the same way as the Contact app which is provided as a sample does. However, every time I close the app and open it again, it shows me the screen asking for my approval to run the app, and if I have closed out of it or something, it asks me to log in again.

Is there a way to save the credentials or the token or whatever so that it doesn't have to keep asking for the login and permissions?

See the attached screenshots for the pages I mean.

Thank you!

Joseph Image Image


How can I save Salesforce.com credentials?

Posted: Tue Jan 28, 2014 4:05 am
by Alena Prykhodko

How can I save Salesforce.com credentials?

Posted: Tue Jan 28, 2014 6:29 am
by maxkatz

Just to add, right now the token is saved in sessionStorage which expires once you close the window or the browser.


How can I save Salesforce.com credentials?

Posted: Wed Feb 05, 2014 8:48 pm
by Joseph Dindinger

Hi Alena and Max,

I believe I am already using the local storage, not sessionStorage. See the attached screenshot. So, why would it not be working? Or am I misunderstanding?

Thank you!

Joseph
Image


How can I save Salesforce.com credentials?

Posted: Wed Feb 05, 2014 9:37 pm
by Maryna Brodina

Hello! Do verification - if token is set to localStorage, don't invoke login in Salesforce.


How can I save Salesforce.com credentials?

Posted: Wed Feb 05, 2014 9:40 pm
by Joseph Dindinger

So, do you mean, something like this?

pre
If ( localStorage.getItem('access_token') = NULL ) {
etc...
/pre


How can I save Salesforce.com credentials?

Posted: Thu Feb 06, 2014 6:41 pm
by Maryna Brodina

Yes, but you have errors in code: preif (!localStorage.getItem('access_token')) {
//.............
}/pre


How can I save Salesforce.com credentials?

Posted: Wed Feb 12, 2014 12:55 am
by Joseph Dindinger

Thank you!


How can I save Salesforce.com credentials?

Posted: Wed Feb 19, 2014 11:18 pm
by Joseph Dindinger

Hi everyone,

Sorry, but this still does not work. The app will ask me to allow access every single time, it is as if the local storage does not exist. My client is upset about this because they say it is very slow since it requires them to either log in or at least "allow" permissions every single time they load the app.

Here is my code. Do you see anything wrong with it? If not, is it possible that the localstorage isn't working for some reason?

pre
function doLogin () {

Code: Select all

 if (localStorage.getItem("access_token")) { 
   Appery.navigateTo('HotOppsList', {reverse: false}); 
 } 
 else {     
     // as we render all pages into one file, let's check if this is a callback 
     if (window.location.href.indexOf("access_token") == -1) { 

       var client_id = SF_Settings['client_id']; 
       var redirect_url = SF_Settings['redirect_url']; 
       var login_url = SF_Settings['login_url']; 

       var url = login_url + "?response_type=token&display=touch&display=touch&client_id=" +  
         client_id + "&redirect_uri=" + encodeURI(redirect_url); 

       // let's go to Salesforce to login 
       window.open(url, "_self");    
     } 
     else { // yes, this is a callback 
       // save token, url into local storag 
       saveAuthCodeInLocalStorage();  
       // navigate to HotOpps page 
       Appery.navigateTo('HotOppsList', {reverse: false}); 
     } 
 } 

}

function saveAuthCodeInLocalStorage () {

Code: Select all

var url_params = []; 
var params = window.location.href.slice(window.location.href.indexOf('#')+1); 
params = params.split('&');    

for (var i in params) { 

   var item = params[i].split('='); 
   url_params[item[0]] = item[1]; 

   // find the ID for the current user 
   if(i == 2) { 
       var sUserID = item[1].substr(item[1].length - 18); 
       localStorage.setItem('user_id', sUserID); 
   }  
} 

 // save token with type: Bearer 
localStorage.setItem('access_token', 'Bearer ' + decodeURI(url_params.access_token)); 
// save service URL, needed to invoke services 
localStorage.setItem('instance_url', decodeURIComponent(url_params.instance_url)); 
// clear the token from URL 
//window.location.replace(SF_Settings['redirect_url']+"#");  

}
/pre


How can I save Salesforce.com credentials?

Posted: Thu Feb 20, 2014 9:16 am
by Kateryna Grynko

Hi Joseph,

LocalStorage works properly. Let's check the data you save there. For example, display it in an alert.