Я использую веб-аутентификацию firebase и хочу получить свой uid пользователя в консоли моей домашней страницы после того, как я войду со своей страницы входа. Согласно тому, что я сделал, UID не отображается. Пожалуйста, смотрите коды ниже:
Код входа:
console.log("Initialisation Successful!");
const auth = firebase.auth();
var db = firebase.firestore();
function loginUser(){
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
auth.signInWithEmailAndPassword(email, password)
.then(cred => {
alert("success");
})
.then(function(){
window.location.href = "homepage.html";
})
.catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode + " " + errorMessage);
});
}
Код домашней страницы:
console.log("Initialisation Successful!");
const auth = firebase.auth();
var db = firebase.firestore();
var user = firebase.auth().currentUser;
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var email = user.email;
var uid = user.uid;
console.log(email + " " + uid);
} else {
// User is signed out.
// ...
}
});
if (user) {
// User is signed in.
if (user != null) {
email = user.email;
uid = user.uid;
console.log(email + " " + uid);
}
} else {
// No user is signed in.
}
как я могурешить это? Спасибо.