【问题标题】:FireBase getAuth and onAuthStateChanged is not in "firebase/auth" importFireBase getAuth 和 onAuthStateChanged 不在“firebase/auth”导入中
【发布时间】:2021-11-08 08:51:52
【问题描述】:

我正在尝试学习和使用“firebase/auth”中的 getAuth()、onAuthStateChanged() 方法 但是在 node_modules/firebase/auth 的文件夹中不包括这些方法。 似乎根据firebase的版本更改目录。 我的依赖:

"dependencies": {
    "antd": "^4.16.13",
    "firebase": "^9.0.2",
    "next": "11.1.2",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },

Firebase doc 说我应该使用 import { getAuth, onAuthStateChanged } from "firebase/auth"; 我搜索并发现"firebase/firebase-auth"; 文件夹包含getAuth 方法但是当我尝试使用import {getAuth} from "firebase/firebase-auth"; 它给我一个错误状态firebase/firebase-auth 文件未导出。

我错过了什么?
我如何将 getAuth 和 onAuthStateChanged 方法与此依赖项一起使用?
谢谢你的建议

Firebase.js:

// import "firebase/auth"
import "firebase/compat/auth"
//import {getAuth} from "firebase/firebase-auth";
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';

import { getAuth, onAuthStateChanged } from "firebase/auth";


// onAuthStateChanged(auth, user => {
//     // Check for user status
// });

const app = firebase.initializeApp({
    apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
    authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
    projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
    storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
    messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGIN_SENDER_ID,
    appId: process.env.REACT_APP_FIREBASE_APP_ID
})


export const auth = getAuth(app);

export default app

【问题讨论】:

  • 可以分享代码吗?
  • 为什么要导入getAuth 两次?第一个看起来无效
  • @Phil 我不是想在这里表明我取消了他们两个的注释来告诉你他们两个都没有工作!
  • 是错误还是警告?我曾经在 JS 项目(在 Webstorm 中)中得到类似的警告。在 TS 项目中完美运行,仍在尝试调试 JS 项目。另外为什么你也有兼容的进口?使用其中一个?
  • @Dharmaraj 我已经尝试了所有可能的方法,但没有任何效果。问题在于我没有从 firebase/auth 导出的 gethAuth() 函数。文档说我应该使用 firebase/auth 目录中的那个函数

标签: javascript reactjs firebase firebase-authentication


【解决方案1】:
import React, { useState } from 'react'
import { initializeApp } from "firebase/app";
import { getAuth, onAuthStateChanged, GoogleAuthProvider, signInWithPopup, signOut } from "firebase/auth";
import './App.css';

// import { getAnalytics } from "firebase/analytics";

// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

const firebaseConfig = {
  apiKey: 
  authDomain:
  databaseURL: 
  projectId: 
  storageBucket: 
  messagingSenderId: 
  appId: 
};


initializeApp(firebaseConfig);
// const analytics = getAnalytics(app);
const firebaseApp = initializeApp(firebaseConfig);
const provider = new GoogleAuthProvider();
const auth = getAuth(firebaseApp);

export default function App() {
  const [user, setUser] = useState(null)
  onAuthStateChanged(auth, (user) => {
          if (user) {
            // User is signed in, see docs for a list of available properties
            // https://firebase.google.com/docs/reference/js/firebase.User
            setUser(user);
            // ...
          } else {
            // User is signed out
            // ...
            setUser(null)
            }
          }
        );

  return <>{user ? 
  <><p>Welcome</p><button onClick={() => signOut(auth)}>Google Logout</button></>
  : <><p>Enjoy</p>
  <button onClick={() =>  signInWithPopup(auth, provider)
                  .then((result) => {
                    // The signed-in user info.
                    // eslint-disable-next-line
                    const user = result.user;
                    // ...
                  }).catch((error) => {
                    // Handle Errors here.
                    // eslint-disable-next-line
                    const errorCode = error.code;
                    // eslint-disable-next-line
                    const errorMessage = error.message;
                    // The email of the user's account used.
                    // eslint-disable-next-line
                    const email = error.email;
                    // The AuthCredential type that was used.
                    // eslint-disable-next-line
                    const credential = GoogleAuthProvider.credentialFromError(error);
                    // ...
                  })}>
                      Sign in with Google
                    </button>
  </>}</>
}

【讨论】:

    【解决方案2】:

    根据 firebase docs,“版本 9 兼容代码与版本 8 代码相同,但导入发生了变化。”

    这是一个使用 firebase@9.1.3 的工作项目的 sn-p

    import { firebase } from "@firebase/app";
    
    import { initializeApp } from "firebase/app";
    
    import { getAuth, onAuthStateChanged } from "firebase/auth";
    
    const firebaseConfig =  {
      apiKey: "xxx",
      authDomain: "xxx",
      projectId: "xxx",
      storageBucket: "xxx",
      messagingSenderId: "xxx",
      appId: "xxx",
    };
    
    const app = initializeApp(firebaseConfig);
    const auth = getAuth();
    
    

    【讨论】:

      猜你喜欢
      • 2016-12-29
      • 1970-01-01
      • 2020-02-15
      • 2021-07-15
      • 2020-09-22
      • 1970-01-01
      • 2018-07-20
      • 2019-05-22
      • 2020-11-15
      相关资源
      最近更新 更多