I am using Http Service from Angular2 :
My login component:
doLogin() {
this.authenticationService.inValidateSession();
this.authenticationService.userConfiguration=this.httpService.login(this.user.email,this.user.password)
this.router.navigate(['/site']);
}
- Once user submits credentials , dologin with invalidate the old session if any.
- Will do the server login
- And will save the user configuration it will get from the server like name and groups... _ And will redirect to default homepage.
HttpService
login(username:string,password:string) {
let headers = new Headers();
this.createAuthorizationHeader(headers,username,password);
this.http.get(this.url,{headers:headers}).subscribe(this.extractData,this.handleError);
return this.configFromLogin;
}
private extractData(res: Response) {
if(res.status==200){
let body = res.json();
var result={"config":body,
"x-xsrf-token":res.headers.get('x-xsrf-token')};
this.configFromLogin=result;
}
else{
//console.log(error);
// throw exception or use this.error() method
}
}
Since Server provides Token authentication which we need to use for all the Post Requests, i save it in Session.
My Problems or Doubts:
- When i debug doLogin() codes get executed first (saveInSession) even before HttpService extractData is executed. Why ? i know Http get is async call but how can i make doLogin wait ?
- In HttpService i am checking if response is 200 then i am saving it otherwise i want to throw error and use the HandleError method , which i am using in subscribe method of Observable. Is it possible ?
Please help me with above two problems, in case of questions i am available to answer.
Thanks
this.configFromLogin
inHttpService
? - Supamiu