我有一个注销方法,可以从存储中删除令牌。但是在我的服务中,旧令牌仍被存储。因此,在发送HTTP请求时,它将以旧用户身份出现。问题似乎出在获得令牌的服务中。获取令牌功能存储令牌,并且永远不会在用户登录时更新
add.service
export class AddService {
token = this.storage.get('token').then(res => {
this.token = res;
});
sendComment(comment, patientID) {
headers = headers.delete('Authorization', '');
// this is old token;
console.log(this.token);
console.log(this.storage.get('token'));
headers = headers.set('Authorization', '' + this.token);
const formData = new FormData();
formData.append('comment', comment);
formData.append('patientID', patientID);
return this.http.post<any>(apiUrl + 'addComment.php', formData, {headers});
}
}
logout.ts
logout() {
console.log('Old token');
console.log(this.token);
this.storage.clear().then (val => {
console.log(val);
console.log('Empty');
});
this.gettoken();
this.router.navigateByUrl('/login');
}