Property 'auth' does not exist on type 'AngularFireAuth'

a 夏天 提交于 2020-12-29 09:09:10

问题


Getting this error when using angularfire. Have checked my imports and they seem to be correct. I have tried to reinstall angularfire however it is still throwing this error. Is there issues with angularfire?

import { Injectable, NgZone } from '@angular/core';
import { User } from "../services/user";
import { auth } from 'firebase/app';
import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
import { Router } from "@angular/router";


@Injectable({
  providedIn: 'root'
})

export class AuthService {
  userData: any; // Save logged in user data

  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
  ) {    
    /* Saving user data in localstorage when 
    logged in and setting up null when logged out */
    this.afAuth.authState.subscribe(user => {
      if (user) {
        this.userData = user;
        localStorage.setItem('user', JSON.stringify(this.userData));
        JSON.parse(localStorage.getItem('user'));
      } else {
        localStorage.setItem('user', null);
        JSON.parse(localStorage.getItem('user'));
      }
    })
  }

  // Sign in with email/password
  SignIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.ngZone.run(() => {
          this.router.navigate(['dashboard']);
        });
        this.SetUserData(result.user);
      }).catch((error) => {
        window.alert(error.message)
      })
  }

回答1:


Had the same problem. Seems starting "@angular/fire": "^6.0.0" AngularFireAuth has dropped the auth property.

Just delete auth & it should work

https://github.com/angular/angularfire/issues/2409#issuecomment-615993136




回答2:


I had the same problem and it was related to version mismatch in package.json. I had

dependencies {
        ...
        "@angular/fire": "latest", // It was 6.0.0
        "firebase": "5.4.2",
        ...
}

Then I changed to

dependencies {
        ...
        "@angular/fire": "5.4.2",
        "firebase": "5.4.2",
        ...
}

Also ensure to import AngularFireAuthModule in your app.module.ts

import { AngularFireAuthModule } from '@angular/fire/auth';

...
@NgModule({
    imports: [
        AngularFireAuthModule
    ],
    ....
})
export class AppModule { }



回答3:


hello noce if it's the truth but this worked for me

1 - npm uninstall firebase @angular/fire

2- package.json

3 - dependencies { ... "@angular/fire": "6.0.0", "firebase": "7.14.1", ... }

4 - change

dependencies { ... "@angular/fire": "^5.4.2",

    ...

5 - save

6 - npm install firebase @angular/fire

if you find another much better solution but for now this worked




回答4:


You should import the right Firebase:

import firebase from 'firebase/app';

and then for example:

// Sign in with Google
GoogleAuth(): any {
    return this.AuthLogin(new firebase.auth.GoogleAuthProvider());
}



回答5:


After checking different combinations, the latest version of @angular/fire that works with Angular 9.1.6 is 5.4.2.

Any version above would not allow you to use the auth AngularFireAuth property, even though I could not find any explicit deprecation notice in the docs.




回答6:


https://github.com/angular/angularfire/blob/master/docs/version-6-upgrade.md

"AngularFireAuth has dropped the auth property and instead Promise Proxies the underlying Firebase auth.Auth instance; allowing your development experience to more closely mirror the JS SDK. Similar changes have been made to AngularFireFunctions, AngularFireMessaging, and AngularFirePerformance."




回答7:


This one happens because of the new update. Check the release note! changelog.

@angular/fire/auth AngularFireAuth has dropped the auth property and instead Promise Proxies the underlying Firebase auth.Auth instance

Just delete the "auth", and it will work



来源:https://stackoverflow.com/questions/60455433/property-auth-does-not-exist-on-type-angularfireauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!