i'm trying to setup Angular application with Firebase authentication. My goal now is to obtain client user's token and data during App Initialization.
In App module I have setup AuthServise as APP_INITIALIZER
providers: [
AuthService,
{
provide: APP_INITIALIZER,
useFactory:loadAuthService ,
deps: [AuthService],
multi: true
},
export function loadAuthService(authService: AuthService): Function
{
return () => { return authService.load() };
}
And my AuthService with 'Load' function looks like this
private _token: BehaviorSubject<string> = new BehaviorSubject<string>(null);
private _fUser: BehaviorSubject<firebase.User> = new BehaviorSubject<firebase.User>(null);
// Returns true when user is looged in localStorage of the client
get isLoggedIn(): boolean {
return (this.localStorageUser !== null) ? true : false;
}
get localStorageUser(): firebase.User {
return JSON.parse(localStorage.getItem('user'));
}
get token(): string {
return this._token.getValue();
}
set token(token: string) {
this._token.next(token);
}
get fUser(): firebase.User {
return this._fUser.getValue();
}
set fUser(fUser: firebase.User){
this._fUser.next(fUser);
}
/**
* Called and waited on during app initialiation.
* Initializing app with auth service
*/
public async load()
{
console.log('Initializing app with auth service');
//Check whether user is in client's local storage
if (this.isLoggedIn){
this.fUser = this.localStorageUser;
const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${this.fUser.uid}`);
try {
this.token = await this.fUser.getIdToken();
} catch (error) {
console.log('getting token error', error);
}
}
}
I don't know why i'm getting error 'this.fUser.getIdToken is not a function' Any suggestions please ? Thank you in forward.