Jammming: Access Token

I am working on the Jammming Project.

The Spotify object has this method:

  getAccessToken() {
    if (accessToken) {
      return accessToken;

    // Check if the access token is in the URL
    const hash = window.location.hash;
    const token = hash.match(/access_token=([^&]*)/);
    const expire = hash.match(/expires_in=([^&]*)/);

    // If we could extract a valid access token & expiring time,
    // save the values and return the access token. Otherwise,
    // redirect to the authentication login page.
    if (token && expire) {
      accessToken = token[1];
      expiresIn = expire[1];
      window.setTimeout(() => accessToken = '', expiresIn * 1000);
      window.history.pushState('Access Token', null, '/');
      return accessToken;
    } else {
      const authenticationUrl =
      window.location = authenticationUrl;

In the case where we have to fetch a new access token by going to the authentication URL: This part doesn’t really update and return the accessToken.

So in the case where the accessToken is not set, and you try to search and call getAccessToken(), and all it does is redirect us, we still don’t have an accessToken.

I tried to implement setting the accessToken after coming back from the authorizing URL - but I don’t know how to implement this, since the page refreshes asynchronously. How would I implement that?