# #javascript #firebase
Вопрос:
Проблема:- Я пытался аутентифицировать пользователей при входе в систему с помощью аутентификации firebase. но теперь при отправке учетных данных он не проходит проверку подлинности и показывает ошибку журнала.
Ошибка неперехваченной ссылки: в элементе HTMLFormElement не задан пароль signInWithEmailAndPassword. (index.js:42) (анонимный) @ index.js:42
ниже приведен html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="scss/main.scss">
</head>
<body>
<div id="not-log-in">
<form id="form">
<input type="email" id="email" placeholder="email">
<input type="password" id="password" placeholder="password">
<input type="submit" value="Login">
</form>
</div>
<div id="logged-in">
<h3>sucess</h3>
<button onclick="logout()">logout</button>
</div>
<script src="funky/index.js"></script>
</body>
</html>
ниже приведен javascript
import { initializeApp } from 'firebase/app';
import { getAuth, onAuthStateChanged, signOut } from "firebase/auth";
const firebase = initializeApp({
apiKey: "xxxxxxxxxxx",
authDomain: "xxxxxxxxx",
projectId: "xxxxx",
storageBucket: "xxxxxxx",
messagingSenderId: "xxxxxxx",
appId: "xxxxxxxxxxxxx"
});
const notlog = document.getElementById('not-log-in');
const login = document.getElementById('logged-in');
const form = document.getElementById('form')
const auth = getAuth();
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
// const uid = user.uid;
login.style.display = 'block'
notlog.style.display = 'none'
// ...
} else {
// User is signed out
// ...
login.style.display = 'none'
notlog.style.display = 'block'
}
});
form.addEventListener('submit', (event) => {
event.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
console.log(email," ",password)
signInWithEmailAndPassword(auth, email, password)
.then((user) => {
// Signed in
// const user = usercred.user;
if (user) {
alert('sucess')
}
// ...
})
.catch((error) => {
console.log('error',error)
});
})
function logout () {
signOut(auth).then(() => {
// Sign-out successful.
}).catch((error) => {
// An error happened.
});
}
Ответ №1:
Вы не импортировали signInWithEmailAndPassword
функцию, но пытаетесь ее использовать, что приводит к вашей ошибке.
Пожалуйста, импортируйте signInWithEmailAndPassword
из "firebase/auth"
. Вы можете просто включить его в свой оператор импорта, который уже есть
import { getAuth, onAuthStateChanged, signInWithEmailAndPassword, signOut } from "firebase/auth";