Call app.ts after i login Ionic


#1

Hello,

Is there any possibility to load app.ts after successful login? I got custom menu modules that i show to user depending on response permissions I get from server.

This functionality only works if I reload already logged in user, but not working if user login and don’t refresh page.


#2

I have a similar requirement …i’m trying to load a method in app.ts which refreshes user data on side menu.i will post possible way once i get lead.


#3

Hey guys,

You should use an event to refresh your menu once your user is logged in.

in app.ts

    import {Events, ...} from 'ionic-angular';
    constructor( private events: Events) {}
    // update left menu content when the user signs in
    events.subscribe('user:signedIn', (userEventData) => {
      this.user = userEventData[0];
    });

in your login components

   this.events.publish('user:signedIn', user);

#4

Works like a charm.
Thx


#5

Can u elaborate events,subscribe implementation.


#6

This is my app constructor:

  constructor(
    platform: Platform,
    private servis: Servis,
    private login_model: LoginModel,
    private events: Events) {


      //Event koji se poziva kako bi setirali sve module i to kad se user logira
      events.subscribe('user:signedIn', (userEventData) => {
        this.modules = StorageUtils.getModules();
        this.pages = [];

        for (var prava_pristupa of this.modules) {
          if(prava_pristupa == 1)
          {
            this.pages.push({title: 'Pohrana backup i raspored', component: PohranaBackup, icon: 'ios-disc-outline', class: 'pohrana_backup'});
            this.pages.push({title: 'Password manager', component: PasswordManager, icon: 'md-key', class: 'password_manager'});
          }
          if(prava_pristupa == 3)
          {
            this.pages.push({title: 'Kopiranje izlaznih računa', component: KopiranjeIzlaznihRacuna, icon: 'card', class: 'izlazni'});
            this.pages.push({title: 'Kopiranje ulaznih računa', component: KopiranjeUlaznihRacuna, icon: 'card', class: 'ulazni'});
          }
        }

        servis.moduliSet(this.pages);

        if (StorageUtils.hasAccount()) {
          this.rootPage = HomePage;
        } else {
          this.rootPage = Login;
        }
      });

    platform.ready().then(() => {
      StatusBar.styleDefault();

      this.login_model.renew_check();
      
    });

And from my login.ts page I just load event if user successfully login

this.login_model.login(username, password).subscribe(data => {
            this.login_model.store_data(data);
            this.events.publish('user:signedIn', 'login');
        }, error => {
            this.login_failed_alert();
        });

#7

I,m getting following error

Error: Uncaught (in promise): TypeError: Cannot read property ‘subscribe’ of undefined


#8

this.events.publish('user:signedIn', 'login');

This part is where I’m calling event.

Did you pass some parameters in event?


#9

i,m using the following line
this.cusname is username of customer.
this.events.publish('user:signedIn', this.Cus_name);

whether u have implemented anything inside decorator… any provider/directive u included??
@component
{

}


#10

Check if this.Cus_name is not undefined.


#11

even gave hardcoded value…still same error.

whether u have implemented anything inside decorator… any provider/directive u included??


#12

Events in constructor and imported it.

  constructor(
    private nav: NavController,
    form: FormBuilder,
    private login_model: LoginModel,
    private events: Events) {

#13

i have implemented that in both App.ts and login.Ts


#14

I spent Hours on this issue, but this few lines of codes did the magic. God bless u dbaq.
Pls note if u have something like this
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private events: Events) {
platform.ready().then(() => {
}

make sure that dbaq’s code is pasted before the platform.ready(). I mean like this
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private events: Events) {
events.subscribe(‘user:signedIn’, (userEventData) => {
this.subscriber_avatar = userEventData.avatar;
this.membership = userEventData.membership;
this.email = userEventData.email;

});
platform.ready().then(() => {

}