Authentication: How to know when the authentication is done after pageload?
I use a custom provider to log users in via token. This works great, the user can log in and the session is persisted. When I subscribe to the auth Observable I get this:
1: null
2: { uid: 23123123, ... }
It seems that this library at first emits null, then logs the user in using the token in localStorage, then emits the authStatus. All Good.
My Question is: How do i know when the automatic initial login has finished? If the user is simply not logged in a null is emitted, and nothing after that. Using a timeout is inconvenient and adds unnecessary delays on page load.
got an issue directed related with this question https://github.com/angular/angularfire2/issues/566 for redirect method of login that would help if this is awnsered.
@Kamshak : Timeout is inconvenient. Why not try something similar and call the appropriate methods as required.
constructor(public auth: FirebaseAuth) { this.authState = auth.getAuth(); auth.subscribe((state: FirebaseAuthState) => { this.authState = state; }) }
get authenticated(): boolean { return this.authState !== null && !this.expired; }
get expired(): boolean { return !this.authState || (this.authState.expires * 1000) < Date.now(); }
Don't think it can be solved like that as there is still no way to know when to call authenticated(). To clarify the problem: I want to use this in conjunction with a router so that I can redirect the user to the login page if they are not authenticated. To answer the question "Do I need to redirect the user to a login page" i need to know: "Has firebase tried to log this user in using the local session?".
Right now all I get is this:
a) User is logged in:
b) User is not logged in:
To implement this functionality I need to know at which point "Token Checked" has happened. A simple Promise could do this as this only happens once. Or a method to try and log in using the local session. Or something like this which i don't think is very nice (refering to case b here):

I have also faced this issue. In the past I've used a variable like authReady which is awaited on by anything that wants to check to see the token.
If authReady is false then then the startup code is still running, if is true then the startup code has been run and we can safely determine if we're good to check the status of the users auth.
I find this very important for things like route guards which run on page load if the user enters the site on that page.
I think exposing an Observable or Promise (preferably Observable) with a similar concept to the above would solve this issue.
AngularFire2 no longer provides these auth methods. If this problem still persists open a question on Stackoverflow for Firebase Authentication.