angular - Ionic interceptor utilizing refresh tokens using sqLite async storage -


i have developed angular2 interceptor utilizing refresh tokens (refreshing token , retrying http request) in access_token , refresh_token both stored in localstorage.

the following code refreshing token in "authservice":

refreshtoken():observable<response> {   let reftoken: string = localstorage.getitem("bdrefreshtoken");   let username: string = localstorage.getitem("bdusername");   let body = "grant_type=refresh_token"             +"&client_id=" + settings.logininfo.client_id             +"&client_secret=" + settings.logininfo.client_secret             +"&scope=" + settings.logininfo.scope             +"&username="+username             +"&refresh_token="+reftoken;   let header = new headers({       'content-type': 'application/x-www-form-urlencoded'   });   let ro:requestoptions = new requestoptions({       headers: header   });   return this._http.post(settings.tokenendpoint, body, ro)       //.map(res => res.json())       .map(data => {             let d = data.json();             localstorage.setitem('bdaccesstoken', d.access_token);             localstorage.setitem('bdrefreshtoken', d.refresh_token);             this.saveroles(d.access_token);             return data;         }    )    .catch(error => {         this.logout();         return observable.throw(error)    }); } 

and following part of inherited http class serves interceptor:

get(url:string, options?:requestoptions):observable<response>   {            return super.get(url, this.getauthorizedoptions())        .catch(err => {           if (err && err.status === 401){               return this._authservice.refreshtoken()                 .flatmap(r =>                     super.get(url, this.getauthorizedoptions())                 )                 .catch(err2 => {                     this.redirect();                     return observable.throw(err2);                 });           }           else {               return observable.throw(err);           }       });         }  private getauthorizedoptions():requestoptions   {       let token = localstorage.get('bdaccesstoken');       let header = new headers({         'authorization': 'bearer '+ token       });       let ro = new requestoptions({         headers: header       });       let options = new requestoptions();       if (options.headers) options.headers.delete("authorization");       options.headers = header;       return options;   } 

now, have been trying develop same thing in ionic, however, have not been able make work because of asynchronous nature of ionic storage. retrieving value storage not simple localstorage.get(item), , have like: storage.get(item).then(v => stuff...).

i looking create authservice , inherited http class works interceptor; following standard procedure:

  1. reads access token storage, builds requestoptions based off , tries request.
  2. if results 401, reads refresh token storage, , gets new access token using , stores in storage.
  3. tries original http request using new access token
  4. if still fails, redirects user login page.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -