I am using this module in a react program that a component imports this module. I am needing the access token once received to be stored in the global variable but for some reason that I cant figure out it seems the global variables are not changing as desired
let usersAccessToken = '';
const clientId = 'ca47d370807d46718c513653fb3d2e';
const UriRedirect = 'http://localhost:3000/';
let flag = false;
console.log(flag);
console.log(usersAccessToken);
const Spotify = {
getAccessToken: function () {
//
if (usersAccessToken.length>0) {
console.log('step 1');
flag = true;
return usersAccessToken;
} else if (window.location.href.match(/access_token=([^&]*)/) !== null && window.location.href.match(/expires_in=([^&]*)/) !== null) {
**At this point in the code Im wondering why userAccessToken is not stored in the global variable above the module? I know the value is being stored locally but I need that value to be changed globally. when I console.log it it always remains an empty string and also my boolean flags are not changing?
console.log('step 2');
usersAccessToken = window.location.href.match(/access_token=([^&]*)/)[1];
let expirationTime = window.location.href.match(/expires_in=([^&]*)/)[1];
window.setTimeout(() => usersAccessToken = '', expirationTime );
window.history.pushState('Access Token', null, '/');
flag = true;
return usersAccessToken;
} else {
console.log('step 3');
flag = true;
window.location = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&scope=playlist-modify-public&redirect_uri=${UriRedirect}`;
}
},