Help with refreshing token (Keep getting token_not_provided error)
See original GitHub issueI have an app that has a search screen that allows users to search for products. My token expires after 60 mins and has a TTL of 7 days so if users come back to the browser say after a day of not searching and start searching I want to refresh the token if it can be refreshed. The search()
function retrieves a list of products which is completely separate from the refreshToken()
function which only attempts to refresh the token. Currently the search()
works completely fine until the token expires. When the token expires it goes to the refreshToken()
but my API endpoint to refresh the token is never called. Instead the get request request in search()
keeps returning a token_not_provided
error which is weird because somehow it is getting deleted or not sent which leads me to believe the problem is my refreshToken()
function
export function getAuthHttp(http, storage) {
return new AuthHttp(new AuthConfig({
headerName: 'Authorization',
headerPrefix: 'Bearer',
noJwtError: true,
globalHeaders: [{'Accept': 'application/json', 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest'}],
tokenGetter: (() => storage.ready().then(() => storage.get('token')))
}), http);
}
search(): Observable<any> {
this.auth.refreshToken();
return this.authHttp.get('')
.map(
(response: Response) => {
return response.json().info;
},
(error: Response) => {
console.log(error);
}
);
}
onSearch(event)
{
let keyword = event.target.value;
this.searchService.search(keyword)
.subscribe(
(loc: Home[]) => this.loc = loc,
(error: Response) => {
console.log(error);
//alert("Session Expired. You must login again.");
}
)
}
refreshToken(): Observable<string> {
console.log(" is token valid",this.isTokenValid());
if ( this.isTokenValid()) {
return this.authHttp.get('http://api.app/api/refresh?token=' + this.token).map((rsp) => {
this.token = rsp.json().token;
this.storage.ready().then(() => this.storage.set('token', this.token));
return this.token;
});
} else {
return Observable.of(this.token);
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (6 by maintainers)
this.auth.refreshToken();
<- this returns an observable, but it doesn’t actually do anything until yousubscribe()
search() should probably look like this:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇♂️