Не удается получить идентификатор при входе в систему и перенаправлении на домашнюю страницу - PullRequest
0 голосов
/ 06 октября 2019

Я использую веб-аутентификацию 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.
}

как я могурешить это? Спасибо.

1 Ответ

1 голос
/ 06 октября 2019

Загружен неверный скрипт. В конце концов, как сказал Тайо в комментарии, сначала onauth установили функцию log uid.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...