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

Commit 92156b3

Browse files
committed
Added updatePassword functionality to firebase_auth plugin.
Move `updateEmail` and `updateProfile` from FirebaseAuth to FirebaseUser. Minor cleani-up in Java. Updated tests. Fix dartfmt issue. Fix merge conflicts.
1 parent be3e558 commit 92156b3

File tree

6 files changed

+114
-82
lines changed

6 files changed

+114
-82
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.6.0
2+
3+
* Addition of `updatePassword` function to `FirebaseUser`.
4+
* **Breaking Change**: `updateEmail` and `updateProfile` moved to `FirebaseUser`.
5+
This brings the `firebase_auth` package inline with other implementations and documentation.
6+
17
## 0.5.20
28

39
* Replaced usages of guava's: ImmutableList and ImmutableMap with platform

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

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ public void onMethodCall(MethodCall call, Result result) {
111111
case "linkWithFacebookCredential":
112112
handleLinkWithFacebookCredential(call, result);
113113
break;
114-
case "updateProfile":
115-
handleUpdateProfile(call, result);
116-
break;
117114
case "updateEmail":
118115
handleUpdateEmail(call, result);
119116
break;
117+
case "updatePassword":
118+
handleUpdatePassword(call, result);
119+
break;
120+
case "updateProfile":
121+
handleUpdateProfile(call, result);
122+
break;
120123
case "startListeningAuthState":
121124
handleStartListeningAuthState(call, result);
122125
break;
@@ -139,6 +142,7 @@ public void onMethodCall(MethodCall call, Result result) {
139142
}
140143

141144
private void handleSignInWithPhoneNumber(MethodCall call, Result result) {
145+
@SuppressWarnings("unchecked")
142146
Map<String, String> arguments = (Map<String, String>) call.arguments;
143147
String verificationId = arguments.get("verificationId");
144148
String smsCode = arguments.get("smsCode");
@@ -424,6 +428,24 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {
424428
});
425429
}
426430

431+
private void handleUpdateEmail(MethodCall call, final Result result) {
432+
@SuppressWarnings("unchecked")
433+
Map<String, String> arguments = (Map<String, String>) call.arguments;
434+
firebaseAuth
435+
.getCurrentUser()
436+
.updateEmail(arguments.get("email"))
437+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
438+
}
439+
440+
private void handleUpdatePassword(MethodCall call, final Result result) {
441+
@SuppressWarnings("unchecked")
442+
Map<String, String> arguments = (Map<String, String>) call.arguments;
443+
firebaseAuth
444+
.getCurrentUser()
445+
.updatePassword(arguments.get("password"))
446+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
447+
}
448+
427449
private void handleUpdateProfile(MethodCall call, final Result result) {
428450
@SuppressWarnings("unchecked")
429451
Map<String, String> arguments = (Map<String, String>) call.arguments;
@@ -439,40 +461,7 @@ private void handleUpdateProfile(MethodCall call, final Result result) {
439461
firebaseAuth
440462
.getCurrentUser()
441463
.updateProfile(builder.build())
442-
.addOnCompleteListener(
443-
new OnCompleteListener<Void>() {
444-
@Override
445-
public void onComplete(@NonNull Task<Void> task) {
446-
if (!task.isSuccessful()) {
447-
Exception e = task.getException();
448-
result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null);
449-
} else {
450-
result.success(null);
451-
}
452-
}
453-
});
454-
}
455-
456-
private void handleUpdateEmail(MethodCall call, final Result result) {
457-
@SuppressWarnings("unchecked")
458-
Map<String, String> arguments = (Map<String, String>) call.arguments;
459-
String email = arguments.get("email");
460-
461-
firebaseAuth
462-
.getCurrentUser()
463-
.updateEmail(email)
464-
.addOnCompleteListener(
465-
new OnCompleteListener<Void>() {
466-
@Override
467-
public void onComplete(@NonNull Task<Void> task) {
468-
if (!task.isSuccessful()) {
469-
Exception e = task.getException();
470-
result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null);
471-
} else {
472-
result.success(null);
473-
}
474-
}
475-
});
464+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
476465
}
477466

