Skip to content

Commit 9298f1c

Browse files
awazgyawalicollinjackson
authored andcommitted
Migrated linkWithCredential function to FirebaseUser object instead of FirebaseAuth (flutter#1584)
* Migrated linkWithCredential function to user object
1 parent c19d60a commit 9298f1c

File tree

5 files changed

+112
-43
lines changed

5 files changed

+112
-43
lines changed

packages/firebase_auth/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.11.0
2+
3+
* **Breaking change**: `linkWithCredential` is now a function of `FirebaseUser`instead of
4+
`FirebaseAuth`.
5+
* Added test for newer `linkWithCredential` function.
6+
17
## 0.10.0+1
28

39
* Increase Firebase/Auth CocoaPod dependency to '~> 6.0'.

packages/firebase_auth/lib/src/firebase_auth.dart

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -417,39 +417,6 @@ class FirebaseAuth {
417417
return currentUser;
418418
}
419419

420-
/// Associates a user account from a third-party identity provider with this
421-
/// user and returns additional identity provider data.
422-
///
423-
/// This allows the user to sign in to this account in the future with
424-
/// the given account.
425-
///
426-
/// Errors:
427-
/// • `ERROR_WEAK_PASSWORD` - If the password is not strong enough.
428-
/// • `ERROR_INVALID_CREDENTIAL` - If the credential is malformed or has expired.
429-
/// • `ERROR_CREDENTIAL_ALREADY_IN_USE` - If the account is already in use by a different account.
430-
/// • `ERROR_USER_DISABLED` - If the user has been disabled (for example, in the Firebase console)
431-
/// • `ERROR_REQUIRES_RECENT_LOGIN` - If the user's last sign-in time does not meet the security threshold. Use reauthenticate methods to resolve.
432-
/// • `ERROR_PROVIDER_ALREADY_LINKED` - If the current user already has an account of this type linked.
433-
/// • `ERROR_OPERATION_NOT_ALLOWED` - Indicates that this type of account is not enabled.
434-
/// • `ERROR_INVALID_ACTION_CODE` - If the action code in the link is malformed, expired, or has already been used.
435-
/// This can only occur when using [EmailAuthProvider.getCredentialWithLink] to obtain the credential.
436-
Future<FirebaseUser> linkWithCredential(AuthCredential credential) async {
437-
assert(credential != null);
438-
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
439-
// https://github.com/flutter/flutter/issues/26431
440-
// ignore: strong_mode_implicit_dynamic_method
441-
final Map<dynamic, dynamic> data = await channel.invokeMethod(
442-
'linkWithCredential',
443-
<String, dynamic>{
444-
'app': app.name,
445-
'provider': credential._provider,
446-
'data': credential._data,
447-
},
448-
);
449-
final FirebaseUser currentUser = FirebaseUser._(data, app);
450-
return currentUser;
451-
}
452-
453420
/// Sets the user-facing language code for auth operations that can be
454421
/// internationalized, such as [sendEmailVerification]. This language
455422
/// code should follow the conventions defined by the IETF in BCP47.

packages/firebase_auth/lib/src/firebase_user.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ class FirebaseUser extends UserInfo {
4444
});
4545
}
4646

