javascript - FireBase getAuth 和 onAuthStateChanged 不在“firebase/auth”导入中
问题描述
我正在尝试学习和使用“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
解决方案
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>
</>}</>
}
推荐阅读
- ios - 在应用更新完成时显示自定义通知?
- python - 如何在 aix 节点上发送带有附件的邮件
- nopcommerce-3.80 - 如何从 nopcommerce 3.8 的产品详细信息页面上的产品属性中删除 [+£xxx] 重量调整
- git-bash - 命令 'yarn --v' 也会启动 'yarn install' 并自动安装软件包。为什么会这样?
- php - 根据 Woocommerce 中的延期交货项目显示隐藏支付网关
- javascript - 在 ASP NET 中序列化 EF 关系
- java - 变量具有私有访问权限 javax.swing.AbstractButton
- ios - iOS -GeoFire ExceptionThrown 'Precision must be less than 23!' 当搜索半径变宽时
- ruby-on-rails - 比较 postgres 和 Rails 中的时间
- javascript - 无法使用 d3 更新 SVG 元素