亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Firebase 不保留身份驗證狀態

Firebase 不保留身份驗證狀態

動漫人物 2023-08-24 10:24:17
我想知道如何使用 firebase 保持身份驗證狀態。我正在做的是嘗試設置持久狀態,我這樣做:login.addEventListener('click', function (e) {    e.preventDefault();    var email = emailInput.value;    var password = passwordInput.value;    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)    .then(function() {        auth.signInWithEmailAndPassword(email, password)        .then(function (user) {         })        .catch(function (error) {                alert(error);        });    })});問題是,當我刷新網絡應用程序時,會話似乎不會持續。它將用戶重定向回登錄屏幕,這根本不是所需的輸出。所需的輸出是保存用戶的會話,并且用戶能夠瀏覽應用程序而不會出現被迫再次登錄的問題。我正在關注 firebase 文檔:https://firebase.google.com/docs/auth/web/auth-state-persistence
查看完整描述

1 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

如果您想知道用戶之前在新頁面加載時是否登錄過您的網站,您應該使用身份驗證狀態觀察器來接收回調,該回調會在首次知道用戶登錄時通知您。它不會觸發頁面加載時立即 - SDK 需要一些時間來加載用戶帳戶并確定其是否有效。

firebase.auth().onAuthStateChanged((user) => {

? if (user) {

? ? // User is signed in, see docs for a list of available properties

? ? // https://firebase.google.com/docs/reference/js/firebase.User

? ? var uid = user.uid;

? ? // ...

? } else {

? ? // User is signed out

? ? // ...

? }

});

不要用于firebase.auth().currentUser確定用戶之前是否已登錄。頁面加載時它最初始終為空。

查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號