A new problem. Every time I think I got this, something comes up and makes me realize I still have a fairly long way to go.
I understand you are busy but would highly appreciate some help to move forward. I am also looking into how I can get past this but thought of putting it here in case you have time to help.
On my side, I am looking into async/wait of Promise to resolve the problem I am facing. Not sure if that’s the right option.
So, I moved my
AuthorzedUser code into
AuthService. For your reference, I added the three files (AuthService, app.component.ts and app.module.ts) on Plunker to keep this post free of big code. Here is the Plunker link.
Now, the problem I am facing.
Before that, please don’t mind me iterating a known fact: Storage in ionic/storage deals with Promises.
What’s happening is the
silentLogin() method in my
AuthService class calls the
getAuthorizedUser() method (to get the stored access_token) but doesn’t wait for it to return because of which it fails and therefore the app shows LoginPage on the screen instead of HomePage as per the login in
attemptSilentLogin() method in
And right after the above sequence of things happen, the
condole.log() prints the token.
I am copying the console.log output here to show the sequence of things:
In AuthService->getAuthorizedUser(), storedUserToken retrieved storedUserToken is: -undefined-
In AuthService->silentLogin(), NO USER!
In MyApp->attemptSilentLogin(), allowed is: false
In MyApp->attemptSilentLogin(), User not available, setting rootPage as LoginPage!
OPEN database: _ionicstorage plugins/cordova-sqlite-storage/www/SQLitePlugin.js:175
new transaction is waiting for open operation plugins/cordova-sqlite-storage/www/SQLitePlugin.js:106
OPEN database: _ionicstorage - OK plugins/cordova-sqlite-storage/www/SQLitePlugin.js:179
DB opened: _ionicstorage plugins/cordova-sqlite-storage/www/SQLitePlugin.js:80
In AuthService->getAuthorizedUser(), token is: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlZXVkljMVdEMVRrc2JiMzAxc2FzTTVrT3E1...
I would highly appreciate your input on resolving the issue. I know I need to wait for
getAuthorizedUser() to retrieve (from
Storage) and return the token but don’t know how to implement it.
My apologies for the long post.