47+
/// Associates a user account from a third-party identity provider with this
48+
/// user and returns additional identity provider data.
49+
///
50+
/// This allows the user to sign in to this account in the future with
51+
/// the given account.
52+
///
53+
/// Errors:
54+
/// • `ERROR_WEAK_PASSWORD` - If the password is not strong enough.
55+
/// • `ERROR_INVALID_CREDENTIAL` - If the credential is malformed or has expired.
56+
/// • `ERROR_CREDENTIAL_ALREADY_IN_USE` - If the account is already in use by a different account.
57+
/// • `ERROR_USER_DISABLED` - If the user has been disabled (for example, in the Firebase console)
58+
/// • `ERROR_REQUIRES_RECENT_LOGIN` - If the user's last sign-in time does not meet the security threshold. Use reauthenticate methods to resolve.
59+
/// • `ERROR_PROVIDER_ALREADY_LINKED` - If the current user already has an account of this type linked.
60+
/// • `ERROR_OPERATION_NOT_ALLOWED` - Indicates that this type of account is not enabled.
61+
/// • `ERROR_INVALID_ACTION_CODE` - If the action code in the link is malformed, expired, or has already been used.
62+
/// This can only occur when using [EmailAuthProvider.getCredentialWithLink] to obtain the credential.
63+
Future<FirebaseUser> linkWithCredential(AuthCredential credential) async {
64+
assert(credential != null);
65+
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
66+
// https://github.com/flutter/flutter/issues/26431
67+
// ignore: strong_mode_implicit_dynamic_method
68+
final Map<dynamic, dynamic> data = await FirebaseAuth.channel.invokeMethod(
69+
'linkWithCredential',
70+
<String, dynamic>{
71+
'app': _app.name,
72+
'provider': credential._provider,
73+
'data': credential._data,
74+
},
75+
);
76+
final FirebaseUser currentUser = FirebaseUser._(data, _app);
77+
return currentUser;
78+
}
79+
4780
/// Initiates email verification for the user.
4881
Future<void> sendEmailVerification() async {
4982
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.

packages/firebase_auth/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Flutter plugin for Firebase Auth, enabling Android and iOS
44
like Google, Facebook and Twitter.
55
author: Flutter Team <[email protected]>
66
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_auth
7-
version: "0.10.0+1"
7+
version: "0.11.0"
88

99
flutter:
1010
plugin:

packages/firebase_auth/test/firebase_auth_test.dart

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,18 @@ void main() {
218218
219219
link: '<Url with domain from your Firebase project>',
220220
);
221-
final FirebaseUser user = await auth.linkWithCredential(credential);
221+
FirebaseUser user = await auth.currentUser();
222+
user = await user.linkWithCredential(credential);
222223
verifyUser(user);
223224
expect(
224225
log,
225226
<Matcher>[
227+
isMethodCall(
228+
'currentUser',
229+
arguments: <String, dynamic>{
230+
'app': auth.app.name,
231+
},
232+
),
226233
isMethodCall(
227234
'linkWithCredential',
228235
arguments: <String, dynamic>{
@@ -294,11 +301,18 @@ void main() {
294301
authToken: kMockIdToken,
295302
authTokenSecret: kMockAccessToken,
296303
);
297-
final FirebaseUser user = await auth.linkWithCredential(credential);
304+
FirebaseUser user = await auth.currentUser();
305+
user = await user.linkWithCredential(credential);
298306
verifyUser(user);
299307
expect(
300308
log,
301309
<Matcher>[
310+
isMethodCall(
311+
'currentUser',
312+
arguments: <String, dynamic>{
313+
'app': auth.app.name,
314+
},
315+
),
302316
isMethodCall(
303317
'linkWithCredential',
304318
arguments: <String, dynamic>{
@@ -343,11 +357,18 @@ void main() {
343357
final AuthCredential credential = GithubAuthProvider.getCredential(
344358
token: kMockGithubToken,
345359
);
346-
final FirebaseUser user = await auth.linkWithCredential(credential);
360+
FirebaseUser user = await auth.currentUser();
361+
user = await user.linkWithCredential(credential);
347362
verifyUser(user);
348363
expect(
349364
log,
350365
<Matcher>[
366+
isMethodCall(
367+
'currentUser',
368+
arguments: <String, dynamic>{
369+
'app': auth.app.name,
370+
},
371+
),
351372
isMethodCall(
352373
'linkWithCredential',
353374
arguments: <String, dynamic>{
@@ -390,11 +411,18 @@ void main() {
390411
email: kMockEmail,
391412
password: kMockPassword,
392413
);
393-
final FirebaseUser user = await auth.linkWithCredential(credential);
414+
FirebaseUser user = await auth.currentUser();
415+
user = await user.linkWithCredential(credential);
394416
verifyUser(user);
395417
expect(
396418
log,
397419
<Matcher>[
420+
isMethodCall(
421+
'currentUser',
422+
arguments: <String, dynamic>{
423+
'app': auth.app.name,
424+
},
425+
),
398426
isMethodCall(
399427
'linkWithCredential',
400428
arguments: <String, dynamic>{
@@ -602,11 +630,18 @@ void main() {
602630
idToken: kMockIdToken,
603631
accessToken: kMockAccessToken,
604632
);
605-
final FirebaseUser user = await auth.linkWithCredential(credential);
633+
FirebaseUser user = await auth.currentUser();
634+
user = await user.linkWithCredential(credential);
606635
verifyUser(user);
607636
expect(
608637
log,
609638
<Matcher>[
639+
isMethodCall(
640+
'currentUser',
641+
arguments: <String, dynamic>{
642+
'app': auth.app.name,
643+
},
644+
),
610645
isMethodCall(
611646
'linkWithCredential',
612647
arguments: <String, dynamic>{
@@ -626,11 +661,18 @@ void main() {
626661
final AuthCredential credential = FacebookAuthProvider.getCredential(
627662
accessToken: kMockAccessToken,
628663
);
629-
final FirebaseUser user = await auth.linkWithCredential(credential);
664+
FirebaseUser user = await auth.currentUser();
665+
user = await user.linkWithCredential(credential);
630666
verifyUser(user);
631667
expect(
632668
log,
633669
<Matcher>[
670+
isMethodCall(
671+
'currentUser',
672+
arguments: <String, dynamic>{
673+
'app': auth.app.name,
674+
},
675+
),
634676
isMethodCall(
635677
'linkWithCredential',
636678
arguments: <String, dynamic>{
@@ -673,11 +715,18 @@ void main() {
673715
authToken: kMockAuthToken,
674716
authTokenSecret: kMockAuthTokenSecret,
675717
);
676-
final FirebaseUser user = await auth.linkWithCredential(credential);
718+
FirebaseUser user = await auth.currentUser();
719+
user = await user.linkWithCredential(credential);
677720
verifyUser(user);
678721
expect(
679722
log,
680723
<Matcher>[
724+
isMethodCall(
725+
'currentUser',
726+
arguments: <String, dynamic>{
727+
'app': auth.app.name,
728+
},
729+
),
681730
isMethodCall(
682731
'linkWithCredential',
683732
arguments: <String, dynamic>{
@@ -722,11 +771,18 @@ void main() {
722771
final AuthCredential credential = GithubAuthProvider.getCredential(
723772
token: kMockGithubToken,
724773
);
725-
final FirebaseUser user = await auth.linkWithCredential(credential);
774+
FirebaseUser user = await auth.currentUser();
775+
user = await user.linkWithCredential(credential);
726776
verifyUser(user);
727777
expect(
728778
log,
729779
<Matcher>[
780+
isMethodCall(
781+
'currentUser',
782+
arguments: <String, dynamic>{
783+
'app': auth.app.name,
784+
},
785+
),
730786
isMethodCall(
731787
'linkWithCredential',
732788
arguments: <String, dynamic>{
@@ -769,11 +825,18 @@ void main() {
769825
email: kMockEmail,
770826
password: kMockPassword,
771827
);
772-
final FirebaseUser user = await auth.linkWithCredential(credential);
828+
FirebaseUser user = await auth.currentUser();
829+
user = await user.linkWithCredential(credential);
773830
verifyUser(user);
774831
expect(
775832
log,
776833
<Matcher>[
834+
isMethodCall(
835+
'currentUser',
836+
arguments: <String, dynamic>{
837+
'app': auth.app.name,
838+
},
839+
),
777840
isMethodCall(
778841
'linkWithCredential',
779842
arguments: <String, dynamic>{

0 commit comments

Comments
 (0)