Search code examples
javascriptlocal-storageazure-ad-msal

How to get from local storage when key is crazy


Problem: Library is giving me tokens with long keys. I need the value of a key(refresh-token) so I can make an api request with this token in the headers

Msal is storing tokens to my localstorage with a crazy string, multiple of them. 1 accesstoken, 1 refresh token and a tokenId. I just need to get the refresh token value.. but the key is 35eef60c-0000-0000-0000-20cd77000000.bf390000-0000-0000-0000-528094e00000-login.windows.net-refreshtoken-00000000-0000-0000-bf00-000000000003---- How would I attempt to get this value when the key changes its string and is so long?


Solution

  • You can use Object.keys(localStorage) to get all of the values within localStorage, then proceed to loop over all of them and grab the value for each one.

    const keys = Object.keys(localStorage);
    
    const storageMap = keys.reduce((acc, curr) => {
        acc[curr] = localStorage.getItem(curr);
        return acc;
    }, {});
    
    console.log(storageMap)