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