Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Commit

Permalink
Fix linking account on Android #43 (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrfarid140 authored Mar 6, 2021
1 parent f082d84 commit 39aa267
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand All @@ -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()
}
40 changes: 39 additions & 1 deletion firebase_auth_oauth/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ class MyApp extends StatelessWidget {
}
}

Future<void> performLink(String provider, List<String> scopes,
Map<String, String> 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(
Expand Down Expand Up @@ -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"),
)
]
],
);
})),
Expand Down

0 comments on commit 39aa267

Please sign in to comment.