CapacitorGoogleAuth icon indicating copy to clipboard operation
CapacitorGoogleAuth copied to clipboard

Custom scopes for different requirements (Android and iOS)

Open AlvinTCH opened this issue 2 years ago • 7 comments

Having custom scopes whenever the function initializes

I have an app whereby there are different scope requirements for different functions of the app so as to not request excessive scopes from the user.

For example,

  1. the part whereby I just need the user to log in, the scope of ['email', 'profile'] is sufficient.
  2. the part whereby I need access to the user's calendar, I need the scope of ['email', 'profile', 'https://www.googleapis.com/auth/calendar']

There is currently no convenient way to do this in the app as it takes the scope from the capacitor config (i.e capacitor.config.json)

So in this implementation, I am taking the data from the GoogleAuth.initialize function for reusability

In the component that requires login, initialize GoogleAuth with the appropriate parameters they need

import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

// use hook after platform dom ready
GoogleAuth.initialize({
  clientId: 'CLIENT_ID.apps.googleusercontent.com',
  scopes: ['profile', 'email'],
  grantOfflineAccess: true,
});

The general idea for this implementation:

  1. Move the load function to a function on its own (loadSignInClient)
  2. Under the initialize function, get the custom params from the call (GoogleAuth.initialize), with the data from capacitor.config.json as the fallback
  3. Pass the applicable parameters to loadSignInClient and use these applicable parameters to initialize the Google Signin Client

Testing

Currently, I have tested this implementation with my app to ensure that it only queries the applicable scope for the different features in my app. If required, I am considering creating an NPM package for my fork as I kind of need this feature urgently, and anyone who needs this implementation can use that package to test before this pull request is merged in.

Note

  1. I don't have any clue how to change a string ('["email", "profile"]') to a valid string[] easily in Java. So do update the code if anyone has a better idea on how to do it
  2. I don't really know if this implementation fits the requirements of #167
  3. When this pull request is merged in, I can help to update the readme

AlvinTCH avatar Apr 06 '23 13:04 AlvinTCH

interesting But it needs some fine-tuning.

reslear avatar May 30 '23 11:05 reslear

Just to add on, I am using this code in production and have no problems with it so far

Do let me know what can be improved on the code whether on readability or others. Would like to learn from it

AlvinTCH avatar Jun 02 '23 05:06 AlvinTCH

please, add example how to use

reslear avatar Jun 02 '23 09:06 reslear

we just need to reuse the same code example in the readme.md for web

Example:

import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

// use hook after platform dom ready
GoogleAuth.initialize({
  clientId: 'CLIENT_ID.apps.googleusercontent.com',
  scopes: ['profile', 'email'],
  grantOfflineAccess: true,
});

The parameters will be passed to the iOS or Android handlers automatically. So essentially there is no code change for the end user for this to work

AlvinTCH avatar Jun 06 '23 02:06 AlvinTCH

hi @AlvinTCH please sync changes with https://github.com/CodetrixStudio/CapacitorGoogleAuth/pull/321

reslear avatar Nov 17 '23 16:11 reslear

I still think your idea is great, I also think you should remove the

  • androidClientId
  • iosClientId

and add an example of how to use it dynamically.

@AlvinTCH could you help with that?

reslear avatar Nov 26 '23 16:11 reslear

hi @AlvinTCH please sync changes with #321

synced changes

I still think your idea is great, I also think you should remove the

  • androidClientId
  • iosClientId

and add an example of how to use it dynamically.

@AlvinTCH could you help with that?

For the androidClientId and iosClientId, it is currently in the code base and it might be a huge change that we can roll out in v4 of this package as there might be people using it. We can merge this in first and discuss about v4 in another separate pull request

Sorry I can't see which part that needs explanation due to my own blindsight as it is written by myself. Can you let me know which part of the explanation is not clear on how to use it dynamically?

@reslear

AlvinTCH avatar Jan 16 '24 01:01 AlvinTCH

hi @AlvinTCH let's fix the conflicts then, and I'll do the merge for the next major release

reslear avatar Apr 02 '24 15:04 reslear

Ok sure. Let me get to it when I get back tonight

AlvinTCH avatar Apr 03 '24 01:04 AlvinTCH

@reslear it is done

AlvinTCH avatar Apr 03 '24 12:04 AlvinTCH

by the way I have created an npm package with this particular implementation and have been using it for over a year

AlvinTCH avatar May 01 '24 01:05 AlvinTCH

sounds cool, and you're not using capacitor.config now?

P.s. I plan to merge the changes into this major release

reslear avatar May 01 '24 01:05 reslear

Thats a really fast reply. I am still putting the standard values at capacitor.config as a fallback

AlvinTCH avatar May 01 '24 01:05 AlvinTCH

@AlvinTCH @reslear this PR changes the default behavior of the plugin, this is not documented and prevents many apps from upgrading to v6.

riderx avatar Aug 15 '24 03:08 riderx

I made a PR to help users understand the changes: https://github.com/CodetrixStudio/CapacitorGoogleAuth/pull/390/files

riderx avatar Aug 15 '24 03:08 riderx