Uncaught (in promise): false when using ionViewCanEnter

Something like (its an example with login credentials like i have in my application):

ionViewCanEnter(){
 this.authService.login(this.loginCredentials).subscribe(allowed => {
      if (allowed) 
      {
          //if you only want to push a page with the back button in it
          this.nav.push(Page2);
          //if you want to generally move to other MAIN Page
          this.nav.setRoot(Page2);
      }
   }
}

and in your authService youll add the login function (i use http post but it works with get also):

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { Http } from '@angular/http';
import 'rxjs/add/observable/throw';

........................

  public login(credentials) 
  {
    if (credentials.email === "" || credentials.password === "") 
    {
      return Observable.throw("Please insert credentials!");
    } 
    else 
    {
      return Observable.create(observer => {
        var url = 'http://yourAPIorServer/User_Login.php';
        this.http.post(url, credentials).subscribe((rsp) => {
          //if the user is authentificated "access" is the output of the server as text
          this.accessgranted = rsp.text(); //i use text but you can use rsp.json as well
          let access = (this.accessgranted === 'access');
          observer.next(access);
          observer.complete();
        });
      });
    }

Its only an example with Login Credentials but you can do it all without. But with this method youll get access == true or accees == false back.

1 Like