Khan Khan
Khan Khan

Reputation: 119

provider.credential' is undefined in Expo Firebase Google authentication

I'm very new and trying to set Firebase Google authentication in Expo RN app,

Expo docs code snippet below:

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();
        }}
    />
  );
}

I'm getting error something like that:

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

Any solution?

Thanks in advance

Upvotes: 3

Views: 619

Answers (1)

Phobos
Phobos

Reputation: 1646

Replace the following code:

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

with:

const credential = GoogleAuthProvider.credential(idToken);

I believe the expo docs need to be updated to reflect this.

Upvotes: 8

Related Questions