Unable to login in app

Hi, guys, I’m trying to implement a role-based login but got stuck, i’m getting the response from the server correctly but I think something is wrong with my logic…can anybody please show me a way out…
Thanks.

in the JSON response the ‘status’ and ‘role’ field are of type number, in your code you are comparing number value with string ‘0’ & ‘1’, you need to remove quotes from the conditional statements

if (dataObj.status === 0 && dataObj.role === 0) { // <-- here
this.currentUser = {
name: dataObj.name,
role: 0
};
resolve(true);
} else if (dataObj.status === 0 && dataObj.role === 1) { // <-- here
this.currentUser = {
name: dataObj.name,
role: 1
};
resolve(true);
}

Ohh, thanks man, that is so silly of me… how come I didn’t noticed that!!
Thanks a lot brother you made my day.

Congratulations. By editing your initial post to remove the code, you have rendered the thread considerably less useful to anybody else who might have benefited from it in the future, ensuring that you and only you get the value of the assistance you received.

That’s not my intention I did removed it because I’m now trying to implement token based auth with the same code and will repost it as I’m facing issues.
Just give some spare time and you’ll know.

Actually let me provide it for your satisfaction.

Hi, guys, I’m trying to implement a role-based login but got stuck, i’m getting the response from the server correctly but I think something is wrong with my logic…can anybody please show me a way out…
Thanks.

login.ts:

export class LoginPage {
  user = {
    id: '',
    pass: '',
  };
  rootPage:any = LoginPage;
  constructor(public navCtrl: NavController, public navParams: NavParams, private authProvider: AuthProvider, private alertCtrl: AlertController) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }
  loginUser() {
    this.authProvider.login(this.user).then(success => {
      if (success) {
        this.navCtrl.setRoot(MenuPage);
      } else {
        let alert = this.alertCtrl.create({
          title: 'Login failed',
          message: 'Please check your credentials',
          buttons: ['OK']
        });
        alert.present();
      }
    });
  }
}

auth.ts:

export interface User {
  name: string;
  role: number;
}
@Injectable()
export class AuthProvider {
  currentUser: User;
  role: number;
  constructor(public http: Http) {
    console.log('Hello AuthProvider Provider');
  }
  login(user) : Promise<boolean> {
    return new Promise((resolve, reject) => {
      this.http.post ('http://api.mylogIn/login.php', JSON.stringify(user))
        .subscribe(data => {
          console.log(data['_body']);
          var dataObj = JSON.parse(data['_body']);
          console.log(dataObj);
        if (dataObj.status === '0' && dataObj.role === '0') {
        this.currentUser = {
          name: dataObj.name,
          role: 0
        };
        resolve(true);
      } else if (dataObj.status === '0' && dataObj.role === '1') {
        this.currentUser = {
          name: dataObj.name,
          role: 1
        };
        resolve(true);
      } else {
        resolve(false);
        }
        }
        
    )});
    }
 
  isLoggedIn() {
    return this.currentUser != null;
  }
 
  isAdmin() {
    return this.currentUser.role === 0;
  }
 
  logout() {
    this.currentUser = null;
  }
}