[Fixed] get data from database when logging then navigate to proper page using angular on ionic

Issue

I had develop an app and there is two path for some users (based on nationality ) and there is a test result for each one then based on the result moves to particular page and this test take once not every time they logging in

Here is the code i used to get data :

ngOnInit() {
  this.http.get<User>(this.env.API_URL + 'auth/user')
    .pipe(
      tap(user => {
      this.user = user
      }
   this.http.get<initial_study>(this.env.API_URL +'auth/result')
    .pipe(tap(initial => {
     this.initial=initial
      }
  }

condition after login

  if(this.initial.result=='مؤهل' && this.user.user_type =='saudi')
     this.navCtrl.navigateForward('/home');
  else if(this.user.user_type =='saudi')
     this.navCtrl.navigateForward('/initial-study');
  else if(this.nonsaudi.result =='مؤهل' && this.user.user_type =='non_saudi')
  else if(this.user.user_type =='non_saudi')
     this.navCtrl.navigateForward('/non-saudi');

data for (user_type and result) it is from backend
this code above is not working

Solution

Nothing really work with angular
but here is what I have did :

I have pass a $result from laravel with login token :

 if($checkResult= 'مؤهل' )
     { $result = '/home';}
 elseif($checkNashonality ='saudi')
     { $result = '/initial-study';}
 else 
    { $result = '/non-saudi';}
 return response()->json([
            'access_token' => $tokenResult->accessToken,
            'token_type' => 'Bearer',
            'result' => $result,    <------ here
            'expires_at' => Carbon::parse(
                $tokenResult->token->expires_at
            )->toDateTimeString()
        ]);

and here is angular side:

 this.authService.login(form.value.email, form.value.password).subscribe(
      data => {
        localStorage.setItem('token', data.access_token);
        localStorage.setItem('result', data.result); <------ here
        this.alertService.presentToast("تم تسجيل الدخول");
        console.log(data)
      },
      error => {
        this.alertService.presentToast("خطأ في اسم المستخدم أو كلمة المرور");
        console.log(error)
      },
      async () => {
        this.dismissLogin();
        this.result = await this.storage.getItem('result') <------ here
        this.navCtrl.navigateForward(this.result); <------ here
      }
    );

Leave a Reply

(*) Required, Your email will not be published