478467
private void handleStartListeningAuthState(MethodCall call, final Result result) {

packages/firebase_auth/example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class _MyHomePageState extends State<MyHomePage> {
9999
(AuthException authException) {
100100
setState(() {
101101
_message = Future<String>.value(
102-
'Phone numbber verification failed. Code: ${authException.code}. Message: ${authException.message}');
102+
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
103103
});
104104
};
105105

packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ - (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 ([@"updateEmail" isEqualToString:call.method]) {
180+
NSString *email = call.arguments[@"email"];
181+
[[FIRAuth auth].currentUser updateEmail:email
182+
completion:^(NSError *error) {
183+
[self sendResult:result forUser:nil error:error];
184+
}];
185+
} else if ([@"updatePassword" isEqualToString:call.method]) {
186+
NSString *password = call.arguments[@"password"];
187+
[[FIRAuth auth].currentUser updatePassword:password
188+
completion:^(NSError *error) {
189+
[self sendResult:result forUser:nil error:error];
190+
}];
179191
} else if ([@"updateProfile" isEqualToString:call.method]) {
180192
FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest];
181193
if (call.arguments[@"displayName"]) {
@@ -187,12 +199,6 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
187199
[changeRequest commitChangesWithCompletion:^(NSError *error) {
188200
[self sendResult:result forUser:nil error:error];
189201
}];
190-
} else if ([@"updateEmail" isEqualToString:call.method]) {
191-
NSString *toEmail = call.arguments[@"email"];
192-
[[FIRAuth auth].currentUser updateEmail:toEmail
193-
completion:^(NSError *_Nullable error) {
194-
[self sendResult:result forUser:nil error:error];
195-
}];
196202
} else if ([@"signInWithCustomToken" isEqualToString:call.method]) {
197203
NSString *token = call.arguments[@"token"];
198204
[[FIRAuth auth] signInWithCustomToken:token

packages/firebase_auth/lib/firebase_auth.dart

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ class FirebaseUser extends UserInfo {
9595
await FirebaseAuth.channel.invokeMethod('delete');
9696
}
9797

98+
/// Updates the email address of the user.
99+
Future<void> updateEmail(String email) async {
100+
assert(email != null);
101+
return await FirebaseAuth.channel.invokeMethod(
102+
'updateEmail',
103+
<String, String>{'email': email},
104+
);
105+
}
106+
107+
/// Updates the password of the user.
108+
Future<void> updatePassword(String password) async {
109+
assert(password != null);
110+
return await FirebaseAuth.channel.invokeMethod(
111+
'updatePassword',
112+
<String, String>{'password': password},
113+
);
114+
}
115+
116+
/// Updates the user profile information.
117+
Future<void> updateProfile(UserUpdateInfo userUpdateInfo) async {
118+
assert(userUpdateInfo != null);
119+
return await FirebaseAuth.channel.invokeMethod(
120+
'updateProfile',
121+
userUpdateInfo._updateData,
122+
);
123+
}
124+
98125
@override
99126
String toString() {
100127
return '$runtimeType($_data)';
@@ -369,26 +396,6 @@ class FirebaseAuth {
369396
return currentUser;
370397
}
371398

372-
Future<void> updateEmail({
373-
@required String email,
374-
}) async {
375-
assert(email != null);
376-
return await channel.invokeMethod(
377-
'updateEmail',
378-
<String, String>{
379-
'email': email,
380-
},
381-
);
382-
}
383-
384-
Future<void> updateProfile(UserUpdateInfo userUpdateInfo) async {
385-
assert(userUpdateInfo != null);
386-
return await channel.invokeMethod(
387-
'updateProfile',
388-
userUpdateInfo._updateData,
389-
);
390-
}
391-
392399
/// Links google account with current user and returns [Future<FirebaseUser>]
393400
///
394401
/// throws [PlatformException] when

packages/firebase_auth/test/firebase_auth_test.dart

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ void main() {
4141
return mockHandleId++;
4242
break;
4343
case "sendPasswordResetEmail":
44-
case "updateProfile":
45-
return null;
46-
break;
4744
case "updateEmail":
45+
case "updatePassword":
46+
case "updateProfile":
4847
return null;
4948
break;
5049
case "fetchProvidersForEmail":
@@ -359,13 +358,52 @@ void main() {
359358
);
360359
});
361360

361+
test('updateEmail', () async {
362+
final FirebaseUser user = await auth.currentUser();
363+
await user.updateEmail(kMockEmail);
364+
expect(log, <Matcher>[
365+
isMethodCall(
366+
'currentUser',
367+
arguments: null,
368+
),
369+
isMethodCall(
370+
'updateEmail',
371+
arguments: <String, String>{
372+
'email': kMockEmail,
373+
},
374+
),
375+
]);
376+
});
377+
378+
test('updatePassword', () async {
379+
final FirebaseUser user = await auth.currentUser();
380+
await user.updatePassword(kMockPassword);
381+
expect(log, <Matcher>[
382+
isMethodCall(
383+
'currentUser',
384+
arguments: null,
385+
),
386+
isMethodCall(
387+
'updatePassword',
388+
arguments: <String, String>{
389+
'password': kMockPassword,
390+
},
391+
),
392+
]);
393+
});
394+
362395
test('updateProfile', () async {
363396
final UserUpdateInfo userUpdateInfo = new UserUpdateInfo();
364397
userUpdateInfo.photoUrl = kMockPhotoUrl;
365398
userUpdateInfo.displayName = kMockDisplayName;
366399

367-
await auth.updateProfile(userUpdateInfo);
400+
final FirebaseUser user = await auth.currentUser();
401+
await user.updateProfile(userUpdateInfo);
368402
expect(log, <Matcher>[
403+
isMethodCall(
404+
'currentUser',
405+
arguments: null,
406+
),
369407
isMethodCall(
370408
'updateProfile',
371409
arguments: <String, String>{
@@ -376,20 +414,6 @@ void main() {
376414
]);
377415
});
378416

379-
test('updateEmail', () async {
380-
final String updatedEmail = 'atestemail@gmail.com';
381-
auth.updateEmail(email: updatedEmail);
382-
expect(
383-
log,
384-
<Matcher>[
385-
isMethodCall(
386-
'updateEmail',
387-
arguments: <String, String>{'email': updatedEmail},
388-
),
389-
],
390-
);
391-
});
392-
393417
test('signInWithCustomToken', () async {
394418
final FirebaseUser user =
395419
await auth.signInWithCustomToken(token: kMockCustomToken);

0 commit comments

Comments
 (0)