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));
}
}
npm install firebase angularfire2@next --save
– dev-assassin