I'm writing a function to retrieve a token from localStorage, if not there, fetch the token and store it, but I have a bug because the second if never evaluates and console just logs NULL for the first 'failed attempt', then nothing. Here is my onInit function on my search component
ngOnInit() {
this.token = localStorage.getItem('myToken');//null when not loaded
console.log(this.token);//401 invalid acces token
if(this.token == null){
let possibleToken:string = window.location.href;
let position:number = possibleToken.indexOf('access_token=')+13;//13 is the length of 'acces_token='
let lastPos:number = possibleToken.indexOf('&token_type');
if(lastPos !== -1){//if we have token
this.token = possibleToken.slice(position,lastPos);
console.log(this.token);
localStorage.setItem('myToken', this.token);
}
}
this.inputField.valueChanges
.subscribe(inputField => this.searchService.searchTracks(inputField, this.token)
.subscribe(result => {
if (result.status === 400) {
console.log('400 status error');
return;
}else {
console.log(result);
this.searchResults = result.tracks.items;
}
}));
}
Aucun commentaire:
Enregistrer un commentaire