From 39aa26713bd1a4f30da65eaeda3c4b1f8b5ac9bf Mon Sep 17 00:00:00 2001 From: Amr Yousef Date: Sat, 6 Mar 2021 13:36:31 +0000 Subject: [PATCH] Fix linking account on Android #43 (#46) --- .../FirebaseAuthOAuthPlugin.kt | 51 ++++++++++++------- firebase_auth_oauth/example/lib/main.dart | 40 ++++++++++++++- 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/firebase_auth_oauth/android/src/main/kotlin/me/amryousef/apple/auth/firebase_auth_oauth/FirebaseAuthOAuthPlugin.kt b/firebase_auth_oauth/android/src/main/kotlin/me/amryousef/apple/auth/firebase_auth_oauth/FirebaseAuthOAuthPlugin.kt index b33e8ad..271aa47 100644 --- a/firebase_auth_oauth/android/src/main/kotlin/me/amryousef/apple/auth/firebase_auth_oauth/FirebaseAuthOAuthPlugin.kt +++ b/firebase_auth_oauth/android/src/main/kotlin/me/amryousef/apple/auth/firebase_auth_oauth/FirebaseAuthOAuthPlugin.kt @@ -84,24 +84,16 @@ class FirebaseAuthOAuthPlugin : FlutterPlugin, ActivityAware, MethodCallHandler FirebaseAuthOAuthPluginError .FirebaseAuthError(error) .toResult(result) - } ?: auth.startActivityForSignInWithProvider(it, provider) - .addOnSuccessListener { authResult -> - if (call.method == CREATE_USER_METHOD) { - result.success("") - return@addOnSuccessListener - } else if (call.method == LINK_USER_METHOD) { - val user = auth.currentUser - if (user == null) { - FirebaseAuthOAuthPluginError.PluginError( - "" - ).toResult(result) - } - user?.linkWithCredential(authResult.credential!!) - } + } ?: run { + val task = call.method.toSignInTask(provider, auth, result) + task.addOnSuccessListener { + result.success("") + return@addOnSuccessListener }.addOnFailureListener { error -> - FirebaseAuthOAuthPluginError - .FirebaseAuthError(error) - .toResult(result) + FirebaseAuthOAuthPluginError + .FirebaseAuthError(error) + .toResult(result) + } } } } @@ -124,4 +116,29 @@ class FirebaseAuthOAuthPlugin : FlutterPlugin, ActivityAware, MethodCallHandler override fun onDetachedFromActivityForConfigChanges() { activity = null } + + private fun String.toSignInTask( + provider: OAuthProvider, + auth: FirebaseAuth, + result: Result + ) = activity?.let { + when (this) { + LINK_USER_METHOD -> { + val user = auth.currentUser + if (user == null) { + FirebaseAuthOAuthPluginError.PluginError( + "" + ).toResult(result) + } + user!!.startActivityForLinkWithProvider(it, provider) + } + CREATE_USER_METHOD -> { + auth.startActivityForSignInWithProvider(it, provider) + } + else -> { + FirebaseAuthOAuthPluginError.PluginError("Unknown method called") + com.google.android.gms.tasks.Tasks.forCanceled() + } + } + } ?: com.google.android.gms.tasks.Tasks.forCanceled() } diff --git a/firebase_auth_oauth/example/lib/main.dart b/firebase_auth_oauth/example/lib/main.dart index 32783fc..2dce32d 100644 --- a/firebase_auth_oauth/example/lib/main.dart +++ b/firebase_auth_oauth/example/lib/main.dart @@ -28,6 +28,22 @@ class MyApp extends StatelessWidget { } } + Future performLink(String provider, List scopes, + Map parameters) async { + try { + await FirebaseAuthOAuth() + .linkExistingUserWithCredentials(provider, scopes, parameters); + } on PlatformException catch (error) { + /** + * The plugin has the following error codes: + * 1. FirebaseAuthError: FirebaseAuth related error + * 2. PlatformError: An platform related error + * 3. PluginError: An error from this plugin + */ + debugPrint("${error.code}: ${error.message}"); + } + } + @override Widget build(BuildContext context) { return MaterialApp( @@ -69,13 +85,35 @@ class MyApp extends StatelessWidget { child: Text("Sign in By Github"), ) ], - if (snapshot.data != null) + if (snapshot.data != null) ...[ + ElevatedButton( + onPressed: () async { + await performLink( + "apple.com", ["email"], {"locale": "en"}); + }, + child: Text("Link Sign in By Apple"), + ), + ElevatedButton( + onPressed: () async { + await performLink( + "twitter.com", ["email"], {"lang": "en"}); + }, + child: Text("Link Sign in By Twitter"), + ), + ElevatedButton( + onPressed: () async { + await performLink( + "github.com", ["user:email"], {"lang": "en"}); + }, + child: Text("Link Sign in By Github"), + ), ElevatedButton( onPressed: () async { await FirebaseAuth.instance.signOut(); }, child: Text("Logout"), ) + ] ], ); })),