Not sure if I fully understand your question, but here is what I am doing:
I make the first http call, and then when the fire signs, it calls completeLogin. Then I could start another HTTP call with its own full function and retry the chain.
Here is the component code. The user filled in the registration information and clicked login:
onSubmit() { console.log(' in on submit'); this.localUser.email = this.loginForm.controls["email"].value; this.localUser.password = this.loginForm.controls["password"].value; this.loginMessage = ""; this.checkUserValidation(); } checkUserValidation() { this.loginService.getLoggedIn() .subscribe(loggedIn => { console.log("in logged in user validation") if(loggedIn.error != null || loggedIn.error != undefined || loggedIn.error != "") { this.loginMessage = loggedIn.error; } }); this.loginService.validateUser(this.localUser); }
This calls the loginservice ValidateUser authentication method
validateUser(localUser: LocalUser) { this.errorMessage = ""; this.email.email = localUser.email; var parm = "validate~~~" + localUser.email + "/" var creds = JSON.stringify(this.email); var headers = new Headers(); headers.append("content-type", this.constants.jsonContentType); console.log("making call to validate"); this.http.post(this.constants.taskLocalUrl + parm, { headers: headers }) .map((response: Response) => { console.log("json = " + response.json()); var res = response.json(); var result = <AdminResponseObject>response.json(); console.log(" result: " + result); return result; }) .subscribe( aro => { this.aro = aro }, error => { console.log("in error"); var errorObject = JSON.parse(error._body); this.errorMessage = errorObject.error_description; console.log(this.errorMessage); }, () => this.completeValidateUser(localUser)); console.log("done with post"); } completeValidateUser(localUser: LocalUser) { if (this.aro != undefined) { if (this.aro.errorMessage != null && this.aro.errorMessage != "") { console.log("aro err " + this.aro.errorMessage); this.setLoggedIn({ email: localUser.email, password: localUser.password, error: this.aro.errorMessage }); } else { console.log("log in user"); this.loginUser(localUser); } } else { this.router.navigate(['/verify']); }
}
In my login service, I call the authorization service, which returns the observed token.
loginUser(localUser: LocalUser) { this.auth.loginUser(localUser) .subscribe( token => { console.log('token = ' + token) this.token = token }, error => { var errorObject = JSON.parse(error._body); this.errorMessage = errorObject.error_description; console.log(this.errorMessage); this.setLoggedIn({ email: "", password: "", error: this.errorMessage }); }, () => this.completeLogin(localUser)); }
In the authorization service:
loginUser(localUser: LocalUser): Observable<Token> { var email = localUser.email; var password = localUser.password; var headers = new Headers(); headers.append("content-type", this.constants.formEncodedContentType); var creds:string = this.constants.grantString + email + this.constants.passwordString + password; return this.http.post(this.constants.tokenLocalUrl, creds, { headers: headers }) .map(res => res.json()) }
The point here in this code is to first call the validateUser method of the login service, after responding based on the returned information, if it is valid, I call the loginUser method in the login service. This chain can continue as long as you need it. You can set class level variables to store the necessary information in each method in the chain to make decisions about what to do next.
Please note that you can sign up for a return to the service and process it there, it does not need to return to the component.
Ok, here goes:
public getShows():any { this._ShowsHttpService .getShows() .subscribe( w => this.shows = w, error => this.errorMessage = error, () => this.completeGetShows()); } completeGetShow() { //any logic here to deal with previous get; this.http.get