Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How can I save Salesforce.com credentials?

Hello! Unfortunately there is a bug in plugin. There is incorrect doLogin function. After you allow changes you're redirected to a public app link and not return to app. Login to Salesforce should be done as shown here https://getsatisfaction.com/apperyio/... (through new browser window)

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

How can I save Salesforce.com credentials?

Hi Maryna,

I have changed the login to use this code:

pre
window.open(url, '_blank', 'location=no');
/pre

Now the login launches a new window, but it still ALWAYS asks to login every time that I open the app, because the token is not found in local storage. (See the screenshot that I will post to Katya's response above.)

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

How can I save Salesforce.com credentials?

Yes, Katya. I have an alert like this:

pre
alert(localStorage.getItem("access_token"));
/pre

But the first time the app loads, it always says "null". See below.

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

How can I save Salesforce.com credentials?

Here is a screenshot showing the alert:

pre
function doLogin () {

Code: Select all

// THIS SHOWS THAT THE LOCALSTORAGE DOES NOT WORK 
alert(localStorage.getItem("access_token")); 

// 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");    
   window.open(url, '_blank', 'location=no');    
 } 
 else { // yes, this is a callback 
   // save token, url into local storag 
   saveAuthCodeInLocalStorage();  
   // navigate to HotOpps page 
   Appery.navigateTo('HotOppsList', {reverse: false}); 

 } 

}
/pre

The first time, it gives me a null (see the screenshot) and after logging in, the page reloads and then it shows me the token.

Image

Nikita
Posts: 0
Joined: Fri Feb 28, 2014 4:02 pm

How can I save Salesforce.com credentials?

Hello!

If access_token saves only after salesforce's login, then before invoke alert() you should check for localStorage.getItem("access_token").

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

How can I save Salesforce.com credentials?

Hi Kikita,

Did you see the code above? The alert that I have above shows that the localStorage.getItem("access_token") returns NULL value. However, it should not.

My code originally said this:

pre
// THIS NEVER WORKS. THE LOCALSTORAGE NEVER SAVES
if (localStorage.getItem("access_token")) {
Appery.navigateTo('xxx', {reverse: false});
}
else {
/pre

However, because of the NULL value being returned the first time the app loads, it was no use to have this code in there.

Also, I will show you the function where the localStorage is saved. Am I doing anything wrong?

pre
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]; 
} 

 // 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

Nikita
Posts: 0
Joined: Fri Feb 28, 2014 4:02 pm

How can I save Salesforce.com credentials?

Hello!

You should check access_token in the function doLogin() after execution of the function saveAuthCodeInLocalStorage();

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

How can I save Salesforce.com credentials?

Hi Nikita,

I do that and it comes back fine. The problem is on the start of the app, the access_token is NULL. See below, the first time the page loads, the first alert comes through as NULL and the second one returns the token. When the page re-loads after logging in, the first alert also has the token.

It appears that the localStorage.setItem function is NOT working? Can you confirm?

pre
function doLogin () {

Code: Select all

 alert(localStorage.getItem("access_token")); 

 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");  
       window.open(url, '_blank', 'location=no');   
     } 
     else { // yes, this is a callback 
       // save token, url into local storag 
       saveAuthCodeInLocalStorage(); 

       alert(localStorage.getItem("access_token")); 

       // 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

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How can I save Salesforce.com credentials?

Hello!
You need to open not just new window prewindow.open(url, '_blank', 'location=no')/pre but add Event Listener, but you don't do that. Please take a look here https://getsatisfaction.com/apperyio/.... Please note you have to invoke your code only after device ready event. Otherwise window.open would open not InAppBrowser, but just in the same window.

Joseph Dindinger
Posts: 0
Joined: Sat Nov 30, 2013 12:27 am

How can I save Salesforce.com credentials?

Hi Maryna,

I am using the same code that is in your standard Salesforce.com contact list plugin that you provide. The only difference, is that in your standard code, you have:

pre
window.open(url, "_self");
/pre

I am a beginning programmer on mobile. When I look at the link to the forum post that you give, I see that you gave code to create an event listener for the Facebook issue. Could you do the same for me here? You have the login code above that I am using (which is from your plugin), so can you construct how it should change to add an Event Listener?

Thank you Maryna!

Return to “Issues”