简体   繁体   English

将身份验证数据保存到 firebase React native

[英]save Authentication data to firebase React native

i have a react natve app it has a signup whith google button when i click on signin i am getting data in console.log i want to save the data in firebase i sont know how to do it我有一个 React Natve 应用程序,当我单击登录时,它有一个带有谷歌按钮的注册我正在 console.log 中获取数据我想将数据保存在 firebase 中我不知道该怎么做

  const googleLogin = async () => {
    try {
      await GoogleSignin.hasPlayServices();
      const userInfo = await GoogleSignin.signIn();
      console.log(userInfo);// i am getting user data here

     
    } catch (error) {
      if (error.code === statusCodes.SIGN_IN_CANCELLED) {
        // user cancelled the login flow
      } else if (error.code === statusCodes.IN_PROGRESS) {
        // operation (e.g. sign in) is in progress already
      } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
        // play services not available or outdated
      } else {
        // some other error happened
      }
    }
  };

You can refer to this LINK for google social auth, this is what you're looking for to save the auth data to firebase:你可以参考这个LINK for google social auth,这是你正在寻找的将auth数据保存到firebase的东西:

import auth from '@react-native-firebase/auth';
import { GoogleSignin } from '@react-native-google-signin/google-signin';

async function onGoogleButtonPress() {
  // Check if your device supports Google Play
  await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
  // Get the users ID token
  const { idToken } = await GoogleSignin.signIn();

  // Create a Google credential with the token
  const googleCredential = auth.GoogleAuthProvider.credential(idToken);

  // Sign-in the user with the credential
  return auth().signInWithCredential(googleCredential);
}

This can helpful.这很有帮助。 Just make sure you have integrated all libraries for firebase authentication.只需确保您已经集成了 firebase 身份验证的所有库。

import {
  GoogleSignin,
  statusCodes,
} from '@react-native-google-signin/google-signin';
import auth, {FirebaseAuthTypes} from '@react-native-firebase/auth';

// This function will be call on tapping sign in with google button
const signInWithGoogle = async () => {
  try {
    // This will check whether there is Google play service or not
    await GoogleSignin.hasPlayServices();

//This will give you userInformation
    const userInfo = await GoogleSignin.signIn();

    // This will create new credential which can help to signIn in firebase

    const credential = auth.GoogleAuthProvider.credential(userInfo.idToken);
  
//Here we are trying to return promise so when we call function we can have promise object
    
return new Promise((resolve, reject) => {
      auth()
        .signInWithCredential(credential)
        .then(response => {
          console.log('response in', response);
          resolve(response);
        })
        .catch(error => {
          console.log('error in', error);
          reject(error);
        });
    });
  } catch (error) {
    if (error.code === statusCodes.SIGN_IN_CANCELLED) {
      // user cancelled the login flow
    } else if (error.code === statusCodes.IN_PROGRESS) {
      // operation (e.g. sign in) is in progress already
      alert(JSON.stringify(error));
    } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
      // play services not available or outdated
      alert(JSON.stringify(error));
    } else {
      alert(error);
    }
  }
};

Now when you call this function on google button on press it will give you promise object and you can do it like below.现在当你在谷歌按钮上调用这个 function 按下它会给你 promise object 你可以像下面那样做。

onPress={() => {

          SignInMethods.signInWithGoogle()
            .then(response => {
              console.log('user information from firebase authentication', response.user);

            })
            .catch(error => {

              console.log('error in google sign in :', error);

            });
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM