3

I have a string which I need in multiple functions. Therefore I want to save it in a variable.
But when I try to assign it inside a function it doesn't update the variable.

var auth_code = "na";
function safeAuthCode(authcode){
  auth_code = authcode;
  console.log(auth_code);
}

"auth_code" prints just fine in the console at that point, but when I try to use it later it just contains "na".
Not sure what I'm doing wrong tbh :/

Edit:

This is the function in which safeAuthCode is called:

function auth(){
  chrome.identity.launchWebAuthFlow({
    "url": "https://accounts.spotify.com/authorize?client_id="+client_id+
    "&redirect_uri="+ encodeURIComponent(redirectUri) +
    "&response_type=code"+
    "&scope=" + encodeURIComponent(scopes),
    "interactive": true
  },
  function(redirect_url) {
    var url = new URL(redirect_url);
    var code = url.searchParams.get("code");
    safeAuthCode(code);
  });
}
thaiten
  • 47
  • 8
  • There isn't sufficient code available to make an educated guess, if you make a [mcve] you could possibly exclude why this is happening – Icepickle Nov 26 '17 at 09:04
  • Could it be possible that you are creating the variable in more than 1 part of your code? Maybe the saved value gets overwritten in the future. This kind of data should possibly be saved into `sessionStorage` though, that would make a lot more sense. You could then use `getItem` and `setItem` when reading / saving it – Icepickle Nov 26 '17 at 09:15
  • No, it's only set once. The code is part of a chrome extension. Am I still able to use session storage? "window.sessionStorage.setItem('authcode', auth_code);" and "console.log(window.sessionStorage.getItem('authcode'));" just returns null :/ – thaiten Nov 26 '17 at 09:23
  • Oh, it wasn't clear from the tags or the question that your code is an extension. There is however the `chrome.storage` object, that can be used to save data – Icepickle Nov 26 '17 at 09:36

2 Answers2

0

I am assuming that the problem you are having is because of the global variable that either gets overwritten in a different part of the code, or because your code at a certain point in time reloads, and the initial value gets reset.

To save such authentication code, you could make use of the sessionStorage object of your browser.

To make sure you only have 1 such object, you could use the const keyword to define your variables (in case another definition of that variable would come at a later time, you should get an error thrown)

const authorisationSettings = {
  get code() {
    return sessionStorage.getItem('authorisationCode') || 'na';
  },
  set code(value) {
    return sessionStorage.setItem('authorisationCode');
  }
};

function saveAuthorisationCode( code ) {
  authorisationSettings.code = code;
}

saveAuthorisationCode( 'test' );
console.log( authorisationSettings.code );

This snippet doesn't work on stackoverflow, so you can find the jsfiddle here

Icepickle
  • 12,689
  • 3
  • 34
  • 48
-2

It happens because of when your function is executed, in lexical environment of that function is already exist authcode variable and you are trying to set this one instead of global authcode

You need to change name of global variable or param of the fuction...

EVGENY GLUKHOV
  • 106
  • 1
  • 3
  • the global is called `auth_code` and seems to be set inside the function, and is named different from the parameter... – Icepickle Nov 26 '17 at 09:05
  • I don't really know what you mean. I called that function in another function passing the variable authcode which was called differently in that function. I also tried renaming it – thaiten Nov 26 '17 at 09:07