因此,我有一个可路由到页面的javascript文件,每刷新一次页面,“主页”页面就会丢失访问令牌并变为空值。刷新页面时如何防止丢失令牌。因为现在,如果我按主页上的刷新按钮,它将丢失访问令牌,直到我按注销并重新登录。
var Spotify = require('spotify-web-api-js');
var spotifyApi = new Spotify();
class App extends Component {
constructor(){
super();
const params = this.getHashParams(); //tokens saved here
const token = params.access_token;
if (token) {
spotifyApi.setAccessToken(token);
}
this.state = {
loggedIn: token ? true : false,
atoken: token, //access token
};
console.log(spotifyApi.getAccessToken())
//localStorage.setItem(token, this.state.atoken)
}
getHashParams() { //gets users refresh and access token
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
e = r.exec(q)
while (e) {
hashParams[e[1]] = decodeURIComponent(e[2]);
e = r.exec(q);
}
return hashParams;
}
render() {
return (
<Router>
<div className='App'>
<Route exact path = '/' component= {Home}/>
<Route path= '/playlist' component = {Playlist}/>
</div>
</Router>
);
}
}
export default App;