Execution flow in providers, constructors, and app.component.ts

Hi all –

I’m having an execution flow problem with an Ionic 2 app that I can’t quite solve. I’m trying to implement Auth0 in an existing app. All was well with the Auth0 starter app (01-login), including retrieving an auth token from storage in the auth provider constructor before the app.component.ts called a method to validate authentication of the token.

However, in my existing app, the app.component.ts call to authenticate executes before the auth service constructor has returned the token from storage. Attached is a debug screenshot, and below is a link to the Auth0 forum where I have posted the code. I thought perhaps this might be more of an Ionic question – really about execution flow and why it works in the sample app and not in my larger (though not particularly complex) app.

Any ideas on where to look? I’ve combed through the code and I swear it is identical to the sample app from what I can see. I’ve checked all of the module declarations and injections, etc. I think. But obviously I’m missing something.

Thanks,
Marc

Link to Auth0 forum (with code):
https://auth0.com/forum/t/problem-migrating-01-login-code-to-a-larger-ionic-2-app

Debug:

In my experience, trying to figure out what is going to execute when is a fool’s errand, for two reasons:

  • it tends to change frequently as new versions of various code is released
  • it likely isn’t even guaranteed to be consistent anyway

Instead, do your own synchronization. If libraries are returning Promises, make sure the only place you do anything with whatever they’re fetching is done in a then block chained off that promise (similarly with Observables and subscribe).

Thanks for the pep talk, rapropos. I guess I got sidelined by my own insecurity – I just assumed that the code that the Auth0 guys wrote would be of the “rock-solid, best practice” variety and that I was missing something due to my inexperience. I was tempted to write promises but wasted too much time combing through that sample code instead.

Your response encouraged me to dig in and go for it, and I solved my problem in about half an hour, and added some additional token authentication features for expired tokens using the refresh_token while I was at it. I will post the code over at Auth0 once I am cleaned up. I’m sure it’s not perfect, but it’s doing the job right now and I’d be happy to get comments.

Marc

Posted my code in the Auth0 forum:

https://auth0.com/forum/t/problem-migrating-01-login-code-to-a-larger-ionic-2-app/