Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom scopes for different requirements (Android) #267

Closed
wants to merge 1 commit into from

Conversation

AlvinTCH
Copy link
Contributor

@AlvinTCH AlvinTCH commented Apr 5, 2023

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 for 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 as it's used on the web and accessing the data inside Android for reusability

In JS, initialize GoogleAuth same as web

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,
});

In GoogleAuth.java, I moved the entire load function to another function. So it becomes as follows

public void loadSignInClient (String clientId, boolean forceCodeForRefreshToken, String[] scopeArray) {
    GoogleSignInOptions.Builder googleSignInBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
      .requestIdToken(clientId)
      .requestEmail();

    if (forceCodeForRefreshToken) {
      googleSignInBuilder.requestServerAuthCode(clientId, true);
    }

    Scope[] scopes = new Scope[scopeArray.length - 1];
    Scope firstScope = new Scope(scopeArray[0]);
    for (int i = 1; i < scopeArray.length; i++) {
      scopes[i - 1] = new Scope(scopeArray[i]);
    }
    googleSignInBuilder.requestScopes(firstScope, scopes);

    GoogleSignInOptions googleSignInOptions = googleSignInBuilder.build();
    googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions);
  }

 @Override
 public void load() {}

Then I call the loadSignInClient from initialize, while getting the custom data from the plugin call, with the data inside capacitor.json as a fallback

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

…itialize function for users to add scopes on the fly
@reslear
Copy link
Collaborator

reslear commented Apr 5, 2023

this is not part of #167 ?
ios solution?

@AlvinTCH
Copy link
Contributor Author

AlvinTCH commented Apr 5, 2023

Am not too sure if it's the same as #167 regarding the requirements. But it should solve the same issues i guess?

I need to get familiar with swift (which I am not ATM). so just in case the outcome is not that desirable, I was thinking of putting it up as a separate pull request in the next few days when I come up with the solution and test it inside my app

@AlvinTCH AlvinTCH closed this Apr 6, 2023
@AlvinTCH AlvinTCH deleted the androidCustomScope branch April 7, 2023 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants