Authentication with Angular 4 and Firebase 2

I'm having some difficulty learning how to authenticate users using Angular and Firebase.

When I run the Angular app using ng serve in the terminal, I keep getting this ERROR message:

ERROR in /Users/.../Desktop/angular/fireauth/node_modules/angularfire2/angularfire2.d.ts (2,10): Module '"/Users/.../Desktop/angular/fireauth/node_modules/@angular/core/index"' has no exported member 'InjectionToken'.

ERROR in /Users/.../Desktop/angular/fireauth/node_modules/angularfire2/firebase.app.module.d.ts (1,10): Module '"/Users/.../Desktop/angular/fireauth/node_modules/@angular/core/index"' has no exported member 'InjectionToken'.

Answer №1

Encountered the same issue myself. The problem stemmed from a typo in one of my file imports - I mistakenly used angularFire2/... instead of angularfire2/..., resulting in the error.

Here's an example showcasing a basic setup for Angular Firestore integration.

Install angularfire2 and firebase

npm install firebase angularfire2 --save

environments/environment.ts

export const environment = {
  production: false,
  firebase: { // add your API details here
    apiKey: '<your-key>',
    authDomain: '<your-project-authdomain>',
    databaseURL: '<your-database-URL>',
    projectId: '<your-project-id>',
    storageBucket: '<your-storage-bucket>',
    messagingSenderId: '<your-messaging-sender-id>'
  }
}

app.module.ts

Imports along with other necessary imports

// Firebase Modules
import { AngularFireModule } from 'angularfire2';
import { AngularFirestoreModule } from 'angularfire2/firestore';
import { AngularFireAuthModule } from 'angularfire2/auth';
// Auth Service
import { AuthService } from './services/auth.service';
// Environment with Firebase API key
import { environment } from './../environments/environment';

Module Imports & Providers Arrays

imports: [
  BrowserModule,
  AngularFireModule.initializeApp(environment.firebase),
  AngularFirestoreModule.enablePersistence(),
  AngularFireAuthModule,
  RouterModule.forRoot(appRoutes)
]
providers: [
  AuthService,
]

Your Firestore Auth Service

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { User } from './../../models/user.interface';
import * as firebase from 'firebase/app';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/switchMap';

@Injectable()
export class AuthService {

  constructor (
    private afAuth: AngularFireAuth,
    private afs: AngularFirestore,
    private router: Router
  ) { }
  // Your authentication logic goes here.
}

Ensure your read write rules are properly set up in the Firebase console. Also, enable one or more login services through the console.

From console.firebase.google.com/project stuff/database/firestore/rules

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

Lastly, import your auth service and inject it into the component constructor wherever authentication services are needed.

My similar error was due to incorrect importing from angualrFire2, causing issues during ng serve that indicated conflicting modules with casing differences. Make sure to rectify any such discrepancies.

Note: You can also create a generic firestore service to handle CRUD operations for your app akin to our custom auth service.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Bootstrap root.scss is showing an error about an undeclared variable called $theme-colors-rgb

My Angular project is configured for SASS, and I'm integrating Bootstrap 5.2 by importing the necessary scss files within the main style.scss file. My goal is to utilize the Bootstrap Grid and some basic components alongside Angular Material. The cur ...

The component is expected to return a JSX.Element, however it is failing to return any value

The issue lies with this component: const NavigationItems = (props: {name: string, href: string}[]): JSX.Element => { props.map((item, index) => { return <a href={item.href} key={index}>{item.name}</a> }) }; export default Naviga ...

Setting up a custom PrimeNG theme to match our unique style is a great way to

I am currently using the most recent version of "primeng": "^12.2.0", and I am looking to implement my own custom theme for primeng. Despite searching through numerous blogs, I have yet to find a solution. In an attempt to create my cu ...

Promise rejection not handled: The play() function was unsuccessful as it requires the user to interact with the document beforehand

After upgrading my application from Angular 10 to 11, I encountered an error while running unit tests. The error causes the tests to terminate, but strangely, sometimes they run without any issues. Does anyone have suggestions on how to resolve this issue? ...

Angular: Issue encountered while attempting to differentiate an '[object Object]'. Arrays and iterables are the only permissible types for this operation

