Skip to content

Commit f5fa8d9

Browse files
authored
Custom scopes for different requirements (Android and iOS) (#269)
* Moved google auth initialize methods from the load function to the initialize function for users to add scopes on the fly * custom scopes for ios * update readme * update readme
1 parent f613415 commit f5fa8d9

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,15 @@ public class GoogleAuth extends Plugin {
5353

5454
private GoogleSignInClient googleSignInClient;
5555

56-
@Override
57-
public void load() {
58-
String clientId = getConfig().getString("androidClientId",
59-
getConfig().getString("clientId",
60-
this.getContext().getString(R.string.server_client_id)));
61-
62-
boolean forceCodeForRefreshToken = getConfig().getBoolean("forceCodeForRefreshToken", false);
63-
56+
public void loadSignInClient (String clientId, boolean forceCodeForRefreshToken, String[] scopeArray) {
6457
GoogleSignInOptions.Builder googleSignInBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
65-
.requestIdToken(clientId)
66-
.requestEmail();
58+
.requestIdToken(clientId)
59+
.requestEmail();
6760

6861
if (forceCodeForRefreshToken) {
6962
googleSignInBuilder.requestServerAuthCode(clientId, true);
7063
}
7164

72-
String[] scopeArray = getConfig().getArray("scopes", new String[] {});
7365
Scope[] scopes = new Scope[scopeArray.length - 1];
7466
Scope firstScope = new Scope(scopeArray[0]);
7567
for (int i = 1; i < scopeArray.length; i++) {
@@ -81,6 +73,9 @@ public void load() {
8173
googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions);
8274
}
8375

76+
@Override
77+
public void load() {}
78+
8479
@PluginMethod()
8580
public void signIn(PluginCall call) {
8681
Intent signInIntent = googleSignInClient.getSignInIntent();
@@ -177,6 +172,31 @@ public void onFailure(Exception e) {
177172

178173
@PluginMethod()
179174
public void initialize(final PluginCall call) {
175+
// get data from config
176+
String configClientId = getConfig().getString("androidClientId",
177+
getConfig().getString("clientId",
178+
this.getContext().getString(R.string.server_client_id)));
179+
boolean configForceCodeForRefreshToken = getConfig().getBoolean("forceCodeForRefreshToken", false);
180+
// need to get this as string so as to standardize with data from plugin call
181+
String configScopeArray = getConfig().getString("scopes", new String());
182+
183+
// get client id from plugin call, fallback to be client id from config
184+
String clientId = call.getData().getString("clientId", configClientId);
185+
// get forceCodeForRefreshToken from call, fallback to be from config
186+
boolean forceCodeForRefreshToken = call.getData().getBoolean("grantOfflineAccess", configForceCodeForRefreshToken);
187+
// get scopes from call, fallback to be from config
188+
String scopesStr = call.getData().getString("scopes", configScopeArray);
189+
// replace all the symbols from parsing array as string
190+
// leaving only scopes delimited by commas
191+
String replacedScopesStr = scopesStr
192+
.replaceAll("[\"\\[\\] ]", "")
193+
// this is for scopes that are in the form of a url
194+
.replace("\\", "");
195+
196+
// scope to be in the form of an array
197+
String[] scopeArray = replacedScopesStr.split(",");
198+
199+
loadSignInClient(clientId, forceCodeForRefreshToken, scopeArray);
180200
call.resolve();
181201
}
182202

ios/Plugin/Plugin.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,56 @@ public class GoogleAuth: CAPPlugin {
1414
var forceAuthCode: Bool = false;
1515
var additionalScopes: [String]!;
1616

17-
18-
public override func load() {
17+
func loadSignInClient (
18+
customClientId: String,
19+
customScopes: [String]
20+
) {
1921
googleSignIn = GIDSignIn.sharedInstance;
2022

2123
let serverClientId = getServerClientIdValue();
22-
23-
guard let clientId = getClientIdValue() else {
24-
NSLog("no client id found in config")
25-
return;
26-
}
2724

28-
googleSignInConfiguration = GIDConfiguration.init(clientID: clientId, serverClientID: serverClientId)
25+
googleSignInConfiguration = GIDConfiguration.init(clientID: customClientId, serverClientID: serverClientId)
2926

3027
// these are scopes granted by default by the signIn method
3128
let defaultGrantedScopes = ["email", "profile", "openid"];
32-
3329
// these are scopes we will need to request after sign in
34-
additionalScopes = (getConfig().getArray("scopes") as? [String] ?? []).filter {
30+
additionalScopes = customScopes.filter {
3531
return !defaultGrantedScopes.contains($0);
3632
};
37-
33+
3834
forceAuthCode = getConfig().getBoolean("forceCodeForRefreshToken", false)
3935

4036
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(Notification.Name.capacitorOpenURL.rawValue), object: nil);
4137
}
4238

39+
40+
public override func load() {
41+
}
42+
4343
@objc
4444
func initialize(_ call: CAPPluginCall) {
45+
// get client id from initialize, with client id from config file as fallback
46+
guard let clientId = call.getString("clientId") ?? getClientIdValue() as? String else {
47+
NSLog("no client id found in config");
48+
call.resolve();
49+
return;
50+
}
51+
52+
// get scopes from initialize, with scopes from config file as fallback
53+
let customScopes = call.getArray("scopes", String.self) ?? (
54+
getConfigValue("scopes") as? [String] ?? []
55+
);
56+
57+
// get force auth code from initialize, with config from config file as fallback
58+
forceAuthCode = call.getBool("grantOfflineAccess") ?? (
59+
getConfigValue("forceCodeForRefreshToken") as? Bool ?? false
60+
);
61+
62+
// load client
63+
self.loadSignInClient(
64+
customClientId: clientId,
65+
customScopes: customScopes
66+
)
4567
call.resolve();
4668
}
4769

0 commit comments

Comments
 (0)