Need work around to show logout link in side menu on login

Hello Everyone ,

I am working on progressive web app where I have user registration and login . But I am solution how can I enable the logout button in the side menu when user logs in or register .

I have read about the events but did not get much idea how to achieve that . For reference I am attaching my screen shot .

Screenshot_1

Trying to understand your objective here : You want a logout button to appear when a user logs in, right?

Why not just add a logout button to the menu beforehand, and disable the menu in all pages where it’s irrelevant to see this menu?

Thanks for your response …
I cannot hide the menu because there are other menu items which i have to display which user can access without login also…

Okay I think I understand your goal now.

Add an ngIf directive to your logout button in the menu :

app.html

  <ion-menu>
    <button ion-button *ngIf="showButton">Log Out</button>
     ....
  </ion-menu>

app.component.ts

  import {Events} from 'ionic/angular';
  
  export class MyApp {
  
     showButton : any=false;  //will not show the log out button by default
      
     constructor(public events : Events){
        
        // Listening for the event when the user has logged in
        this.events.subscribe('loggedin', ()=>{
              this.showButton = true      // will show the log out button now
        }
          
     }
  }

Now, in the .ts file of the page where you want the menu to show the button (the page where a user has logged in), publish the event

page.ts

      import {Events} from 'ionic-angular';
       
      export class MyPage {
           
         constructor(public events : Events){

         }

         ionViewDidLoad(){ 
            
              this.events.publish('loggedin');
         }
      }

This should work.

1 Like

Thanks Sidharth , it really helped me … It saved my day…

Did it work for you? If it did, you can mark that post as the solution for others to refer to

Yes…:grinning:… could you help me with this post

Thanks, is the way more easy to showing menu