App does not go through login

Asked

Viewed 25 times

1

I’m trying to validate the login, but is passing straight through and entering the home, I don’t know what could be going on:

import { Component } from '@angular/core';
import {LoginPage} from '../login/login';
import { NavController } from 'ionic-angular';

@Component({ 
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {
      window.localStorage.removeItem('currentuser');
   if(this.isLoggedin()){
       console.log('you are not logged in');
       this.navCtrl.push(LoginPage);
   }
  }
    isLoggedin(){
        if(window.localStorage.getItem('currentuser')){
            return true;
        }
    }
}
  • And if you put if(window.localStorage.getItem('currentuser')){
 return true;
 } else { return false;}

  • The result is the same

  • Your logic is: If there is an item on the site.Log, return true. If not, return false. Right?! At the moment there is something in the saved currentuser?

  • Are you using Ionic 1 or 2?

  • Ionic 2. With the exclamation mark, it worked, thank you!

  • I removed the Ionic tag and left only Ionic 2.

Show 1 more comment

1 answer

2


The problem is in your logic. At first you need to check if you are different from "logged in". For example: !this.isLoggedin(). See below:

if(!this.isLoggedin()){
   console.log('you are not logged in');
   this.navCtrl.push(Login);
}

Therefore, your method isLoggedin() can be checked whether or not there is an item saved in your localStorege with the corresponding value. If available, return true, or return false. Behold:

isLoggedin(){ 
   return window.localStorage.getItem('currentuser'); 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.