I've found a bunch of different documentation to implement GoogleSignIn.
React native with Google SignIn doc (to be used without firebase I think)
Firebase Auth doc (showing several api docs for every platform)
React Native doc (Using firebase, this one seems more relevant to me)
All of this is very confusing, I've done and undone things several times.
I'm finally running with the last one:
using "@react-native-firebase/auth" package seems the good way to implement it.
Here is the process:
First : Show the google sign in button and get a first idToken from GoogleSignIn lib
import {
GoogleSignin,
GoogleSigninButton,
} from '@react-native-google-signin/google-signin';
import { auth } from '@react-native-firebase/auth';
GoogleSignin.configure({
webClientId: 'web-client-id-from-firebase-after-having-enabled-authentication',
});
const onGoogleButtonClick = async () => {
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
// Get the users ID token
const { idToken } = await GoogleSignin.signIn();
...
Then the google idToken is used to get credential and sign into firebase :
// 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 signIn call should trigger onAuthStateChanged, with signIn user information and possibility to retreive a firebase idToken.
useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
return subscriber; // unsubscribe on unmount
}, []);
const onAuthStateChanged = async (user) => {
let token = await user.getIdToken();
//then send token to be validated on server side and start user session
... }
I didn't try this last point yet because I cannot import the "auth" object properly. The doc says:
import { auth } from '@react-native-firebase/auth';
auth().signInWithCredential ...
trigger this error:
TypeError: 0, _auth.auth is not a function (it is undefined)
I've tried different ways to import it, reading this post without success.
Thank you for any help.