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

Add updatePassword functionality to firebase_auth plugin. #678

Merged
merged 1 commit into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/firebase_auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.6.0

* Added support for `updatePassword` in `FirebaseUser`.
* **Breaking Change** Moved `updateEmail` and `updateProfile` to `FirebaseUser`.
This brings the `firebase_auth` package inline with other implementations and documentation.

## 0.5.20

* Replaced usages of guava's: ImmutableList and ImmutableMap with platform
Expand All @@ -15,7 +21,7 @@ Collections.unmodifiableList() and Collections.unmodifiableMap().

## 0.5.17

* Adding support for FirebaseUser.delete.
* Adding support for FirebaseUser.delete.

## 0.5.16

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ public void onMethodCall(MethodCall call, Result result) {
case "linkWithFacebookCredential":
handleLinkWithFacebookCredential(call, result);
break;
case "updateProfile":
handleUpdateProfile(call, result);
break;
case "updateEmail":
handleUpdateEmail(call, result);
break;
case "updatePassword":
handleUpdatePassword(call, result);
break;
case "updateProfile":
handleUpdateProfile(call, result);
break;
case "startListeningAuthState":
handleStartListeningAuthState(call, result);
break;
Expand All @@ -139,6 +142,7 @@ public void onMethodCall(MethodCall call, Result result) {
}

private void handleSignInWithPhoneNumber(MethodCall call, Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
String verificationId = arguments.get("verificationId");
String smsCode = arguments.get("smsCode");
Expand Down Expand Up @@ -424,6 +428,24 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {
});
}

private void handleUpdateEmail(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
firebaseAuth
.getCurrentUser()
.updateEmail(arguments.get("email"))
.addOnCompleteListener(new TaskVoidCompleteListener(result));
}

private void handleUpdatePassword(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
firebaseAuth
.getCurrentUser()
.updatePassword(arguments.get("password"))
.addOnCompleteListener(new TaskVoidCompleteListener(result));
}

private void handleUpdateProfile(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
Expand All @@ -439,40 +461,7 @@ private void handleUpdateProfile(MethodCall call, final Result result) {
firebaseAuth
.getCurrentUser()
.updateProfile(builder.build())
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null);
} else {
result.success(null);
}
}
});
}

private void handleUpdateEmail(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
String email = arguments.get("email");

firebaseAuth
.getCurrentUser()
.updateEmail(email)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null);
} else {
result.success(null);
}
}
});
.addOnCompleteListener(new TaskVoidCompleteListener(result));
}

private void handleStartListeningAuthState(MethodCall call, final Result result) {
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class _MyHomePageState extends State<MyHomePage> {
(AuthException authException) {
setState(() {
_message = Future<String>.value(
'Phone numbber verification failed. Code: ${authException.code}. Message: ${authException.message}');
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
});
};

Expand Down
18 changes: 12 additions & 6 deletions packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
completion:^(FIRUser *user, NSError *error) {
[self sendResult:result forUser:user error:error];
}];
} else if ([@"updateEmail" isEqualToString:call.method]) {
NSString *email = call.arguments[@"email"];
[[FIRAuth auth].currentUser updateEmail:email
completion:^(NSError *error) {
[self sendResult:result forUser:nil error:error];
}];
} else if ([@"updatePassword" isEqualToString:call.method]) {
NSString *password = call.arguments[@"password"];
[[FIRAuth auth].currentUser updatePassword:password
completion:^(NSError *error) {
[self sendResult:result forUser:nil error:error];
}];
} else if ([@"updateProfile" isEqualToString:call.method]) {
FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest];
if (call.arguments[@"displayName"]) {
Expand All @@ -187,12 +199,6 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[changeRequest commitChangesWithCompletion:^(NSError *error) {
[self sendResult:result forUser:nil error:error];
}];
} else if ([@"updateEmail" isEqualToString:call.method]) {
NSString *toEmail = call.arguments[@"email"];
[[FIRAuth auth].currentUser updateEmail:toEmail
completion:^(NSError *_Nullable error) {
[self sendResult:result forUser:nil error:error];
}];
} else if ([@"signInWithCustomToken" isEqualToString:call.method]) {
NSString *token = call.arguments[@"token"];
[[FIRAuth auth] signInWithCustomToken:token
Expand Down
47 changes: 27 additions & 20 deletions packages/firebase_auth/lib/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ class FirebaseUser extends UserInfo {
await FirebaseAuth.channel.invokeMethod('delete');
}

/// Updates the email address of the user.
Future<void> updateEmail(String email) async {
assert(email != null);
return await FirebaseAuth.channel.invokeMethod(
'updateEmail',
<String, String>{'email': email},
);
}

/// Updates the password of the user.
Future<void> updatePassword(String password) async {
assert(password != null);
return await FirebaseAuth.channel.invokeMethod(
'updatePassword',
<String, String>{'password': password},
);
}

/// Updates the user profile information.
Future<void> updateProfile(UserUpdateInfo userUpdateInfo) async {
assert(userUpdateInfo != null);
return await FirebaseAuth.channel.invokeMethod(
'updateProfile',
userUpdateInfo._updateData,
);
}

@override
String toString() {
return '$runtimeType($_data)';
Expand Down Expand Up @@ -369,26 +396,6 @@ class FirebaseAuth {
return currentUser;
}

Future<void> updateEmail({
@required String email,
}) async {
assert(email != null);
return await channel.invokeMethod(
'updateEmail',
<String, String>{
'email': email,
},
);
}

Future<void> updateProfile(UserUpdateInfo userUpdateInfo) async {
assert(userUpdateInfo != null);
return await channel.invokeMethod(
'updateProfile',
userUpdateInfo._updateData,
);
}

/// Links google account with current user and returns [Future<FirebaseUser>]
///
/// throws [PlatformException] when
Expand Down
60 changes: 42 additions & 18 deletions packages/firebase_auth/test/firebase_auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ void main() {
return mockHandleId++;
break;
case "sendPasswordResetEmail":
case "updateProfile":
return null;
break;
case "updateEmail":
case "updatePassword":
case "updateProfile":
return null;
break;
case "fetchProvidersForEmail":
Expand Down Expand Up @@ -359,13 +358,52 @@ void main() {
);
});

test('updateEmail', () async {
final FirebaseUser user = await auth.currentUser();
await user.updateEmail(kMockEmail);
expect(log, <Matcher>[
isMethodCall(
'currentUser',
arguments: null,
),
isMethodCall(
'updateEmail',
arguments: <String, String>{
'email': kMockEmail,
},
),
]);
});

test('updatePassword', () async {
final FirebaseUser user = await auth.currentUser();
await user.updatePassword(kMockPassword);
expect(log, <Matcher>[
isMethodCall(
'currentUser',
arguments: null,
),
isMethodCall(
'updatePassword',
arguments: <String, String>{
'password': kMockPassword,
},
),
]);
});

test('updateProfile', () async {
final UserUpdateInfo userUpdateInfo = new UserUpdateInfo();
userUpdateInfo.photoUrl = kMockPhotoUrl;
userUpdateInfo.displayName = kMockDisplayName;

await auth.updateProfile(userUpdateInfo);
final FirebaseUser user = await auth.currentUser();
await user.updateProfile(userUpdateInfo);
expect(log, <Matcher>[
isMethodCall(
'currentUser',
arguments: null,
),
isMethodCall(
'updateProfile',
arguments: <String, String>{
Expand All @@ -376,20 +414,6 @@ void main() {
]);
});

test('updateEmail', () async {
final String updatedEmail = 'atestemail@gmail.com';
auth.updateEmail(email: updatedEmail);
expect(
log,
<Matcher>[
isMethodCall(
'updateEmail',
arguments: <String, String>{'email': updatedEmail},
),
],
);
});

test('signInWithCustomToken', () async {
final FirebaseUser user =
await auth.signInWithCustomToken(token: kMockCustomToken);
Expand Down