I encountered the following error message while attempting to retrieve updated data: Error trying to diff '[object Object]'. Only arrays and iterables are allowed Snippet of Get Code: allDatas allData(data) { this.allDatas = data } Up ...

Explore the intricacies of RxJS catchError

I am a beginner in RxJS and I am struggling to understand how the parameters are passed in this code snippet: import { catchError, map, Observable, of } from 'rxjs'; let obs$ = of(1,2,3,4,5); obs$.pipe( map(n => { if (n === 4) { ...

To successfully deploy React with Firebase, it is essential to clear the cache

Recently, I successfully deployed a simple react application that I developed using create-react-app. Following this, I utilized the Firebase CLI to build and deploy the app. However, upon making modifications to the application and redeploying it, I encou ...

Angular: using the filter pipe to render HTML content

When using the pipe, I encounter an issue where the css is not being applied to highlight the searched words in a list. Instead of displaying the yellow background for the searched words, it outputs and displays the tag below: <span class='highlig ...

What is the best way to determine the variable height of a div in Angular 7?

I'm currently trying to use console.log in order to determine the height of a div based on the size of a table inside it. This information is crucial for me to be able to ascertain whether or not a scrollbar will be present, especially within a dialog ...

How is it possible that TypeScript does not provide a warning when a function is called with a different number of arguments than what is expected?

I am working on a vanilla JavaScript project in VS Code and have set up jsconfig.json. Here is an example of the code I am using: /** * @param {(arg: string) => void} nestedFunction */ function myFunction(nestedFunction) { // Some logic here } myFu ...

Adjust the height of a div vertically in Angular 2+

Recently, I started using angular2 and I've been attempting to create a vertically resizable div without success. I have experimented with a directive for this purpose. Below is the code for my directive: import { Directive, HostListener, ElementRef ...

What is the best way to create a type guard for a path containing a dynamic field

In certain scenarios, my field can potentially contain both a schema and an object where this schema is defined by key. How can a guard effectively tackle this issue? Below is an example of the code: import * as z from 'zod'; import type { ZodTy ...

Seems like ngAfterViewInit isn't functioning properly, could it be an error on my end

After implementing my ngAfterViewInit function, I noticed that it is not behaving as expected. I have a hunch that something important may be missing in my code. ngOnInit() { this.dataService.getUsers().subscribe((users) => {this.users = users) ; ...

The primary origin of TypeScript is derived from the compiled JavaScript and its corresponding source map

Being new to sourcemaps and typescript, I am faced with a project that has been compiled into a single javascript file from multiple typescript files. The files available to me are: lib.js (the compiled js code of the project) lib.js.map (the source map ...

Create a hierarchical tree structure using a string separated by dots

I am struggling with organizing a tree structure. :( My goal is to create a tree structure based on the interface below. export type Tree = Array<TreeNode>; export interface TreeNode { label: string; type: 'folder' | 'file'; ...

What could be causing an error with NextJS's getStaticPaths when running next build?

When attempting to use Next.js's SSG with getStaticPaths and getStaticProps, everything worked fine in development. However, upon running the build command, an error was thrown: A required parameter (id) was not provided as a string in getStaticPath ...

Revamp the angular design of the mat-tree UI bottom border line issue

Can you help me with changing the direction of the mat tree from right to left? I need to remove the bottom border, please refer to the image https://i.sstatic.net/ecRIO.png here ...

typescript function intersection types

Encountering challenges with TypeScript, I came across the following simple example: type g = 1 & 2 // never type h = ((x: 1) => 0) & ((x: 2) => 0) // why h not never type i = ((x: 1 & 2) => 0)// why x not never The puzzling part is w ...

Navigating in Angular: How can I direct to a completely new page instead of injecting it into <router-outlet></router-outlet>?

I am in the process of designing a new HTML page that requires a unique header compared to my other pages. Typically, I have been using <router-outlet></router-outlet> to inject content, resulting in consistent headers and footers across all ...

A guide on setting up a countdown timer in Angular 4 for a daily recurring event with the help of Rxjs Observable and the Async Pipe

I'm currently working on developing a countdown timer for a daily recurring event using Angular 4, RxJS Observables, and the Async Pipe feature. Let's take a look at my component implementation: interface Time { hours: number; minutes: numbe ...