Using backend information from response: Angular

Issue

My backend is giving me an array of text:

['lamIt','lamItLite']

Here is my GET Request to get either version lamIt or lamItLite:

getVersion() {
    const url = this.lamItUrl + this.versionExt + 'lamit/version';
    console.log(this.environment.getConfig('Url'));
    const params = new HttpParams()
        .set('lanId', this.authService.username)
        .set('domain', 'NAM');
    const options = {
        params: params,
        responseType: 'text'as 'text' // Getting back text not JSON.
    };
    return this.http.get(url, options);
}

This is how I am rerouting the user in order to enter to either lamIt or lamItLite once the form is submitted:

onFormSubmit(f: NgForm) {
    if (this.signInForm.valid) {
      this.authService.username = this.signInForm.value.username;
      this.authService.password = this.signInForm.value.password;
      this.authService.loginUser().subscribe(
        response => {
          this.authService.isAuthenticated = true;
          this.authService.storeToken(response['access_token'], response['refresh_token']);
          this.daq.getVersion().subscribe(
            version =>  {
              this.authService.data = version;
              console.log(version);
              if (version === 'lamIt') { // checks if user has lam, it access, backend returned text not string, see daq service.
                this.router.navigate(['/lamit']);
              } else if (version === 'lamItLite') {
                this.router.navigate(['/lamitlite']);  // checks if user has lam it lite access, backend returned text not string, see lam service.
              } else if (version === 'lamIt' || 'lamItLite') {
                this.router.navigate(['/lamit']);  // checks if user has lam it and lamit lite, backend returned text not string, see lam service.
              }
              this.onLoginError(this.groupLoginErrorMessage);

            }
          );
        }, (error) => {
          console.log(error);
          this.onLoginError(this.invalidCredentialsMessage); // wrong password for auth
        }
      );
    }
  }

How can I use what the backend is providing (the array of text mentioned earlier) in the front end to redirect the user to the correct application?

The problem is, I don’t know how to use what the backend is giving me so I can redirect the user to the correct version of the application.

Solution

Need to loop through the array since backend return an array

 this.daq.getVersion().subscribe(
     version =>  {
          this.authService.data = version;
          console.log(version);
          version.foreach((item) => {
               this.router.navigate(['/' + lamit]);
          }) 
          this.onLoginError(this.groupLoginErrorMessage);

     }
 );

Answered By – Sachila Ranawaka

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.