Skip to content

Commit bc9578a

Browse files
jarrodsinclaircollinjackson
authored andcommitted
Added password reset function to Firebase Auth object (flutter#378)
* Added password reset function to Firebase Auth object * Update CHANGELOG, pubspec.yaml, and add a unit test
1 parent c4cc677 commit bc9578a

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

packages/firebase_auth/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.4
2+
3+
* Added support for sendPasswordResetEmail
4+
15
## 0.4.3
26

37
* Moved to the io.flutter.plugins organization.

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public void onMethodCall(MethodCall call, Result result) {
5858
case "createUserWithEmailAndPassword":
5959
handleCreateUserWithEmailAndPassword(call, result);
6060
break;
61+
case "sendPasswordResetEmail":
62+
handleSendPasswordResetEmail(call, result);
63+
break;
6164
case "signInWithEmailAndPassword":
6265
handleSignInWithEmailAndPassword(call, result);
6366
break;
@@ -140,6 +143,16 @@ private void handleCreateUserWithEmailAndPassword(MethodCall call, final Result
140143
.addOnCompleteListener(new SignInCompleteListener(result));
141144
}
142145

146+
private void handleSendPasswordResetEmail(MethodCall call, final Result result) {
147+
@SuppressWarnings("unchecked")
148+
Map<String, String> arguments = (Map<String, String>) call.arguments;
149+
String email = arguments.get("email");
150+
151+
firebaseAuth
152+
.sendPasswordResetEmail(email)
153+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
154+
}
155+
143156
private void handleSignInWithEmailAndPassword(MethodCall call, final Result result) {
144157
@SuppressWarnings("unchecked")
145158
Map<String, String> arguments = (Map<String, String>) call.arguments;
@@ -305,6 +318,24 @@ public void onComplete(@NonNull Task<AuthResult> task) {
305318
}
306319
}
307320

321+
private class TaskVoidCompleteListener implements OnCompleteListener<Void> {
322+
private final Result result;
323+
324+
TaskVoidCompleteListener(Result result) {
325+
this.result = result;
326+
}
327+
328+
@Override
329+
public void onComplete(@NonNull Task<Void> task) {
330+
if (!task.isSuccessful()) {
331+
Exception e = task.getException();
332+
result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null);
333+
} else {
334+
result.success(null);
335+
}
336+
}
337+
}
338+
308339
private ImmutableMap.Builder<String, Object> userInfoToMap(UserInfo userInfo) {
309340
ImmutableMap.Builder<String, Object> builder =
310341
ImmutableMap.<String, Object>builder()

packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
9393
completion:^(FIRUser *user, NSError *error) {
9494
[self sendResult:result forUser:user error:error];
9595
}];
96+
} else if ([@"sendPasswordResetEmail" isEqualToString:call.method]) {
97+
NSString *email = call.arguments[@"email"];
98+
[[FIRAuth auth] sendPasswordResetWithEmail:email
99+
completion:^(NSError *error) {
100+
[self sendResult:result forUser:nil error:error];
101+
}];
96102
} else if ([@"signInWithEmailAndPassword" isEqualToString:call.method]) {
97103
NSString *email = call.arguments[@"email"];
98104
NSString *password = call.arguments[@"password"];

packages/firebase_auth/lib/firebase_auth.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ class FirebaseAuth {
156156
return currentUser;
157157
}
158158

159+
Future<Null> sendPasswordResetEmail({
160+
@required String email,
161+
}) async {
162+
assert(email != null);
163+
return await channel.invokeMethod(
164+
'sendPasswordResetEmail',
165+
<String, String>{
166+
'email': email,
167+
},
168+
);
169+
}
170+
159171
Future<FirebaseUser> signInWithEmailAndPassword({
160172
@required String email,
161173
@required String password,

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
and Twitter.
55
author: Flutter Team <flutter-dev@googlegroups.com>
66
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_auth
7-
version: 0.4.3
7+
version: 0.4.4
88

99
flutter:
1010
plugin:

packages/firebase_auth/test/firebase_auth_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void main() {
3636
case "startListeningAuthState":
3737
return mockHandleId++;
3838
break;
39+
case "sendPasswordResetEmail":
3940
case "updateProfile":
4041
return null;
4142
break;
@@ -208,6 +209,23 @@ void main() {
208209
);
209210
});
210211

212+
test('sendPasswordResetEmail', () async {
213+
await auth.sendPasswordResetEmail(
214+
email: kMockEmail,
215+
);
216+
expect(
217+
log,
218+
<Matcher>[
219+
isMethodCall(
220+
'sendPasswordResetEmail',
221+
arguments: <String, String>{
222+
'email': kMockEmail,
223+
},
224+
),
225+
],
226+
);
227+
});
228+
211229
test('updateProfile', () async {
212230
final UserUpdateInfo userUpdateInfo = new UserUpdateInfo();
213231
userUpdateInfo.photoUrl = kMockPhotoUrl;

0 commit comments

Comments
 (0)