I am using firebase in angular 8 to authenticate the user both in frontend and backend. To authenticate the user in the backend I need to send user Id token.
I am using firebase getIdToken to get the token and it works but partially. The error "TypeError: Cannot read property 'getIdToken' of null" occurs when I refresh the page.
I have tried to hard code the token to the getToken() method and it works even on refresh but that is not feasible, so I made the getToken method to return Observable.
That observable is fetched in Http interceptor TokenInterceptorService, to add the token to all the requests.
export class AuthService {
constructor(
public afs: AngularFirestore, // Inject Firestore service
public afAuth: AngularFireAuth, // Inject Firebase auth service
public router: Router,
public ngZone: NgZone // NgZone service to remove outside scope warning
) {}
// Other authentication methods for sign up etc.
// removed here for readability
getToken(): Observable<string> {
// getIdToken() returns promise so using 'from' to
// convert it to an Observable
const result = from(this.afAuth.auth.currentUser.getIdToken()
.then( token => {
console.log(token);
return token;
})
);
return result;
}
}
export class TokenInterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return this.authService.getToken()
.pipe(
switchMap(token => {
const newRequest = request.clone({
setHeaders: {Authorization: `JWT ${token}`}
});
return next.handle(newRequest);
})
);
}
}
I have seen similar questions and I am using that solution with some modification.
I have even tried returning promise from getToken() method but that too doesn't work.