CodetrixStudio / CapacitorGoogleAuth

Capacitor plugin for Google Auth. Lightweight & no dependencies.
MIT License
284 stars 160 forks source link

Android App Crashing when SIgnIn button is clicked #389

Open MalikHamza007 opened 3 months ago

MalikHamza007 commented 3 months ago

Ok so I used this pluggin to create a simple google sign in it works best on web and on android the app crashes when signIn function is clicked the app crashes and yes i have checked every thing my credentials client id strings even main java is correct when I debug the app the pluggin also syncs in the catlog. " E Serious error executing plugin" and another error. "E FATAL EXCEPTION: CapacitorPlugins Process: io.ionic.starter, PID: 2383"

jhenriquez07 commented 3 months ago

Hi @MalikHamza007 , Same issue in my code. Did you find any solution?

erious error executing plugin java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816) at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:959) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loopOnce(Looper.java:232) at android.os.Looper.loop(Looper.java:317) at android.os.HandlerThread.run(HandlerThread.java:85) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()' on a null object reference at com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth.signIn(GoogleAuth.java:81) at java.lang.reflect.Method.invoke(Native Method)  at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)  at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816)  at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)  at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:959)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loopOnce(Looper.java:232)  at android.os.Looper.loop(Looper.java:317)  at android.os.HandlerThread.run(HandlerThread.java:85)  2024-08-13 12:01:30.713 14460-14788 AndroidRuntime io.ionic.eanaquel.qa E FATAL EXCEPTION: CapacitorPlugins Process: io.ionic.eanaquel.qa, PID: 14460 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:825) at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:959) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loopOnce(Looper.java:232) at android.os.Looper.loop(Looper.java:317) at android.os.HandlerThread.run(HandlerThread.java:85) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816) at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)  at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:959)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loopOnce(Looper.java:232)  at android.os.Looper.loop(Looper.java:317)  at android.os.HandlerThread.run(HandlerThread.java:85)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()' on a null object reference at com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth.signIn(GoogleAuth.java:81) at java.lang.reflect.Method.invoke(Native Method)  at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)  at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816)  at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)  at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:959)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loopOnce(Looper.java:232)  at android.os.Looper.loop(Looper.java:317)  at android.os.HandlerThread.run(HandlerThread.java:85) 

riderx commented 3 months ago

I think it’s due to the last change in v6. Look if your initialize method send the web client id. If yes remove it on mobile or sent it conditionally with the right one by platform. Web, iOS or android. It’s now used in priority

eqcb commented 2 months ago

I'm having the same issue. Was not passing ClientId in the initialize method. Error continues even if I pass it.

Fixed by removing one optional plugin I was trying out 'cordova-plugin-lottie-splashscreen' that added

    implementation "androidx.annotation:annotation:1.5.0"
    implementation "androidx.appcompat:appcompat:1.3.0"

to my capacitor.build.gradle.

not sure what really cause it. no time to look further as I'm sole dev of my app.

Capacitor 6 and 3.4.0-rc.4

Serious error executing plugin java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:820) at com.getcapacitor.Bridge.$r8$lambda$FNYM7cvgeBk0k8YXQH7M96Mrf-c(Unknown Source:0) at com.getcapacitor.Bridge$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at android.os.HandlerThread.run(HandlerThread.java:67) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()' on a null object reference at com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth.signIn(GoogleAuth.java:81) at java.lang.reflect.Method.invoke(Native Method)  at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)  at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:820)  at com.getcapacitor.Bridge.$r8$lambda$FNYM7cvgeBk0k8YXQH7M96Mrf-c(Unknown Source:0)  at com.getcapacitor.Bridge$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)  at android.os.Handler.handleCallback(Handler.java:958)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loopOnce(Looper.java:230)  at android.os.Looper.loop(Looper.java:319)  at android.os.HandlerThread.run(HandlerThread.java:67) 

                                                                     `FATAL EXCEPTION: CapacitorPlugins
                                                                                                Process: xxxx, PID: 5973
                                                                                                java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
                                                                                                    at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:829)
                                                                                                    at com.getcapacitor.Bridge.$r8$lambda$FNYM7cvgeBk0k8YXQH7M96Mrf-c(Unknown Source:0)
                                                                                                    at com.getcapacitor.Bridge$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
                                                                                                    at android.os.Handler.handleCallback(Handler.java:958)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    at android.os.Looper.loopOnce(Looper.java:230)
                                                                                                    at android.os.Looper.loop(Looper.java:319)
                                                                                                    at android.os.HandlerThread.run(HandlerThread.java:67)
                                                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)
                                                                                                    at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:820)
                                                                                                    at com.getcapacitor.Bridge.$r8$lambda$FNYM7cvgeBk0k8YXQH7M96Mrf-c(Unknown Source:0) 
                                                                                                    at com.getcapacitor.Bridge$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) 
                                                                                                    at android.os.Handler.handleCallback(Handler.java:958) 
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                                    at android.os.Looper.loopOnce(Looper.java:230) 
                                                                                                    at android.os.Looper.loop(Looper.java:319) 
                                                                                                    at android.os.HandlerThread.run(HandlerThread.java:67) 
                                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()' on a null object reference
                                                                                                    at com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth.signIn(GoogleAuth.java:81)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) 
                                                                                                    at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:820) 
                                                                                                    at com.getcapacitor.Bridge.$r8$lambda$FNYM7cvgeBk0k8YXQH7M96Mrf-c(Unknown Source:0) 
                                                                                                    at com.getcapacitor.Bridge$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) 
                                                                                                    at android.os.Handler.handleCallback(Handler.java:958) 
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                                    at android.os.Looper.loopOnce(Looper.java:230) 
                                                                                                    at android.os.Looper.loop(Looper.java:319) 
                                                                                                    at android.os.HandlerThread.run(HandlerThread.java:67) `                            
