Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Cannot read properties of undefined (reading 'credential')

Hi,  i have this error :  Cannot read properties of undefined (reading 'credential') 
when i try to 

signInWithCredential

i have client side (react) that use GoogleLogin :

      <GoogleLogin
  onSuccess={onSuccess}
  onError={onFailure}
/>;

component (all the react return is wrap with 

    </GoogleOAuthProvider>
 with my id.
thisis my onsucsess :
  const onSuccess = async (response) => {
    try {
      console.log('Google Sign-In Response:', response);
 
      // Assuming the ID token is directly available in response.credential
      const idToken = response.credential;
     // console.log('id', idToken);

      // Now you can use the idToken to call your Cloud Function
      const signInWithGoogleFunction = httpsCallable(functions, 'signInWithGoogle');
      console.log("resultttsad")
      const result = await signInWithGoogleFunction(response);
      console.log("resulttt", result)

        //console.log("data",result.data,idToken)
 
      if (result.data.success) {
        // Google Sign-In successful, you can use the user data in result.data.user
        setRegistrationStatus({ type: 'success', message: 'Registration succeeded' });
      } else {
        // Google Sign-In failed
        setRegistrationStatus({ type: 'error', message: 'Registration failed' });
      }
    } catch (error) {
      console.error('Error during registration:', error);
      setRegistrationStatus({ type: 'error', message: 'Registration failed' });
    }
  };
 and this the cloud function :
const functions = require("firebase-functions");
const admin = require('firebase-admin');
   
admin.initializeApp();
// Callable function for user registration
    exports.signInWithGoogle = functions.https.onCall(async (data, context) => {
        try {
        console.log('Received data:', data);
        //return data  
        const auth = admin.auth();
        const credential = admin.auth.GoogleAuthProvider.credential(data.credential);
        // Sign in with the Google credential
        const result = await auth.signInWithCredential(credential);
       
        // Handle the successful sign-in result
        return { success: true, user: result.user };
        } catch (error) {
        console.error('Error during Google Sign-In:', error);
        throw new functions.https.HttpsError('internal', 'Google Sign-In failed');
        }
and i always get the error i mentioned above , this is the line where i get the problem :         const credential = admin.auth.GoogleAuthProvider.credential(data.credential);


Thanks ahead for help:)
  1.     });
0 0 1,060
0 REPLIES 0