Hi everyone. I'm trying to giving synchronous behavior to my authentication 
workflow in angular 2 but i can't resolve.

Here's the code:

saveToken(token){
  debugger;
  var expireDate = 0.00034722222;
  //var expireDate = token.expires_in;

  Cookie.set("access_token", token.access_token, expireDate);
  Cookie.set("refresh_token", token.refresh_token, 1);

  console.log("saveToken -> il token passato come param scade in secondi: " + 
token.expires_in);

  //controlli sui cookie contenitori dei due token
  if(Cookie.check("access_token"))
    console.log(Cookie.get("access_token"));
  if(Cookie.check("refresh_token"))
    console.log(Cookie.get("refresh_token"));

  this._router.navigate(['/']);
}

getRefresh(token):Observable<any>{
  debugger;
  console.log("sono dentro get refresh");
  console.log(token);
  let params = new URLSearchParams();
  params.append('grant_type','refresh_token');
  params.append('refresh_token',token);
  params.append('client_id','web_app');

  let headers = new Headers({'Content-type': 
'application/x-www-form-urlencoded; charset=utf-8','Authorization': 'Basic 
'+btoa("web_app:")});
  let options = new RequestOptions({headers: headers});

  return 
this._http.post('http://localhost:9997/oauth/token',params.toString(),options)
    .map(res => res.json());
}

getResource(resourceUrl): Observable<any> {
  debugger;
  var cookie;

  var bool = (!(Cookie.get("access_token")));
  console.log("step in GET RESOURCE");
  console.log("access token e' assente? : "+bool);

  if (!Cookie.get("access_token")) { //controllo sei il token e' sempre vivo
    this.getRefresh(Cookie.get("refresh_token"))
      .subscribe(
        data => this.saveToken(data),
        err => alert('Invalid Credentials')
      );

    cookie = Cookie.get("access_token");

  } else {
    cookie = Cookie.get("access_token");
  }

    var headers = new Headers({
      'Content-type': 'application/x-www-form-urlencoded; charset=utf-8',
      'Authorization': 'Bearer ' +  cookie                                 
//Cookie.get('access_token')
    });
    //a questo punto gli headers sono pronti e il token e' stato assegnato
    var options = new RequestOptions({headers: headers});

    return this._http.get(resourceUrl, options)
      .map((res: Response) => res.text())
      .catch((error: any) => Observable.throw(error().json().error || 'Server 
error'));

}


As you can see, everything starts from getResource that is called from the 
component like this

export class FooComponent {
    //public foo = new Foo(1,'sample foo');
    private stringret : string;
    public errorMessage : string; 
    private id : number = 0; 

    private foosUrl = 'http://localhost:8888/foo';

    constructor(private _service:AppService) {}

    getFoo(){
        this._service.getResource(this.foosUrl)
         .subscribe(
                     data => (this.stringret = data) && this.id++,
                     error =>  this.errorMessage = "Errore!");
    }
}


As i run the console on chrome, i can see immediatly the problem : 
->When i call the getRefresh function and then subscribe to it, the data => 
this.saveToken(data), isn't resolved
Nothing change if i put the subscribe after the map in getRefresh-> cookie 
still get null as value.
Due to asynchronous workflow i think.

How i can solve? thanks in advance

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to