how to load the app.component.ts only after logging in

Asked

Viewed 177 times

-2

I am creating a menu for my application Ionic 3, and I came across the following problem: In this menu I bring data from the user coming from the database, these data I load in the app.component.ts, but it searches this data before the client log in and the error pq still does not have the data, and when I take it to load right away obviously it is necessary to give a refresh in the application so that the data appear in the menu, if contract is without the data. I wanted to know how to get the.Component app to load right after the user logs in. Thank you.

  • Your question is strange, because if you have already developed the app.Component with database query and everything, so you should already know that you need a login system, with token generation and bank verification and everything else a login needs to be the entry port of your application.

  • Who says you don’t? I think you don’t understand the question, I made the menu after creating all this, in the app.Component I give a get on the data of the logged in user, the problem is that this is only possible after it login, and what happens is that when you arrive at the login page the app.Component is already loaded and generates an error on the screen saying that the user id is null, which is really why it hasn’t logged in

  • i wanted to know how to call the app.Component only after the user logs in, I did an if and put it to load only when the id is not null, the problem is that when the user logs in the menu comes empty and only the data appears after giving a F5 on the screen

  • Okay, now it’s clearer, so this you have to define by creating routes.

  • How would it work ? pq realized that the app.Component only loads when the app opens or when it updates the page

  • Face this is subject for an entire course module, is very broad, there is no way to explain here, depends on the structure that the app was made, the language in the back end and many other issues.

  • basically if the id is null is pq the user is not logged in right? then just direct to the login, and this can be done with the routes as @Leandrade mentioned

  • Ricardo that is done, my problem is that the menu always carries null, understood ? the Component app is only loaded once when you open the app, then it is only closed if I update the page, and after the update the data appears

  • Low quality question. Add information to help you.

  • Maury, if you spent a little time looking at the answer below, you would see that it has already been solved!

Show 5 more comments

1 answer

0


Just to put on record how I did to reolver ..

I used Ionic Events,

On my first screen that is loaded after login I put:

ngOnInit():void{
  this.events.publish(configHelper.Events.atualizacaoUserMenu, {});
   let user = JSON.parse(localStorage.getItem(configHelper.storageKeys.user));
   console.log('usuario Logado', user )
}

In the app.:

private checkEvent(): void{
  this.events.subscribe(configHelper.Events.atualizacaoUserMenu, ()=>{ 
    this.loadUser();
  })

This way whenever the first page is loaded after login, a Publish is made in the app.Component, which in turn makes a subscribe that loads the logged in user information to the menu.

Browser other questions tagged

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