Custom scopes for different requirements (Android and iOS)
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,
- the part whereby I just need the user to log in, the scope of
['email', 'profile']is sufficient. - 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:
- Move the load function to a function on its own (
loadSignInClient) - Under the
initializefunction, get the custom params from the call (GoogleAuth.initialize), with the data from capacitor.config.json as the fallback - Pass the applicable parameters to
loadSignInClientand 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
- 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
- I don't really know if this implementation fits the requirements of #167
- When this pull request is merged in, I can help to update the readme
interesting But it needs some fine-tuning.
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
please, add example how to use
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
hi @AlvinTCH please sync changes with https://github.com/CodetrixStudio/CapacitorGoogleAuth/pull/321
I still think your idea is great, I also think you should remove the
androidClientIdiosClientId
and add an example of how to use it dynamically.
@AlvinTCH could you help with that?
hi @AlvinTCH please sync changes with #321
synced changes
I still think your idea is great, I also think you should remove the
androidClientIdiosClientIdand 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
hi @AlvinTCH let's fix the conflicts then, and I'll do the merge for the next major release
Ok sure. Let me get to it when I get back tonight
@reslear it is done
by the way I have created an npm package with this particular implementation and have been using it for over a year
sounds cool, and you're not using capacitor.config now?
P.s. I plan to merge the changes into this major release
Thats a really fast reply. I am still putting the standard values at capacitor.config as a fallback
@AlvinTCH @reslear this PR changes the default behavior of the plugin, this is not documented and prevents many apps from upgrading to v6.
I made a PR to help users understand the changes: https://github.com/CodetrixStudio/CapacitorGoogleAuth/pull/390/files