如何访问Firebase的身份验证持久性管理器密钥?

huangapple go评论68阅读模式
英文:

How to Access Firebase's Authentication Persistence Manager Keys?

问题

I am running a web app with Firebase redirect authentication.

When the user is returned to the web app, the web app initializes once more while the authentication redirect is pending.

This state can be seen in the browser session storage, like this:

{firebase:pendingRedirect:XXXXXXXXXXXXXXXXXXXXXXXXXXX:[DEFAULT]: '"true"'}

I want to retrieve this item in an idiomatic way. Right now I am using this util:

function getAuthRedirectIsPending() {
  for (let i = 0; i < sessionStorage.length; i++) {
    const key = sessionStorage.key(i)
    if (
      key?.startsWith('firebase:pendingRedirect') &&
      sessionStorage.getItem(key) === '"true"'
    ) {
      return true
    }
  }
  return false
}

But I also noticed that this key exists on the firebase auth object, albeit inaccessible:

{
    "persistenceManager": {
        ...
        "fullUserKey": "firebase:authUser:XXXXXXXXXXXXXXXXXXXXXXXXXXX:[DEFAULT]",
        "fullPersistenceKey": "firebase:persistence:XXXXXXXXXXXXXXXXXXXXXXXXXXX:[DEFAULT]"
}

I was wondering if there was a way to retrieve the persistence redirect key name from Firebase, then using it to access the window session storage?

英文:

I am running a web app with Firebase redirect authentication.

When the user is returned to the web app, the web app initializes once more while the authentication redirect is pending.

This state can be seen in the browser session storage, like this:

{firebase:pendingRedirect:XXXXXXXXXXXXXXXXXXXXXXXXXXX:[DEFAULT]: '"true"'}

I want to retrieve this item in an idiomatic way. Right now I am using this util:

function getAuthRedirectIsPending() {
  for (let i = 0; i < sessionStorage.length; i++) {
    const key = sessionStorage.key(i)
    if (
      key?.startsWith('firebase:pendingRedirect') &&
      sessionStorage.getItem(key) === '"true"'
    ) {
      return true
    }
  }
  return false
}

But I also noticed that this key exists on the firebase auth object, albeit inaccessible:

{
    "persistenceManager": {
        ...
        "fullUserKey": "firebase:authUser:XXXXXXXXXXXXXXXXXXXXXXXXXXX:[DEFAULT]",
        "fullPersistenceKey": "firebase:persistence:XXXXXXXXXXXXXXXXXXXXXXXXXXX:[DEFAULT]"
}

I was wondering if there was a way to retrieve the persistence redirect key name from Firebase, then using it to access the window session storage?

答案1

得分: 0

Firebase Authentication Persistence Manager 使用 Web 存储 API 来存储用户身份验证令牌。Web 存储 API 是一个简单的键值存储系统,提供两种存储机制:会话存储和本地存储。

会话存储是一个临时存储机制,当用户关闭浏览器窗口或标签页时会被清除,而本地存储会在用户关闭浏览器窗口或标签页后仍然保留。

Firebase Authentication Persistence Manager 将用户身份验证令牌存储在本地存储中,本地存储的键可以通过浏览器的开发者控制台访问。

要访问 Firebase Authentication Persistence Manager 的键,请按照以下步骤进行:

  1. 在浏览器中打开您的网络应用程序并导航到身份验证页面。

  2. 通过按下 F12 键或右键单击页面并选择 "检查",打开浏览器的开发者控制台。

  3. 在开发者控制台中,选择 "应用程序" 选项卡。

  4. 在 "应用程序" 选项卡中,展开 "本地存储" 选项。

  5. 查找 Firebase Authentication Persistence Manager 的键。这些键以 "firebase:authUser" 开头,后面跟着您的 Firebase 项目 ID。

  6. 单击键以查看存储的值,其中包括用户的身份验证令牌。

注意:在大多数情况下,可能不需要访问用户的身份验证令牌,因为 Firebase Authentication 提供了方法来管理用户身份验证状态,而不需要直接访问身份验证令牌。此外,操纵身份验证令牌可能是危险的,并可能危及您的应用程序的安全性。

英文:

Firebase Authentication Persistence Manager uses the Web Storage API to store user authentication tokens. The Web Storage API is a simple key-value storage system that provides two types of storage mechanisms: session storage and local storage.

The session storage is a temporary storage mechanism that is cleared when the user closes the browser window or tab, while the local storage persists even after the user closes the browser window or tab.

Firebase Authentication Persistence Manager stores the user authentication tokens in the local storage, and the keys for the local storage are accessible through the browser's developer console.

To access the Firebase Authentication Persistence Manager keys, follow these steps:

Open your web application in your browser and navigate to the authentication page.

Open the browser's developer console by pressing F12 or right-clicking on the page and selecting "Inspect."

In the developer console, select the "Application" tab.

In the "Application" tab, expand the "Local Storage" option.

Look for the Firebase Authentication Persistence Manager keys. The keys are named "firebase:authUser" followed by your Firebase project ID.

Click on the key to view the stored value, which will include the user's authentication token.

Note: Accessing the user authentication token may not be necessary in most cases, as Firebase Authentication provides methods to manage user authentication state without directly accessing the authentication token. Additionally, manipulating the authentication token can be dangerous and could compromise the security of your application.

huangapple
  • 本文由 发表于 2023年2月19日 16:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定