In ionic4 when i logout page exist to loginpage but when i pressesd back button that return to home page ,

in ionic4 when i logout page exist to loginpage but when i pressesd back button that return to home page , How to resolve it.

logout() {

this.logOutConfirm();
console.log(“hi”);
localStorage.removeItem(‘user_id’);
localStorage.removeItem(‘Status’);
localStorage.removeItem(‘username’);
localStorage.removeItem(‘userimage’);
localStorage.clear();

this.router.navigate([‘login’]);
//this.navCtrl.setRoot([’/loginPage’]);

localStorage.clear();
}

please help

Use some route guard

if you using angular

inside your router module , add canLoad to the end of route object like this

path: 'someWhere',
component: SomeComponent,
canLoad: [AuthGuard]

then inside your Auth Guard implement the logic to make sure that user is already log in to access the SomeComponent or Home Page

Can we use without authGuard, because i use everything without any services.

Yes

you can create simple guard inside your component

inside constructor or maybe ngOnInit

for example if your app need to know if user ID is exist in the storage , then get alive

you can use this

first you need the function like this

async isUserHere() {
  const userId = await  storage.get({key: 'userId'});
  if (userId.value) {
     return true;
  } else {
     this.router.navigateByUrl('/login');
     return false;
  }
}

and inside your contructor do this

constructor(private router: Router) {
  this.isUserHere();
}

Hello, Check out this tutorial. Here it showing how to add auth guard, he added for intro slide but you can use also for login & logout. I am using Auth guard and it’s working good.

My problem is when i logout ,return on login, but press device back-button that’s return again dashboard.

I thinks that’s because of the component lifecycle and caching ecosystem

read this article to understand this more in details

Cache Components With RouterReuseStrategy in Angular

Thanks for your reply but already i have checked this but result same.

write this code inside your home page ts file

ionViewWillEnter() {
  alert('view will enter');
}

check if it will apear when you use mobile back button to get back

if it’s shown , then move that simple guard in my first post , inside this ionViewWillEnter()

thanks but this couldn’t solve.

put console log every where

and try to see whats going on behind the screen

find out the point you need to verify is user log in

you have so many powerful controls to manage component state and life cycles

ngOnViewInit
ngOnContentInit
ngOnContentDidInit
ngOnViewDidInit

and so on

Browser back button will do same, like mobile back button

@keshavking111 then definitely you have to use a auth guard. just add a object to store the user details in storage when you do the login and clear the object when u do the log out.the check whether userdetails object is empty or not in authgurad.if empty route user to login.else route to home page.please make sure to use best practices when u do the coding.u can find sample project here https://github.com/KasunGamage/ionic-4-best-app

Thanks @KasunGamage for reply, i want home page logout and go to login-page ,and pressed back button of device ,then exist app. But i logout object clear, reach at login page and pressed back button return again reached home page without user_id.

You need to use your navigation controller to remove all the components from stack.

When navigating to login page, use

this.navCtl.navigateRoot(’/login’)

It will set the login page as root for your router. Then subscribe for the android back button pressed.

Hope it help!

thanks @mateen1993 , Already i have tried but problem same.

@keshavking111 change the functionality for back button on login page, and exit the app if user presses it. And once user logs in, make its settings to default. Since you are not using auth guard, this is the only work around i think.

Use service that checks weather user is logged in and when you loggout set the boolean value to false or true as applicable u need to override back hardware back button to let user logout before leaving the logged in page

thanku @AdityaJangle , i was use on loginpage :
ionViewDidEnter(){
this.subscription = this.platform.backButton.subscribe(()=>{
navigator[‘app’].exitApp();
});
console.log(“currentpage”,this.subscription);
}

ionViewWillLeave(){
this.subscription.unsubscribe();
}

ionViewDidEnter wont work for ionic 4 you need to use ionViewWillEnter