8

As the question, for Login with google in firebase need to set google-service but if you create new react-native project with create-react-native-app there will have no "android" or "ios" folder (accept used "eject") so, anyone have a suggestion for me? However I've no idea for how to setting google-service in my project too (even I "eject" the project).

Noer Nova
  • 233
  • 4
  • 12

2 Answers2

20

@brentvatne 's answer is a bit out of date. Here's how I got it working on Expo v27

Important bit: you can get your client ids with these instructions.

Just select your firebase app from the project dropdown on the google page.

const _loginWithGoogle = async function() {
  try {
    const result = await Expo.Google.logInAsync({
      androidClientId:"YOUR_ANDROID_CLIENT_ID",
      iosClientId:"YOUR_iOS_CLIENT_ID",
      scopes: ["profile", "email"]
    });

    if (result.type === "success") {
      const { idToken, accessToken } = result;
      const credential = firebase.auth.GoogleAuthProvider.credential(idToken, accessToken);
      firebase
        .auth()
        .signInAndRetrieveDataWithCredential(credential)
        .then(res => {
          // user res, create your user, do whatever you want
        })
        .catch(error => {
          console.log("firebase cred err:", error);
        });
    } else {
      return { cancelled: true };
    }
  } catch (err) {
    console.log("err:", err);
  }
};
Joe Roddy
  • 729
  • 6
  • 12
4

It isn't necessary to make any changes to the android or ios folders in order to support Google sign in with firebase on an app built with Expo.

  1. Follow the guide for configuring Google auth on the Expo docs
  2. Use the approach described in Expo's Using Firebase guide, where it describes how to authenticate with Facebook, and swap out Google where needed.
Aaron Lu
  • 25
  • 5
brentvatne
  • 7,603
  • 4
  • 38
  • 55
  • It's work pretty well to login to google and get username or email but cannot accessToken to firebase with error "Unable to parse Google id_token? Please what is wrong ` code` `googleAuthenticate = (token) => { const provider = firebase.auth.GoogleAuthProvider provider.addScopes('profile'); provider.addScopes('email'); const credential = provider.credential(token) return firebase.auth().signInWithCredential(credential) }; ` – Noer Nova Jun 18 '17 at 20:04
  • 3
    @NoerNova - with the response that you get back from Expo's api you have an object with `idToken`, `accessToken` and some other properties (https://github.com/expo/expo-sdk/blob/d152d47f240aadc1618e13a2ab3832411c62e9b8/src/Google.js#L18-L36) -- you need to take the `idToken` and `accessToken` and pass them into your `googleAuthenticate` function like so: https://gist.github.com/brentvatne/46449731ae852a14ce321e14f0f19187 – brentvatne Jun 19 '17 at 00:11
  • I recorded a coding session where Firebase auth is integrated to an expo project via the Firebase web sdk: https://www.youtube.com/watch?v=0TlOhmdl3-M. – wcandillon Dec 20 '17 at 17:07