Первое что хочу сделать, это переделать из модульного подключения в импорты
Делаю все по доке
Выдает ошибку applicationDefault is not defined
import { initializeApp } from 'firebase/app';
import { getDatabase, set, ref, update } from "firebase/database";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signOut } from "firebase/auth";
import { firebaseConfig} from './firebase/firebaseConfig.js';
initializeApp({
credential: applicationDefault(),
databaseURL: 'https://auth-examplec-default-rtdb.firebaseio.com'
});
export const app = initializeApp(firebaseConfig);
export const database = getDatabase(app);
export const auth = getAuth();
export const signUp = document.getElementById('signUp');
export const login = document.getElementById('login');
export const logout = document.getElementById('logout');
export const goSignUp = document.querySelector('.goSignUp');
export const goSignIn = document.querySelector('.goSignIn');
export let loggedTitle = document.querySelector('.loggedTitle');
goSignUp.style.display = 'none';
logout.style.display = 'none';
login.style.display = 'none';
loggedTitle.style.display = 'none';
//* юзер зарегистрировался
function accountIsRegistred() {
signUp.style.display = 'none';
goSignIn.style.display = 'none';
login.style.display = 'block';
goSignUp.style.display = 'block';
}
//* юзер авторизировался
function logged(){
let email = document.querySelector('.sign-form__email');
let password = document.querySelector('.sign-form__password');
login.style.display = 'none';
goSignUp.style.display = 'none';
logout.style.display = 'block';
email.style.display = 'none';
password.style.display = 'none';
loggedTitle.style.display = 'block';
}
//* переход к регистрации
function registration(){
goSignUp.style.display = 'none';
logout.style.display = 'none';
login.style.display = 'none';
signUp.style.display = 'block';
goSignIn.style.display = 'block';
}
//* переход к авторизации
function goLogin() {
accountIsRegistred();
logout.style.display = 'none';
}
goSignIn.addEventListener('click', (e) => {
accountIsRegistred(signUp)
goSignIn.style.display = "none";
})
goSignUp.addEventListener('click', (e) => {
registration();
})
signUp.addEventListener('click', (e) => {
let email = document.getElementById('email').value;
let password = document.getElementById('password').value;
// let username = document.getElementById('username').value;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
set(ref(database, 'users/' + user.uid),{
// username: username,
email: email,
password: password,
})
alert("user created " + email);
accountIsRegistred();
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});
})
login.addEventListener('click', (e) => {
let email = document.getElementById('email').value;
let password = document.getElementById('password').value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
const dt = new Date();
update(ref(database, 'users/' + user.uid),{
last_login: dt
})
alert('User loged in! ' + email);
logged();
loggedTitle.innerHTML =" You are logged as " + email;
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});
});
const user = auth.currentUser;
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;
// ...
} else {
// No user is signed in.
}
});
logout.addEventListener('click', (e) => {
signOut(auth).then(() => {
// Sign-out successful.
alert('user loged out');
goLogin();
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});
});