React Native Firebase Auth Persistence: Cannot import "getReactNativePersistence" from "firebase/auth/react-native"

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

React Native Firebase Auth Persistence: Cannot import "getReactNativePersistence" from "firebase/auth/react-native"

问题

问题是什么?

我正在构建一个React Native应用程序,使用Expo作为构建工具和Firebase作为身份验证和数据库的后端。

到目前为止,一切都进行得很顺利。身份验证登录和注销正常工作,使用强制性安全规则从Firestore获取数据也正常工作。我唯一的问题是身份验证的持续性不起作用。但是:这个问题并不罕见,有一个简单的解决方法。为了使其工作,必须从默认方式中自定义身份验证对象。

默认情况下,我会这样做:

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

// 初始化Firebase
const firebaseConfig = {...};

export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);

这对React Native来说是有效的,但它缺乏持久性身份验证 - 这就是我的初始问题所在。为了解决这个问题,我按照Expo文档上关于如何设置Firebase的指示进行操作。他们明确引导我去这个解决方案。简而言之,我需要将默认的内存持久性管理器切换为React Native持久性管理器,就像这篇文章中建议的那样:

import { initializeApp } from "firebase/app";
import {
  initializeAuth,
  getReactNativePersistence,
} from "firebase/auth/react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";

// 初始化Firebase
const firebaseConfig = {};

export const app = initializeApp(firebaseConfig);
export const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage),
});

似乎唯一需要额外安装的是这个包,其他导入应该已经可以工作。

npx expo install @react-native-async-storage/async-storage

但是当我尝试导入React Native持久性管理器时,我收到了以下错误:

// 我尝试这个 -->

import { initializeAuth, getReactNativePersistence } from "firebase/auth/react-native";

--> 我得到这个错误:“找不到模块'firebase/auth/react-native'或其相应的类型声明.ts(2307)

这就是我的问题,我只是不知道为什么我不能导入“getReactNativePersistence”,因此无法按预期配置我的Firebase应用程序。

我尝试过什么?

  1. 首先,我尝试弄清楚在我的node_modules/firebase/auth目录中是否确实存在“getReactNativePersistence”对象。为了找出它,我克隆了上面提到的存储库,并查看了本地是否可以在我的计算机上工作以及包含的package.json文件是否有差异。我发现:

    --> 导入在我的机器上工作

    --> 我没有发现依赖项中的明显差异

    --> 我找到了“getReactNativePersisentence”应该位于的位置。它在以下位置:

\node_modules\@firebase\auth\dist\rn\src\platform_react_native\persistence\react_native.d.ts
  1. 然后,我尝试在我的项目中找到这个对象。我找到了它……它与上面提到的存储库中的位置相同。

  2. 之后,我还尝试重新安装Firebase,但也没有改变任何内容。

  3. 最后,我克隆了我的存储库并尝试进行干净的重新初始化,但这也没有起作用。

我不知道问题可能出在哪里……导入应该是有效的,对象位于它应该在的地方。

我的存储库在这里,也许有人对这里发生了什么有任何建议?

英文:

What is the issue?

I am building a react native app, using expo as my build tool and Firebase as my Backend for Auth and Database.

So far, everything has gone fine. Auth Loggin and Logout works, Fetching from Firestore with enforced security rules works. The only issue I have is that the persistence of the authentication is not working. BUT: this issue is not unknown and there is a simple solution for it. In order for this to work, one has to customize the auth object deviant from the default way.

By default, I would do this:

import { initializeApp } from "firebase/app";
import {getAuth } from "firebase/auth";

// Initialize Firebase
const firebaseConfig = {...};

export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);

This will work for react native, but it will lack persistent auth - thus where my initial problem comes from. To fix it I followed Expo docs instructions on how to set up Firebase. They explicitly guided me to this solution. In short, I need to switch the default in-memory persistence manager to the react native persistence manager like this article also suggested:

import { initializeApp } from "firebase/app";
import {
  initializeAuth,
  getReactNativePersistence,
} from "firebase/auth/react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";

// Initialize Firebase
const firebaseConfig = {};

export const app = initializeApp(firebaseConfig);
export const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage),
});

The only thing it seems I have to addiotionally install is this package, the rest of the imports should already work.

npx expo install @react-native-async-storage/async-storage

But when I try to import the react native persistence manager I get this error:

// I try this -->

import { initializeAuth, getReactNativePersistence } from "firebase/auth/react-native";

--> I get this Error: "Cannot find module 'firebase/auth/react-native' or its corresponding type declarations.ts(2307)"

THIS is my issue, I just dont know why I cant import "getReactNativePersistence", and thus I cannot configure my firebase app as desired.

What have I tried?

  1. First, I tried to figure out, if there even is a "getReactNativePersistence" object in my node_modules/firebase/auth directory. In order to find it out I cloned this repository from the article mentioned above and looked into it if the import works locally on my machine and if there are differences in the package.json file. What I found:

    --> The import works on my machine

    --> I found no notable differences in the dependencies

    --> I found where "getReactNativePersisentence" is supposed to be located. It is at:

\node_modules\@firebase\auth\dist\rn\src\platform_react_native\persistence\react_native.d.ts
  1. Then I tried to find this object in my Project. I found it... It is in the very same place as in the repo above.

  2. After that, I also tried reinstalling Firebase, which also changed nothing.

  3. Last but not least I cloned my repo and tried a clean reinitialization, but that didn't work as well.

I have no clue what the problem could be... The import should work, the object is where it is supposed to be.

My Repo is here maybe someone has any suggestions on what is going on here?

答案1

得分: 0

总结问题并提供解决方案:

从 Firebase SDK 版本 10 开始,出现了这个导入问题。V10 对 React Native Auth 和相应的类型进行了更改。

说明:

> Firebase JavaScript SDK v10.0.0 中有许多与React Native Authentication捆绑包以及多个产品的类型更改相关的破坏性更改。

正如评论中的某人指出的那样,有一个 补丁说明,似乎解决了这个问题:

说明:

> 10.3.0 将破坏已转换为使用 getAuth() 初始化身份验证的 React Native 用户。现在,React Native 用户必须明确导入 @react-native-async-storage/async-storage,并将其提供给 initializeAuth() 作为选项,如下所示:

import { initializeAuth, getReactNativePersistence } from "firebase/auth";

import ReactNativeAsyncStorage from "@react-native-async-storage/async-storage";

const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(ReactNativeAsyncStorage),
});
// 初始化后随时可以使用 getAuth()

尽管看起来与此问题有关,但这个补丁似乎没有解决导入类型问题(依我看)。我还测试了他们提供的 V10.3.0 的导入模式,但仍然存在相同的类型问题...

简而言之: 问题的实际补丁仍然需要等待。

在此期间您可以做什么?

  1. 我想 这个线程 可能有一些有用的答案。

  2. 正如评论中建议的那样,我将 Firebase 版本降级到 @9.23.0,然后导入就正常工作了!所以我认为这将是暂时的解决方案!

英文:

Summarizing the Issue and Providing a Solution:

As of Firebase SDK Version 10, this import issue appeared. V10 Introduces changes for both React Native Auth and corresponding typings.

Stating:

> The Firebase JavaScript SDK v10.0.0 has a number of breaking changes related to the React Native bundle for Authentication, as well as typings changes across several products.

As one in the comments pointed out, there is a Patch-Note that seems to address this issue:

Stating:

> 10.3.0 will break React Native users who have converted to using getAuth() to initialize Authentication. React Native users who want persistent auth state must now explicitly import @react-native-async-storage/async-storage and provide it to initializeAuth() as an option like so:

import { initializeAuth, getReactNativePersistence } from "firebase/auth";

import ReactNativeAsyncStorage from "@react-native-async-storage/async-storage";

const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(ReactNativeAsyncStorage),
});
// getAuth() can be used any time after initialization

Although it looks like it is connected to this issue, this patch does not seem (to me) to address the import typing problem. I also tested their provided import schema with V10.3.0 but had the same typing issues...

<b>Long story short:</b> An actual patch for the issue is still to be waited for.

What can you do in the meantime?

  1. I suppose this thread might have some usefull answers.

  2. As the comments suggested, I downgraded to Firebase @9.23.0 and the import worked fine again! So I guess this will be the solution for the time being!

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

发表评论

匿名网友

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

确定