Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 6228808

Browse files
committed
adding support auth link for Twitter
* Link federated Twitter auth provider credentials to a user account. * This fix calls method ```linkWithCredential()``` like other providers (facebook, google).
1 parent 6683c7a commit 6228808

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public void onMethodCall(MethodCall call, Result result) {
111111
case "linkWithFacebookCredential":
112112
handleLinkWithFacebookCredential(call, result);
113113
break;
114+
case "linkWithTwitterCredential":
115+
handleLinkWithTwitterCredential(call, result);
116+
break;
114117
case "updateProfile":
115118
handleUpdateProfile(call, result);
116119
break;
@@ -372,6 +375,18 @@ private void handleLinkWithFacebookCredential(MethodCall call, final Result resu
372375
.addOnCompleteListener(new SignInCompleteListener(result));
373376
}
374377

378+
private void handleLinkWithTwitterCredential(MethodCall call, final Result result) {
379+
@SuppressWarnings("unchecked")
380+
Map<String, String> arguments = (Map<String, String>) call.arguments;
381+
String authToken = arguments.get("authToken");
382+
String authTokenSecret = arguments.get("authTokenSecret");
383+
AuthCredential credential = TwitterAuthProvider.getCredential(authToken, authTokenSecret);
384+
firebaseAuth
385+
.getCurrentUser()
386+
.linkWithCredential(credential)
387+
.addOnCompleteListener(new SignInCompleteListener(result));
388+
}
389+
375390
private void handleSignInWithFacebook(MethodCall call, final Result result) {
376391
@SuppressWarnings("unchecked")
377392
Map<String, String> arguments = (Map<String, String>) call.arguments;

packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
176176
completion:^(FIRUser *user, NSError *error) {
177177
[self sendResult:result forUser:user error:error];
178178
}];
179+
} else if ([@"linkWithTwitterCredential" isEqualToString:call.method]) {
180+
NSString *authToken = call.arguments[@"authToken"];
181+
NSString *authTokenSecret = call.arguments[@"authTokenSecret"];
182+
FIRAuthCredential *credential =
183+
[FIRTwitterAuthProvider credentialWithToken:authToken secret:authTokenSecret];
184+
[[FIRAuth auth].currentUser linkWithCredential:credential
185+
completion:^(FIRUser *user, NSError *error) {
186+
[self sendResult:result forUser:user error:error];
187+
}];
179188
} else if ([@"updateProfile" isEqualToString:call.method]) {
180189
FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest];
181190
if (call.arguments[@"displayName"]) {

packages/firebase_auth/lib/firebase_auth.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,21 @@ class FirebaseAuth {
428428
return currentUser;
429429
}
430430

431+
Future<FirebaseUser> linkWithTwitterCredential({
432+
@required String authToken,
433+
@required String authTokenSecret,
434+
}) async {
435+
final Map<dynamic, dynamic> data = await channel.invokeMethod(
436+
'linkWithTwitterCredential',
437+
<String, String>{
438+
'authToken': authToken,
439+
'authTokenSecret': authTokenSecret,
440+
},
441+
);
442+
final FirebaseUser currentUser = new FirebaseUser._(data);
443+
return currentUser;
444+
}
445+
431446
/// Sets the user-facing language code for auth operations that can be
432447
/// internationalized, such as [sendEmailVerification]. This language
433448
/// code should follow the conventions defined by the IETF in BCP47.

packages/firebase_auth/test/firebase_auth_test.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const String kMockEmail = 'test@example.com';
1616
const String kMockPassword = 'passw0rd';
1717
const String kMockIdToken = '12345';
1818
const String kMockAccessToken = '67890';
19+
const String kMockAuthToken = '23456';
20+
const String kMockAuthTokenSecret = '78901';
1921
const String kMockCustomToken = '12345';
2022
const String kMockPhoneNumber = '5555555555';
2123
const String kMockVerificationId = '12345';
@@ -247,6 +249,26 @@ void main() {
247249
);
248250
});
249251

252+
test('linkWithTwitterCredential', () async {
253+
final FirebaseUser user = await auth.linkWithTwitterCredential(
254+
authToken: kMockAuthToken,
255+
authTokenSecret: kMockAuthTokenSecret,
256+
);
257+
verifyUser(user);
258+
expect(
259+
log,
260+
<Matcher>[
261+
isMethodCall(
262+
'linkWithTwitterCredential',
263+
arguments: <String, String>{
264+
'authToken': kMockAuthToken,
265+
'authTokenSecret': kMockAuthTokenSecret,
266+
},
267+
),
268+
],
269+
);
270+
});
271+
250272
test('signInWithFacebook', () async {
251273
final FirebaseUser user = await auth.signInWithFacebook(
252274
accessToken: kMockAccessToken,
@@ -378,7 +400,7 @@ void main() {
378400

379401
test('updateEmail', () async {
380402
final String updatedEmail = 'atestemail@gmail.com';
381-
auth.updateEmail(email: updatedEmail);
403+
await auth.updateEmail(email: updatedEmail);
382404
expect(
383405
log,
384406
<Matcher>[

0 commit comments

Comments
 (0)