From b78ab31a631f611cc17e8994ef7090eb06284923 Mon Sep 17 00:00:00 2001 From: AlvinT Date: Wed, 5 Apr 2023 15:49:06 +0800 Subject: [PATCH 1/4] Moved google auth initialize methods from the load function to the initialize function for users to add scopes on the fly --- .../capacitor/GoogleAuth/GoogleAuth.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java b/android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java index d700c3b..9b3dd84 100644 --- a/android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java +++ b/android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java @@ -51,23 +51,15 @@ public class GoogleAuth extends Plugin { private GoogleSignInClient googleSignInClient; - @Override - public void load() { - String clientId = getConfig().getString("androidClientId", - getConfig().getString("clientId", - this.getContext().getString(R.string.server_client_id))); - - boolean forceCodeForRefreshToken = getConfig().getBoolean("forceCodeForRefreshToken", false); - + public void loadSignInClient (String clientId, boolean forceCodeForRefreshToken, String[] scopeArray) { GoogleSignInOptions.Builder googleSignInBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) - .requestIdToken(clientId) - .requestEmail(); + .requestIdToken(clientId) + .requestEmail(); if (forceCodeForRefreshToken) { googleSignInBuilder.requestServerAuthCode(clientId, true); } - String[] scopeArray = getConfig().getArray("scopes", new String[] {}); Scope[] scopes = new Scope[scopeArray.length - 1]; Scope firstScope = new Scope(scopeArray[0]); for (int i = 1; i < scopeArray.length; i++) { @@ -79,6 +71,9 @@ public void load() { googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions); } + @Override + public void load() {} + @PluginMethod() public void signIn(PluginCall call) { Intent signInIntent = googleSignInClient.getSignInIntent(); @@ -146,6 +141,31 @@ public void signOut(final PluginCall call) { @PluginMethod() public void initialize(final PluginCall call) { + // get data from config + String configClientId = getConfig().getString("androidClientId", + getConfig().getString("clientId", + this.getContext().getString(R.string.server_client_id))); + boolean configForceCodeForRefreshToken = getConfig().getBoolean("forceCodeForRefreshToken", false); + // need to get this as string so as to standardize with data from plugin call + String configScopeArray = getConfig().getString("scopes", new String()); + + // get client id from plugin call, fallback to be client id from config + String clientId = call.getData().getString("clientId", configClientId); + // get forceCodeForRefreshToken from call, fallback to be from config + boolean forceCodeForRefreshToken = call.getData().getBoolean("grantOfflineAccess", configForceCodeForRefreshToken); + // get scopes from call, fallback to be from config + String scopesStr = call.getData().getString("scopes", configScopeArray); + // replace all the symbols from parsing array as string + // leaving only scopes delimited by commas + String replacedScopesStr = scopesStr + .replaceAll("[\"\\[\\] ]", "") + // this is for scopes that are in the form of a url + .replace("\\", ""); + + // scope to be in the form of an array + String[] scopeArray = replacedScopesStr.split(","); + + loadSignInClient(clientId, forceCodeForRefreshToken, scopeArray); call.resolve(); } From e2672758a6847217c489f945a013234fbd987cfb Mon Sep 17 00:00:00 2001 From: AlvinT Date: Thu, 6 Apr 2023 17:04:25 +0800 Subject: [PATCH 2/4] custom scopes for ios --- ios/Plugin/Plugin.swift | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index 5810584..d706aef 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -14,36 +14,54 @@ public class GoogleAuth: CAPPlugin { var forceAuthCode: Bool = false; var additionalScopes: [String]!; - - public override func load() { + func loadSignInClient ( + customClientId: String, + customScopes: [String] + ) { googleSignIn = GIDSignIn.sharedInstance; let serverClientId = getServerClientIdValue(); - - guard let clientId = getClientIdValue() else { - NSLog("no client id found in config") - return; - } - googleSignInConfiguration = GIDConfiguration.init(clientID: clientId, serverClientID: serverClientId) + googleSignInConfiguration = GIDConfiguration.init(clientID: customClientId, serverClientID: serverClientId) // these are scopes granted by default by the signIn method let defaultGrantedScopes = ["email", "profile", "openid"]; - // these are scopes we will need to request after sign in - additionalScopes = (getConfigValue("scopes") as? [String] ?? []).filter { + additionalScopes = customScopes.filter { return !defaultGrantedScopes.contains($0); }; - - if let forceAuthCodeConfig = getConfigValue("forceCodeForRefreshToken") as? Bool { - forceAuthCode = forceAuthCodeConfig; - } NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(Notification.Name.capacitorOpenURL.rawValue), object: nil); } + + public override func load() { + } + @objc func initialize(_ call: CAPPluginCall) { + // get client id from initialize, with client id from config file as fallback + guard let clientId = call.getString("clientId") ?? getClientIdValue() as? String else { + NSLog("no client id found in config"); + call.resolve(); + return; + } + + // get scopes from initialize, with scopes from config file as fallback + let customScopes = call.getArray("scopes", String.self) ?? ( + getConfigValue("scopes") as? [String] ?? [] + ); + + // get force auth code from initialize, with config from config file as fallback + forceAuthCode = call.getBool("grantOfflineAccess") ?? ( + getConfigValue("forceCodeForRefreshToken") as? Bool ?? false + ); + + // load client + self.loadSignInClient( + customClientId: clientId, + customScopes: customScopes + ) call.resolve(); } From e4b70d6df1f0b57aefc9222eec4a12e828dafa94 Mon Sep 17 00:00:00 2001 From: AlvinT Date: Thu, 6 Apr 2023 21:43:44 +0800 Subject: [PATCH 3/4] update readme --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a1396c2..d081b62 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ If need migrate to different Capacitor versions [see instruction for migrate plu ## Usage -### WEB +### ALL PLATFORMS Register plugin and manually initialize @@ -53,7 +53,12 @@ GoogleAuth.initialize({ }); ``` -or if need use meta tags (Optional): +**_NOTE:_** The custom paramters in the initialize function only applies to Android and iOS platform after version {{ version number here }}. This only applies to the Web platform before verion {{ version number here }}. + +### WEB + + +if need use meta tags (Optional): ```html From 94f42621244e251d8a2945808e25a998dc6c49e6 Mon Sep 17 00:00:00 2001 From: AlvinT Date: Thu, 6 Apr 2023 21:48:11 +0800 Subject: [PATCH 4/4] update readme --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d081b62..a1396c2 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ If need migrate to different Capacitor versions [see instruction for migrate plu ## Usage -### ALL PLATFORMS +### WEB Register plugin and manually initialize @@ -53,12 +53,7 @@ GoogleAuth.initialize({ }); ``` -**_NOTE:_** The custom paramters in the initialize function only applies to Android and iOS platform after version {{ version number here }}. This only applies to the Web platform before verion {{ version number here }}. - -### WEB - - -if need use meta tags (Optional): +or if need use meta tags (Optional): ```html