尝试使用FacebookAuthProvider从Firebase连接,但得到错误.本机的反应 &g



我试图通过Facebook认证和Firebase SDK与createContext连接新用户,但我得到:new FacebookAuthProvider()不支持本地Firebase SDK。

我在这里使用firebase文档:https://firebase.google.com/docs/auth/web/facebook-login和这里:https://github.com/firebase/snippets-web/blob/1452a031ee1b7904a361b23391af8533237eab82/auth/facebook.js#L9-L9

我的代码:
import React, {createContext, useState} from 'react';
import auth from '@react-native-firebase/auth';
import firebase from '@react-native-firebase/app';
export const AuthContext = createContext({});
export const AuthProvider = ({children}) => {
  const [user, setUser] = useState(null);
  return (
    <AuthContext.Provider
      value={{
        user,
        setUser,
        login: async (email, password) => {
          try {
            await auth().signInWithEmailAndPassword(email, password);
          } catch (e) {
            return handleError(e);
          }
        },
        register: async (email, password) => {
          try {
            await auth().createUserWithEmailAndPassword(email, password);
          } catch (e) {
            return handleError(e);
          }
        },
        logout: async () => {
          try {
            await auth().signOut();
          } catch (e) {
            return handleError(e);
          }
        },
        loginWithFacebook: async (processRequest) => {
          try {
            let provider = new firebase.auth.FacebookAuthProvider();
            provider.addScope('user_birthday');
            provider.setCustomParameters({
              display: 'popup',
            });
            await firebase.auth().signInWithPopup(provider);
          } catch (e) {
            console.log(e);
          }
        },
      }}>
      {children}
    </AuthContext.Provider>
  );
};

如果有人有另一个想法使用facebook连接firebase SDK和useContext我也可以。

我使用这种形式,firebase和facebook连接在一起。:)

import auth from '@react-native-firebase/auth';
import { AccessToken, LoginManager } from 'react-native-fbsdk';
async function (dispatch, getState) {
        try {
            if (Platform.OS === 'android') {
                LoginManager.setLoginBehavior('web_only');
            }
            const result = await LoginManager.logInWithPermissions(['public_profile', 'email']);
            if (result.isCancelled) {
                throw 'User cancelled the login process';
            }
            const data = await AccessToken.getCurrentAccessToken();
            if (!data) {
                throw 'Something went wrong obtaining access token';
            }
            const facebookCredential = auth.FacebookAuthProvider.credential(data.accessToken);
            return auth()
                .signInWithCredential(facebookCredential)
                .then(async (result) => {})
                .catch((error) => {
                    console.log(error);
                });
        } catch (error) {
            console.log(error);
        }
    };

就像这里的文档所说的:

支持所有认证功能,除了电话认证和弹出/重定向OAuth操作。

答案在这里

我的AuthProvider现在与onFaceBookButtonPress函数从上面的doc:

import React, {createContext, useState} from 'react';
import auth from '@react-native-firebase/auth';
import {LoginManager, AccessToken} from 'react-native-fbsdk';
export const AuthContext = createContext({});
export const AuthProvider = ({children}) => {
  const [user, setUser] = useState(null);
  return (
    <AuthContext.Provider
      value={{
        user,
        setUser,
        login: async (email, password) => {
          try {
            await auth().signInWithEmailAndPassword(email, password);
          } catch (e) {
            return handleError(e);
          }
        },
        register: async (email, password) => {
          try {
            await auth().createUserWithEmailAndPassword(email, password);
          } catch (e) {
            return handleError(e);
          }
        },
        logout: async () => {
          try {
            await auth().signOut();
          } catch (e) {
            return handleError(e);
          }
        },
        loginWithFacebook: async (processRequest) => {
          try {
            await onFacebookButtonPress();
          } catch (e) {
            throw new Error(e);
          }
        },
      }}>
      {children}
    </AuthContext.Provider>
  );
  async function onFacebookButtonPress() {
    // Attempt login with permissions
    const result = await LoginManager.logInWithPermissions([
      'public_profile',
      'email',
    ]);
    if (result.isCancelled) {
      throw 'User cancelled the login process';
    }
    // Once signed in, get the users AccesToken
    const data = await AccessToken.getCurrentAccessToken();
    if (!data) {
      throw 'Something went wrong obtaining access token';
    }
    // Create a Firebase credential with the AccessToken
    const facebookCredential = auth.FacebookAuthProvider.credential(
      data.accessToken,
    );
    // Sign-in the user with the credential
    return auth().signInWithCredential(facebookCredential);
  }

我这样称呼它:

import {AuthContext} from '../../../navigation/auth/AuthProvider';
const {loginWithFacebook} = useContext(AuthContext);
<Button onPress={loginWithFacebook}

相关内容

最新更新