From a51927ff3acce94cedcd7bfc218952bb106db904 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sat, 21 May 2022 18:12:10 +0200 Subject: [PATCH] refactor(authentication): minor changes (#97) * refactor(authentication): minor changes * style: format --- .../handlers/AppleAuthProviderHandler.java | 8 +-- .../handlers/FacebookAuthProviderHandler.java | 30 +++++------ .../handlers/GoogleAuthProviderHandler.java | 12 +++-- .../handlers/OAuthProviderHandler.java | 8 +-- .../PlayGamesAuthProviderHandler.java | 52 ++++++++++--------- .../Handlers/OAuthProviderHandler.swift | 4 +- packages/authentication/src/web.ts | 36 ++++++------- 7 files changed, 79 insertions(+), 71 deletions(-) diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java index fd8fd695..7b555e0b 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/AppleAuthProviderHandler.java @@ -64,7 +64,7 @@ private String sha256(String s) throws NoSuchAlgorithmException { public void signIn(PluginCall call) { OAuthProvider.Builder provider = OAuthProvider.newBuilder("apple.com"); - applySignInConfig(call, provider); + applySignInOptions(call, provider); Task pendingResultTask = pluginImplementation.getFirebaseAuthInstance().getPendingAuthResult(); if (pendingResultTask == null) { currentNonce = generateNonce(32); @@ -103,7 +103,7 @@ private void finishActivityForSignIn(final PluginCall call, Task pen .addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception)); } - private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) { + private void applySignInOptions(PluginCall call, OAuthProvider.Builder provider) { JSArray customParameters = call.getArray("customParameters"); if (customParameters != null) { try { @@ -118,7 +118,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) provider.addCustomParameter(key, value); } } catch (JSONException exception) { - Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception); + Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception); } } @@ -128,7 +128,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) List scopeList = scopes.toList(); provider.setScopes(scopeList); } catch (JSONException exception) { - Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception); + Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception); } } } diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java index e32c0145..8b75f442 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/FacebookAuthProviderHandler.java @@ -59,23 +59,9 @@ public void onError(FacebookException exception) { } } - private void applySignInConfig(LoginButton button) { - JSArray scopes = this.savedCall.getArray("scopes"); - if (scopes != null) { - try { - List scopeList = scopes.toList(); - scopeList.add("email"); - scopeList.add("public_profile"); - button.setPermissions(scopeList); - } catch (JSONException exception) { - Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception); - } - } - } - public void signIn(PluginCall call) { this.savedCall = call; - this.applySignInConfig(this.loginButton); + this.applySignInOptions(call, this.loginButton); this.loginButton.performClick(); } @@ -87,6 +73,20 @@ public void handleOnActivityResult(int requestCode, int resultCode, Intent data) mCallbackManager.onActivityResult(requestCode, resultCode, data); } + private void applySignInOptions(PluginCall call, LoginButton button) { + JSArray scopes = call.getArray("scopes"); + if (scopes != null) { + try { + List scopeList = scopes.toList(); + scopeList.add("email"); + scopeList.add("public_profile"); + button.setPermissions(scopeList); + } catch (JSONException exception) { + Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception); + } + } + } + private void handleSuccessCallback(LoginResult loginResult) { AccessToken accessToken = loginResult.getAccessToken(); String accessTokenString = accessToken.getToken(); diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java index c093c552..b9b96ae9 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java @@ -30,11 +30,11 @@ public class GoogleAuthProviderHandler { public GoogleAuthProviderHandler(FirebaseAuthentication pluginImplementation) { this.pluginImplementation = pluginImplementation; - this.buildGoogleSignInClient(null); + this.mGoogleSignInClient = buildGoogleSignInClient(); } public void signIn(PluginCall call) { - this.buildGoogleSignInClient(call); + mGoogleSignInClient = buildGoogleSignInClient(call); Intent signInIntent = mGoogleSignInClient.getSignInIntent(); pluginImplementation.startActivityForResult(call, signInIntent, "handleGoogleAuthProviderActivityResult"); } @@ -56,7 +56,11 @@ public void handleOnActivityResult(PluginCall call, ActivityResult result) { } } - private void buildGoogleSignInClient(@Nullable PluginCall call) { + private GoogleSignInClient buildGoogleSignInClient() { + return buildGoogleSignInClient(null); + } + + private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) { GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id)) .requestEmail(); @@ -75,6 +79,6 @@ private void buildGoogleSignInClient(@Nullable PluginCall call) { } } - mGoogleSignInClient = GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build()); + return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build()); } } diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java index c9cfd64c..26ff7340 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/OAuthProviderHandler.java @@ -25,7 +25,7 @@ public OAuthProviderHandler(FirebaseAuthentication pluginImplementation) { public void signIn(PluginCall call, String providerId) { OAuthProvider.Builder provider = OAuthProvider.newBuilder(providerId); - applySignInConfig(call, provider); + applySignInOptions(call, provider); Task pendingResultTask = pluginImplementation.getFirebaseAuthInstance().getPendingAuthResult(); if (pendingResultTask == null) { startActivityForSignIn(call, provider); @@ -58,7 +58,7 @@ private void finishActivityForSignIn(final PluginCall call, Task pen .addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception)); } - private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) { + private void applySignInOptions(PluginCall call, OAuthProvider.Builder provider) { JSArray customParameters = call.getArray("customParameters"); if (customParameters != null) { try { @@ -73,7 +73,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) provider.addCustomParameter(key, value); } } catch (JSONException exception) { - Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception); + Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception); } } @@ -83,7 +83,7 @@ private void applySignInConfig(PluginCall call, OAuthProvider.Builder provider) List scopeList = scopes.toList(); provider.setScopes(scopeList); } catch (JSONException exception) { - Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInConfig failed.", exception); + Log.e(FirebaseAuthenticationPlugin.TAG, "applySignInOptions failed.", exception); } } } diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java index 1b78f883..9c8b4930 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/PlayGamesAuthProviderHandler.java @@ -28,33 +28,11 @@ public class PlayGamesAuthProviderHandler { public PlayGamesAuthProviderHandler(FirebaseAuthentication pluginImplementation) { this.pluginImplementation = pluginImplementation; - this.buildGoogleSignInClient(null); - } - - private void buildGoogleSignInClient(@Nullable PluginCall call) { - GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) - .requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id)) - .requestEmail(); - - if (call != null) { - JSArray scopes = call.getArray("scopes"); - if (scopes != null) { - try { - List scopeList = scopes.toList(); - for (String scope : scopeList) { - gsob = gsob.requestScopes(new Scope(scope)); - } - } catch (JSONException exception) { - Log.e(FirebaseAuthenticationPlugin.TAG, "buildGoogleSignInClient failed.", exception); - } - } - } - - mGoogleSignInClient = GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build()); + this.mGoogleSignInClient = buildGoogleSignInClient(); } public void signIn(PluginCall call) { - this.buildGoogleSignInClient(call); + this.mGoogleSignInClient = buildGoogleSignInClient(call); Intent signInIntent = mGoogleSignInClient.getSignInIntent(); pluginImplementation.startActivityForResult(call, signInIntent, "handlePlayGamesAuthProviderActivityResult"); } @@ -76,4 +54,30 @@ public void handleOnActivityResult(PluginCall call, ActivityResult result) { pluginImplementation.handleFailedSignIn(call, null, exception); } } + + private GoogleSignInClient buildGoogleSignInClient() { + return buildGoogleSignInClient(null); + } + + private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) { + GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) + .requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id)) + .requestEmail(); + + if (call != null) { + JSArray scopes = call.getArray("scopes"); + if (scopes != null) { + try { + List scopeList = scopes.toList(); + for (String scope : scopeList) { + gsob = gsob.requestScopes(new Scope(scope)); + } + } catch (JSONException exception) { + Log.e(FirebaseAuthenticationPlugin.TAG, "buildGoogleSignInClient failed.", exception); + } + } + } + + return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), gsob.build()); + } } diff --git a/packages/authentication/ios/Plugin/Handlers/OAuthProviderHandler.swift b/packages/authentication/ios/Plugin/Handlers/OAuthProviderHandler.swift index 73e3e36b..dea813e9 100644 --- a/packages/authentication/ios/Plugin/Handlers/OAuthProviderHandler.swift +++ b/packages/authentication/ios/Plugin/Handlers/OAuthProviderHandler.swift @@ -13,13 +13,13 @@ class OAuthProviderHandler: NSObject { func signIn(call: CAPPluginCall, providerId: String) { self.provider = OAuthProvider(providerID: providerId) - self.applySignInConfig(call: call, provider: provider!) + self.applySignInOptions(call: call, provider: provider!) DispatchQueue.main.async { self.startSignInFlow() } } - private func applySignInConfig(call: CAPPluginCall, provider: OAuthProvider) { + private func applySignInOptions(call: CAPPluginCall, provider: OAuthProvider) { let customParameters = call.getArray("customParameters", JSObject.self) ?? [] for (_, customParameter) in customParameters.enumerated() { guard let key = customParameter["key"] as? String else { diff --git a/packages/authentication/src/web.ts b/packages/authentication/src/web.ts index c6193e4c..7dd76d8c 100644 --- a/packages/authentication/src/web.ts +++ b/packages/authentication/src/web.ts @@ -125,20 +125,9 @@ export class FirebaseAuthenticationWeb auth.languageCode = options.languageCode; } - private applySignInConfig( - provider: OAuthProvider | GoogleAuthProvider | FacebookAuthProvider, - options?: SignInOptions, - ) { - if (options?.scopes) { - for (const scope of options.scopes) { - provider.addScope(scope); - } - } - } - public async signInWithApple(options?: SignInOptions): Promise { const provider = new OAuthProvider('apple.com'); - this.applySignInConfig(provider, options); + this.applySignInOptions(options || {}, provider); const auth = getAuth(); const result = await signInWithPopup(auth, provider); const credential = OAuthProvider.credentialFromResult(result); @@ -169,7 +158,7 @@ export class FirebaseAuthenticationWeb options?: SignInOptions, ): Promise { const provider = new FacebookAuthProvider(); - this.applySignInConfig(provider, options); + this.applySignInOptions(options || {}, provider); const auth = getAuth(); const result = await signInWithPopup(auth, provider); const credential = FacebookAuthProvider.credentialFromResult(result); @@ -180,7 +169,7 @@ export class FirebaseAuthenticationWeb options?: SignInOptions, ): Promise { const provider = new OAuthProvider('github.com'); - this.applySignInConfig(provider, options); + this.applySignInOptions(options || {}, provider); const auth = getAuth(); const result = await signInWithPopup(auth, provider); const credential = OAuthProvider.credentialFromResult(result); @@ -191,7 +180,7 @@ export class FirebaseAuthenticationWeb options?: SignInOptions, ): Promise { const provider = new GoogleAuthProvider(); - this.applySignInConfig(provider, options); + this.applySignInOptions(options || {}, provider); const auth = getAuth(); const result = await signInWithPopup(auth, provider); const credential = GoogleAuthProvider.credentialFromResult(result); @@ -202,7 +191,7 @@ export class FirebaseAuthenticationWeb options?: SignInOptions, ): Promise { const provider = new OAuthProvider('microsoft.com'); - this.applySignInConfig(provider, options); + this.applySignInOptions(options || {}, provider); const auth = getAuth(); const result = await signInWithPopup(auth, provider); const credential = OAuthProvider.credentialFromResult(result); @@ -223,7 +212,7 @@ export class FirebaseAuthenticationWeb options?: SignInOptions, ): Promise { const provider = new OAuthProvider('twitter.com'); - this.applySignInConfig(provider, options); + this.applySignInOptions(options || {}, provider); const auth = getAuth(); const result = await signInWithPopup(auth, provider); const credential = OAuthProvider.credentialFromResult(result); @@ -232,7 +221,7 @@ export class FirebaseAuthenticationWeb public async signInWithYahoo(options?: SignInOptions): Promise { const provider = new OAuthProvider('yahoo.com'); - this.applySignInConfig(provider, options); + this.applySignInOptions(options || {}, provider); const auth = getAuth(); const result = await signInWithPopup(auth, provider); const credential = OAuthProvider.credentialFromResult(result); @@ -281,6 +270,17 @@ export class FirebaseAuthenticationWeb this.notifyListeners('authStateChange', change); } + private applySignInOptions( + options: SignInOptions, + provider: OAuthProvider | GoogleAuthProvider | FacebookAuthProvider, + ) { + if (options?.scopes) { + for (const scope of options.scopes) { + provider.addScope(scope); + } + } + } + private createSignInResultFromAuthCredential( user: FirebaseUser, credential: FirebaseAuthCredential | null,