0
votes

I created a service file to manage my firebase user authentication. But, I am getting errors for the imports that are found in the auth.service.ts file. Below is the error followed by the imports and the code from the ts file.

ERRORS:

ERROR in src/app/core/auth.service.ts(2,10): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AngularFireAuth'. src/app/core/auth.service.ts(2,27): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AngularFireDatabase'. src/app/core/auth.service.ts(2,48): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'FirebaseAuthState'. src/app/core/auth.service.ts(2,67): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AuthProviders'. src/app/core/auth.service.ts(2,82): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AuthMethods'. src/app/core/auth.service.ts(2,95): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AngularFire'.

i 「wdm」: Failed to compile.

Imports:

import { AngularFireAuth, AngularFireDatabase, FirebaseAuthState, AuthProviders, AuthMethods, AngularFire } from "angularfire2";

angularfire2 is also found in the package.json folder with version number: "^5.0.0-rc.11"

code:

import { Injectable } from '@angular/core';
import { AngularFireAuth, AngularFireDatabase, FirebaseAuthState, AuthProviders, AuthMethods, AngularFire } from "angularfire2";
import { Router } from "@angular/router";

@Injectable({
  providedIn: 'root'
})


export class AuthService {
  authState: FirebaseAuthState = null;

  constructor(private af: AngularFire, private db: AngularFireDatabase, private router: Router) {
    af.auth.subscribe((auth) => {
      this.authState = auth;
    });
  }


  //Return true if user is logged in
  get authenticated(): boolean {
  return this.authState !== null;
}
  //Returns current user
  get currentUser(): any {
  return this.authenticated ? this.authState.auth : null;
}

 //Returns current user UID
 get currentUserId(): string {
 return this.authenticated ? this.authState.uid : '';
}

// Anonymous User
get currentUserAnonymous(): boolean {
return this.authenticated ? this.authState.Anonymous : false
}

//Returns current user display name or guest
get currentUserDisplayName(): string {

if (!this.authenticated) { return 'GUEST' }
else if (this.currentUserAnonymous) { return 'ANONYMOUS'}
else { return this.authState.auth.displayName || 'OAUTH USER'}

}

//login through a Gmail account
googleLogin(): Promise<FirebaseAuthState> {
return this.socialSignIn(AuthProviders.Google);
}

private socialSignIn(provider: number): Promise<FirebaseAuthState> {
  return this.af.auth.login({provider, method: AuthMethods.popup})
  .then(() => this.updateUserData() )
  .catch(error => console.log(error));
}

private updateUserData(): void {
  //Writes user name and email to realtime db
  //useful if your app displays information about users or for admin features

  let path = `users/${this.currentUserId}`; //Endpoint on Firebase
  let data = { name: this.currentUser.displayName, email: this.currentUser.email, }

  this.db.object(path).update(data)
  .catch(error => console.log(error));
}


}
2
I'll try to find out. But it is something related to ES5 configuration and node. Try to add node to your tsconfig.spec.json types array.Bruno João
try to do this. npm install firebase angularfire2@next --savedev-assassin
The following is my tsconfig.spec.json code: { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/spec", "module": "commonjs", "types": [ "jasmine", "node" ] }, "files": [ "test.ts", "polyfills.ts" ], "include": [ "*/.spec.ts", "*/.d.ts" ] }Zakariah Siyaji
@ninjadev1030 I tried the command that you posted but I got a bunch of npm errorsZakariah Siyaji

2 Answers

1
votes

The error messages are pretty clear. You import from wrong paths. Below are some correct examples, look at the import locations.

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase } from 'angularfire2/database';
1
votes

You just need to update your import statements. For example, to import the auth and db modules, you should do:

import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';

The documentation gives a full upgrade path.