我試圖只讓注冊用戶通過 firebase 登錄,但我的方式不起作用
當我嘗試登錄時出現此錯誤
TypeError: Cannot read properties of undefined (reading 'getProvider')
_getProvider
node_modules/@firebase/app/dist/esm/index.esm2017.js:259
> 259 | var heartbeatController =
getAuth
node_modules/@firebase/auth/dist/esm2017/index-909bd8f4.js:17120
> 17120 | var provider = _getProvider(app, 'auth');
toggleSignIn
src/components/login/Login.js:23
> 23 | let myAuth = getAuth(auth)
這是我的代碼:
import { signInWithEmailAndPassword, getAuth } from "firebase/auth";
import { auth } from "../auth/base";
const Login = () => {
const [checked, setChecked] = useState(true);
const [email, setEmail] = useState("");
const [password] = useState("");
const [values, setValues] = React.useState({
password: "",
showPassword: false,
});
function toggleSignIn(e) {
e.preventDefault();
// Sign in with email and pass.
let myAuth = getAuth(auth);
signInWithEmailAndPassword(myAuth, email, password)
.then((response) => {
console.log(response);
// do something when sign in is successful
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === "auth/wrong-password") {
alert("Wrong password.");
} else {
alert(errorMessage);
}
console.log(error);
});
}
return (
<div>
<label
>
E-MAIL
</label>
<input
onChange={(e) => setEmail(e.target.value)}
/>
<label
htmlFor="password"
className="font-manrope float-left block text-sm text-gray-700"
>
Password
</label>
<InputonChange={(e) => {
setPassword(e.target.value)
}}
/>
</div>
</div>
這是從這里匯入 Auth 的地方:
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
*******};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
所以我想要的是只讓注冊用戶登錄,沒有注冊用戶會收到警報
我一直在尋找一個星期的解決方案,但沒有
uj5u.com熱心網友回復:
它getAuth()
采用 Firebase 應用程式實體,而不是身份驗證實體本身。您可以像這樣直接傳遞 auth 實體:
// remove this
// let myAuth = getAuth(auth);
// pass auth directly
signInWithEmailAndPassword(auth, email, password)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517383.html
標籤:Google Cloud Collective 反应火力基地firebase 身份验证
上一篇:將Id從視圖決議到控制器