NativeScript / plugins

@nativescript plugins to help with your developments.
https://docs.nativescript.org/plugins/index.html
Apache License 2.0
189 stars 107 forks source link

Android Google SignIn 2.0.2: user is always null #492

Closed felixkrautschuk closed 1 year ago

felixkrautschuk commented 1 year ago

After upgrading @nativescript/google-signin from 1.0.1 to 2.0.2, the user object inside the Promise of the signIn method is always null.

GoogleSignin.signIn().then((user: User) => {
    console.log(user); //null
    console.log(GoogleSignin.getCurrentUser()); //null
});

Using version 1.0.1, this worked as expected.

In the launchEvent, I call configure like this:

GoogleSignin.configure({}).then(() => {
    console.log("GoogleSignIn configure done");
}, (e) => {
    console.log("GoogleSignIn configure error");
    console.log(e);
});

I am passing an empty object, as the web-client-id is already set as _default_web_clientid inside the strings.xml

NathanWalker commented 1 year ago

Thank you @felixkrautschuk I'm looking into this!

NathanWalker commented 1 year ago

@felixkrautschuk make sure your Info.plist contains the following required key (this is required now), repace {your-client-id} with yours according to GoogleService-Info.plist:

<key>GIDClientID</key>
<string>{your-client-id}.apps.googleusercontent.com</string>

And also, as usual the reverse client scheme as well, eg:

<key>CFBundleURLTypes</key>
    <array>
      <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLName</key>
        <string>REVERSED_CLIENT_ID</string>
        <key>CFBundleURLSchemes</key>
        <array>
          <string>com.googleusercontent.apps.{your-client-id}</string>
        </array>
      </dict>
    </array>

Also be sure you have called configure in main.ts (or app.ts depending on project type), eg:

import { GoogleSignin } from '@nativescript/google-signin';

GoogleSignin.configure();

With those in place, I see a valid user returned:

const user = await GoogleSignin.signIn();
console.log('displayName:', user.displayName); // 'My Valid Account Name'
console.log('idToken:', user.idToken); // 'eyJhbGciOiJSUzI1NiIsImtpZCI6Ijg...' (complete idToken)
felixkrautschuk commented 1 year ago

@NathanWalker thanks for your reply.

On iOS, the issue does not occur. I already added the GIDClientID to Info.plist, everything is working there.

Only Android is affected. It was working with v. 1.0.1, but after upgrading to 2.0.2, the user object is always null.

NathanWalker commented 1 year ago

We're going to move the .kt source to directly inside the plugin to regen latest, looking at Android.

NathanWalker commented 1 year ago

Hi @felixkrautschuk when you get a chance, could you try setting package to 2.0.4 and then ns clean. Should resolve Android but let us know.

felixkrautschuk commented 1 year ago

@NathanWalker thank you for the fix! I can confirm that it s working now on Android.

jackel27 commented 1 year ago

In Android, the activity context is not available until after the application's main activity has been created. If the GoogleSignin.configure() method requires the Android activity context, it must be called after the activity is created. I'm trying to find the correct placement. Maybe I could call it from a component once created has been called.

jackel27 commented 1 year ago

I'm using vue, but since GoogleSignin.configure() method requires the Android activity context, and must be called after the activity is created, I made a service file:

import { GoogleSignin } from '@nativescript/google-signin'
async function configureGoogle() {
    try {
        // Configure Google Sign-In
        await GoogleSignin.configure()
        console.log('Google Sign-In configured')
    } catch(error) {
        console.log('Error during configuration', error)
    }
}

async function signInWithGoogle() {
    let user = null
    try {
        user = await GoogleSignin.signIn()
        console.log(user)
    } catch(error) {
        console.log('Error during sign in', error)
    }
    return user
}

export { configureGoogle, signInWithGoogle }

Then in my component initialization (in my case, App.vue created () function), I call the configure().

Then, on my button or wherever you want to have the login, just call the signInWithGoogle(). Worked like a charm. I'm still curious however on how to wait for the activity to finish. I'll look into it.

triniwiz commented 1 year ago

You can listen to the activityCreated event

Application.android.on('activityCreated', (args) => {
        console.log('activityCreated');
        GoogleSignin.configure();
    });