sarmate commented 2 months ago

Same issue. Hope a solution will be find

riderx commented 2 months ago

Please look the new doc it explains clearly the issue, and how to solve it. I’m creating a fork with Capgo who going to have all social as this is a recurring problem for all social login

DaveRicho commented 2 months ago

I think it’s due to the last change in v6. Look if your initialize method send the web client id. If yes remove it on mobile or sent it conditionally with the right one by platform. Web, iOS or android. It’s now used in priority

Hi @riderx, is this what you are explaining here (or have I misunderstood)?:

import { Plugins, Capacitor } from '@capacitor/core';
const { GoogleAuth } = Plugins;

async function initializeGoogleAuth() {
  const isAndroid = Capacitor.getPlatform() === 'android';
  const isIOS = Capacitor.getPlatform() === 'ios';

  let clientId = '';

  if (isAndroid) {
    // Use the Android client ID for Android devices
    clientId = 'YOUR_ANDROID_CLIENT_ID.apps.googleusercontent.com';
  } else if (isIOS) {
    // Use the iOS client ID for iOS devices
    clientId = 'YOUR_IOS_CLIENT_ID.apps.googleusercontent.com';
  } else {
    // Use the Web client ID for Web platforms
    clientId = 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com';
  }

  try {
    await GoogleAuth.initialize({
      clientId: clientId,
      // grantOfflineAccess: true,
      scopes: ['profile', 'email']
    });

    console.log('GoogleAuth initialized successfully');
  } catch (error) {
    console.error('Error initializing GoogleAuth:', error);
  }
}

Thanks,
Dave.
riderx commented 2 months ago

Yes correct. But to be fair, social login are messy for a long time in Capacitor. So I'm finishing up a plugin to have Google, Apple, Facebook in one plugin and with a simple configuration, and good Doc. The code is done here: https://github.com/Cap-go/capacitor-social-login We are finishing up the doc this week.

MalikHamza007 commented 2 months ago

Hi @MalikHamza007 , Same issue in my code. Did you find any solution?

erious error executing plugin java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816) at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:959) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loopOnce(Looper.java:232) at android.os.Looper.loop(Looper.java:317) at android.os.HandlerThread.run(HandlerThread.java:85) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()' on a null object reference at com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth.signIn(GoogleAuth.java:81) at java.lang.reflect.Method.invoke(Native Method)  at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)  at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816)  at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)  at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:959)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loopOnce(Looper.java:232)  at android.os.Looper.loop(Looper.java:317)  at android.os.HandlerThread.run(HandlerThread.java:85)  2024-08-13 12:01:30.713 14460-14788 AndroidRuntime io.ionic.eanaquel.qa E FATAL EXCEPTION: CapacitorPlugins Process: io.ionic.eanaquel.qa, PID: 14460 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:825) at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:959) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loopOnce(Looper.java:232) at android.os.Looper.loop(Looper.java:317) at android.os.HandlerThread.run(HandlerThread.java:85) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816) at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)  at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:959)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loopOnce(Looper.java:232)  at android.os.Looper.loop(Looper.java:317)  at android.os.HandlerThread.run(HandlerThread.java:85)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()' on a null object reference at com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth.signIn(GoogleAuth.java:81) at java.lang.reflect.Method.invoke(Native Method)  at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)  at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:816)  at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)  at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:959)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loopOnce(Looper.java:232)  at android.os.Looper.loop(Looper.java:317)  at android.os.HandlerThread.run(HandlerThread.java:85) 

Yeahh man i found the issue come mail me i'll send you the zip file hamza.ahtsham1@gmail.com

MalikHamza007 commented 2 months ago

Hello everyone,

i have solved the issue and it works perfecto even the app has gone into production. Now here me out everyone except creating your own client ids from the google cloud console use firebase add your web ,android ,ios and enable google authentication from the fireBase this is the important step and then go to google cloud console select your firebase project from there and use the ids.Now our setup is ready and we come to code make sure that to test the app natively the build apk will be made from that system whose sha-1 fingerprints are added to firebase.

In the code initialize the pluggin in the page or component where your are using Google login and use your web client id for every configuration just add your android client id in the capacitor config ts or json. Trust me this will work.

For the android don't forget to add your permissions , your configuration in the strings.xml and registering the pluggin in the Main Activity Java.

If you need more Help I'll send you the zip file of my project just do npm i and your good to go This is my mail 'hamza.ahtsham1@gmail.com'

PatricioRedegal commented 1 month ago

I have tested with Capacitor 6, and it works correctly.

This is my code:

let googleClientId;
if (Capacitor.getPlatform() === 'web') {
  googleClientId = this.webClientId;
} else if (Capacitor.getPlatform() === 'android') {
  googleClientId = this.webClientId;
} else if (Capacitor.getPlatform() === 'ios') {
  googleClientId = this.iosClientId;
} else {
  console.error('Not supported');
}
await GoogleAuth.initialize({
  clientId: googleClientId,
});

const googleUser = await GoogleAuth.signIn();

On iOS, send the iOS key. For Android, you need to create the project in Firebase, add the SHA-1 from the developer and production environments (in Android, you must obtain the SHA-1 from the Play Console for it to work once the version is published), and download the google-services.json file and place it in the project.

After this, on Android, send the web key, not the Android key.

riderx commented 1 month ago

https://github.com/Cap-go/capacitor-social-login is now the official alternative, if you need more robust login please migrate, it's 99% the same API CleanShot 2024-10-14 at 15 51 02