Grinnex.
Grinnex.

Reputation: 1049

Angular Fire - No Provider for InjectionToken (angularfire2.app.options)

Context

I am working with Ionic & Angular & Angular Fire & Firebase. I already made a successfull connection to the Firestore Database and I am able to manipulate the data.

Specifications

   Ionic CLI                     : 6.18.1
   Ionic Framework               : @ionic/angular 6.0.11
   @angular-devkit/build-angular : 13.2.6
   @angular-devkit/schematics    : 13.2.6
   @angular/cli                  : 13.2.6
   @ionic/angular-toolkit        : 6.1.0
   @angular/angularfire          : 7.2.1 

Implementations

App.module.ts

import { environment } from '../environments/environment';
import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { provideAuth, getAuth } from '@angular/fire/auth';
import { provideFirestore, getFirestore } from '@angular/fire/firestore';

@NgModule({
    ...
    imports: [
      ...
      provideFirebaseApp(
          () => initializeApp(environment.firebaseConfig)), 
          provideAuth(() => getAuth()), 
          provideFirestore(() => getFirestore(),
      ),
    ],
    ...
})

Now I am working on the integrattion of Google Authentication. For that I have been following the Angular Fire Repository and of course the Angular Fire - Authentication Documentation. Where's an abbreviated look of what's implemented (just like the docs):

Login.page.ts

import { AuthenticationService } from 'src/app/services/authentication.service';

export class LoginPage implements OnInit {
    
    constructor(private authenticationSvc: AuthenticationService) { }

    login() { this.authenticationSvc.login(); }
}

Authentication.service.ts

import { AngularFireAuth } from '@angular/fire/compat/auth';
import firebase from 'firebase/compat/app';

export class AuthenticationService {

    constructor(private auth: AngularFireAuth) { }

    login() { this.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()); }
}

Problem

Simply put if I use the Authentication.service.ts on the Login.page.ts the application doesn't work anymore. The browser console outputs the following error:

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(LoginPageModule)[AuthenticationService -> AuthenticationService -> AngularFireAuth -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]: 
  NullInjectorError: No provider for InjectionToken angularfire2.app.options!
NullInjectorError: R3InjectorError(LoginPageModule)[AuthenticationService -> AuthenticationService -> AngularFireAuth -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]: 
  NullInjectorError: No provider for InjectionToken angularfire2.app.options!
    at NullInjector.get (core.mjs:11120:1)
    at R3Injector.get (core.mjs:11287:1)
    at R3Injector.get (core.mjs:11287:1)
    at R3Injector.get (core.mjs:11287:1)
    at injectInjectorOnly (core.mjs:4765:1)
    at Module.ɵɵinject (core.mjs:4769:1)
    at Object.AngularFireAuth_Factory [as factory] (angular-fire-compat-auth.js:126:1)
    at R3Injector.hydrate (core.mjs:11457:1)
    at R3Injector.get (core.mjs:11276:1)
    at injectInjectorOnly (core.mjs:4765:1)
    at resolvePromise (zone.js:1262:1)
    at resolvePromise (zone.js:1216:1)
    at zone.js:1329:1
    at _ZoneDelegate.push.6084._ZoneDelegate.invokeTask (zone.js:443:1)
    at Object.onInvokeTask (core.mjs:25535:1)
    at _ZoneDelegate.push.6084._ZoneDelegate.invokeTask (zone.js:442:1)
    at Zone.push.6084.Zone.runTask (zone.js:214:1)
    at drainMicroTaskQueue (zone.js:632:1)

Upvotes: 10

Views: 5797

Answers (3)

Alok Patel
Alok Patel

Reputation: 96

UPDATE:: for angular > 19 Angular > 19: Fix for NullInjectorError with Firebase Integration When integrating Firebase with an Angular app version 19 or above, you might encounter the following error:

NullInjectorError: R3InjectorError(...)[AngularFirestore -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]:
NullInjectorError: No provider for InjectionToken angularfire2.app.options!

Solution In Angular 19 and later, Firebase requires explicitly providing configuration via the FIREBASE_OPTIONS token in the app.config.ts file.

import { ApplicationConfig } from '@angular/core';
import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
import { provideFirestore, getFirestore } from '@angular/fire/firestore';
import { FIREBASE_OPTIONS } from '@angular/fire/compat';
import { firebaseConfig } from './environment';

export const appConfig: ApplicationConfig = {
  providers: [
    { provide: FIREBASE_OPTIONS, useValue: firebaseConfig }, // Provide Firebase config here
    provideFirebaseApp(() => initializeApp(firebaseConfig)),
    provideFirestore(() => getFirestore()),
  ],
};

Upvotes: 1

augustino
augustino

Reputation: 559

for later versions use or atleast doublecheck the correct object name i.e environment.firebase

import { FIREBASE_OPTIONS } from '@angular/fire/compat';


@NgModule({
    ...
    providers: [
        { provide: FIREBASE_OPTIONS, useValue: environment.firebase}
    ],
})

Upvotes: 3

Grinnex.
Grinnex.

Reputation: 1049

Solution

On the file app.module.ts add the following provider object:

import { FIREBASE_OPTIONS } from '@angular/fire/compat';

@NgModule({
    ...
    providers: [
        { provide: FIREBASE_OPTIONS, useValue: environment.firebaseConfig }
    ],
})

All props go to @Saumya answer on this question.

Upvotes: 52

Related Questions