【问题标题】:provider.credential' is undefined in Expo Firebase Google authentication在 Expo Firebase Google 身份验证中未定义 provider.credential'
【发布时间】:2022-01-06 12:44:35
【问题描述】:

我是新手,正在尝试在 Expo RN 应用中设置 Firebase Google 身份验证,

以下世博会文档代码sn-p:

https://docs.expo.dev/guides/authentication/#google

import * as React from 'react';
import * as WebBrowser from 'expo-web-browser';
import { ResponseType } from 'expo-auth-session';
import * as Google from 'expo-auth-session/providers/google';
import { initializeApp } from 'firebase/app';
import { getAuth, GoogleAuthProvider, signInWithCredential } from 'firebase/auth';
import { Button } from 'react-native';

// Initialize Firebase
initializeApp({
  /* Config */
});

WebBrowser.maybeCompleteAuthSession();

export default function App() {

  const [request, response, promptAsync] = Google.useIdTokenAuthRequest(
    {
      clientId: 'Your-Web-Client-ID.apps.googleusercontent.com',
      },
  );

  React.useEffect(() => {
    if (response?.type === 'success') {
      const { id_token } = response.params;
      
      const auth = getAuth();
      const provider = new GoogleAuthProvider();
      const credential = provider.credential(id_token);
      signInWithCredential(auth, credential);
    }
  }, [response]);

  return (
    <Button
      disabled={!request}
      title="Login"
      onPress={() => {
        promptAsync();
        }}
    />
  );
}

我遇到了类似的错误:

TypeError: provider.credential is not a function. (In 'provider.credential(_id_token)', 'provider.credential' is undefined)

有什么办法吗?

提前致谢

【问题讨论】:

    标签: firebase react-native firebase-authentication expo google-authentication


    【解决方案1】:

    替换以下代码:

    const provider = new GoogleAuthProvider();
    const credential = provider.credential(id_token);
    

    与:

    const credential = GoogleAuthProvider.credential(idToken);
    

    我认为需要更新世博会文档以反映这一点。

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 1970-01-01
      • 2020-04-01
      • 2022-12-10
      • 2019-07-10
      • 1970-01-01
      • 2023-02-14
      • 2022-08-20
      • 1970-01-01
      相关资源
      最近更新 更多