繁体   English   中英

Firebase 导航到其他页面时保持用户登录

[英]Firebase keep user logged in when navigating to other pages

我目前能够登录我的用户帐户并在仍然登录的情况下成功导航到我的仪表板,但是当我 go 到任何其他页面时,我的登录状态消失了。 另一个问题是在更新我的用户信息时,我如何编写 function 以便它根据谁登录来更新信息? 我在下面提供了我的代码。 谢谢!

编辑个人资料 JS:

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Initialize variables
const auth = firebase.auth()

auth.onAuthStateChanged( auth,user => {
    var user = firebase.auth().currentUser;
    if (user) {
      alert("User active: ");

      // User is signed in.
      var email = user.email;
      var uid = user.uid;

      //Take user to a different or home page
      window.location.href ="editprofile.html?id=" + uid;
    
    } else {
      alert("No active user please signup or sign in.");

      window.location.href ="login.html?error";

    }
});

var studioName, email, password, firstName, lastName, address, country, state, city, zip, phoneNumber;

function updateStu() {

    //Get data
    studioName = document.getElementById("studioName").value;
    email = document.getElementById("email").value;
    password =  document.getElementById("password").value;
    firstName = document.getElementById("firstName").value;
    lastName = document.getElementById("lastName").value;
    address = document.getElementById("address").value;
    country = document.getElementById("country").value;
    state = document.getElementById("state").value;
    city = document.getElementById("city").value;
    zip = document.getElementById("zip").value;
    phoneNumber = document.getElementById("phoneNumber").value;

    console.log(studioName, firstName, email);

    firebase
    .database()
    .ref("/studiopick/studio/users" + studioName)
    .update({
        
    //studioName : studioName,
    firstName : firstName,
    lastName : lastName,
    email : email,
    password : password,
    address : address,
    country : country,
    state : state,
    city : city,
    zip : zip,
    phoneNumber : phoneNumber
    });

    document.getElementById("studioName").value ="";
    document.getElementById("email").value ="";
    document.getElementById("password").value ="";
    document.getElementById("firstName").value ="";
    document.getElementById("lastName").value ="";
    document.getElementById("address").value ="";
    document.getElementById("country").value ="";
    document.getElementById("state").value ="";
    document.getElementById("city").value ="";
    document.getElementById("zip").value ="";
    document.getElementById("phoneNumber").value ="";

    alert("Data Updated");
};



对于你的第一个问题:

您正在使用auth.currentUser属性而不是从onAuthStateChanged返回的user

根据文档


import { getAuth, onAuthStateChanged } from "firebase/auth";

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;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

如果useronAuthStateChanged成功返回,您可以全局设置返回用户的值以在整个应用程序中访问它。 否则,您可以导航到 /login(或等效项)。

对于您的第二个问题:这取决于您如何构建数据库。 通常的做法是使用 Firebase Auth Id 作为 users/,然后可以从 user.uid 引用。

如果您想记录谁创建或最后编辑了一个文档,您可以保留包含用户 ID 的字段。

Firestore 还具有利用自定义声明来保护数据库 CRUD 操作的安全规则

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM