Firebase SDK在React Native中本地登录持久性的问题。

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

Problems giving Firebase SDK local login persistence with react-native

问题

我已经尝试了文档中提供的所有工具,以及在Stack Overflow中找到的方法,但对于如何创建登录持久性感到困惑。

`setPersistence(auth, localPersistence);`

在我实施时不起作用,以及一些其他变体也不行。有些来源说我需要实施Redux,而另一些说Firebase应该默认具备这一功能,我只需要实施类似于以下的内容:

```javascript
firebase.auth().onAuthStateChanged((user) => {
    if (user) {
        console.log('用户已登录');
    }
});

以下是我当前用于身份验证的代码,请告诉我如何设置本地持久性。

Firebase电话验证函数:


希望这能帮助您解决问题。

<details>
<summary>英文:</summary>

I&#39;ve tried all the tools given in the documentation and in stack overflow and am confused about how to create login persistence. 

`setPersistence(auth, localPersistence); `

does not work when I implement it, as well as some other variations of this. Some sources say that I need to implement redux while some say that firebase should have this as a default and all I need is to implement something like 

    firebase.auth().onAuthStateChanged((user) =&gt; {
          if (user) {
            console.log(&#39;user is logged&#39;);
          }
    });

Here is my code right now for authentication, please let me know how to set local persistence.

Firebase phone auth functions:
&#39;&#39;&#39;
    const sendVerification = () =&gt; {
        const phoneProvider = new firebase.auth.PhoneAuthProvider();
        phoneProvider
        .verifyPhoneNumber(phoneNumberWithCode, recaptchaVerifier.current)
        .then(setVerificationId);
        setConfirming(!confirming);
        setCode(&#39;&#39;);
    };

    const confirmCode = () =&gt; {
        const credential = firebase.auth.PhoneAuthProvider.credential(
            verificationId,
            code
        );
        firebase.auth().signInWithCredential(credential)
        .then(() =&gt; {
            setCode(&#39;&#39;);
            checkIfPhoneExists(phoneNumberWithCode).then((userExists) =&gt; {
                if (userExists) {
                    navigation.navigate(&#39;Threads&#39;);
                } else {
                    navigation.navigate(&#39;EnterInfo&#39;);
                }
            })
            createAccountIfNotUser(phoneNumberWithCode);
        }).catch((error) =&gt; {
            console.log(error);
        });
    };

Firebase app and auth initialization:

    export const app = firebase.initializeApp(firebaseConfig);

    export const auth = getAuth(app);
    auth.languageCode = &#39;it&#39;;
    setPersistence(auth, localPersistence);



</details>


# 答案1
**得分**: 1

你可以使用[@react-native-async-storage/async-storage][1]来实现与Firebase的持久性,然后在firebase应用中使用它。

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

const app = initializeApp({ ... });

initializeAuth(app, {
   persistence: getReactNativePersistence(AsyncStorage),
});
英文:

In order to achieve persistance with Firebase, you can install @react-native-async-storage/async-storage and use it with the firebase app.

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

const app = initializeApp({ ... });

initializeAuth(app, {
   persistence: getReactNativePersistence(AsyncStorage),
});

huangapple
  • 本文由 发表于 2023年4月6日 22:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950637.html
匿名

发表评论

匿名网友

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

确定