Skip to content

Commit ccf3cd3

Browse files
ko2iccollinjackson
authored andcommitted
[firebase_auth] Adding support for reauthenticate. (flutter#907)
* [firebase_auth] Adding support for reauthenticate. * Update pubspec.yaml
1 parent 3f8abcf commit ccf3cd3

File tree

4 files changed

+278
-1
lines changed

4 files changed

+278
-1
lines changed

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ public void onMethodCall(MethodCall call, Result result) {
110110
case "getIdToken":
111111
handleGetToken(call, result, getAuth(call));
112112
break;
113+
case "reauthenticateWithEmailAndPassword":
114+
handleReauthenticateWithEmailAndPassword(call, result, getAuth(call));
115+
break;
116+
case "reauthenticateWithGoogleCredential":
117+
handleReauthenticateWithGoogleCredential(call, result, getAuth(call));
118+
break;
119+
case "reauthenticateWithFacebookCredential":
120+
handleReauthenticateWithFacebookCredential(call, result, getAuth(call));
121+
break;
122+
case "reauthenticateWithTwitterCredential":
123+
handleReauthenticateWithTwitterCredential(call, result, getAuth(call));
124+
break;
125+
case "reauthenticateWithGithubCredential":
126+
handleReauthenticateWithGithubCredential(call, result, getAuth(call));
127+
break;
113128
case "linkWithEmailAndPassword":
114129
handleLinkWithEmailAndPassword(call, result, getAuth(call));
115130
break;
@@ -374,6 +389,68 @@ private void handleSignInWithGoogle(
374389
.addOnCompleteListener(new SignInCompleteListener(result));
375390
}
376391

392+
private void handleReauthenticateWithEmailAndPassword(
393+
MethodCall call, Result result, FirebaseAuth firebaseAuth) {
394+
@SuppressWarnings("unchecked")
395+
Map<String, String> arguments = (Map<String, String>) call.arguments;
396+
String email = arguments.get("email");
397+
String password = arguments.get("password");
398+
399+
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
400+
firebaseAuth
401+
.getCurrentUser()
402+
.reauthenticate(credential)
403+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
404+
}
405+
406+
private void handleReauthenticateWithGoogleCredential(
407+
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
408+
@SuppressWarnings("unchecked")
409+
Map<String, String> arguments = (Map<String, String>) call.arguments;
410+
String idToken = arguments.get("idToken");
411+
String accessToken = arguments.get("accessToken");
412+
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, accessToken);
413+
firebaseAuth
414+
.getCurrentUser()
415+
.reauthenticate(credential)
416+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
417+
}
418+
419+
private void handleReauthenticateWithFacebookCredential(
420+
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
421+
@SuppressWarnings("unchecked")
422+
Map<String, String> arguments = (Map<String, String>) call.arguments;
423+
String accessToken = arguments.get("accessToken");
424+
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken);
425+
firebaseAuth
426+
.getCurrentUser()
427+
.reauthenticate(credential)
428+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
429+
}
430+
431+
private void handleReauthenticateWithTwitterCredential(
432+
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
433+
@SuppressWarnings("unchecked")
434+
Map<String, String> arguments = (Map<String, String>) call.arguments;
435+
String authToken = arguments.get("authToken");
436+
String authTokenSecret = arguments.get("authTokenSecret");
437+
AuthCredential credential = TwitterAuthProvider.getCredential(authToken, authTokenSecret);
438+
firebaseAuth
439+
.getCurrentUser()
440+
.reauthenticate(credential)
441+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
442+
}
443+
444+
private void handleReauthenticateWithGithubCredential(
445+
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
446+
String token = call.argument("token");
447+
AuthCredential credential = GithubAuthProvider.getCredential(token);
448+
firebaseAuth
449+
.getCurrentUser()
450+
.reauthenticate(credential)
451+
.addOnCompleteListener(new TaskVoidCompleteListener(result));
452+
}
453+
377454
private void handleLinkWithGoogleCredential(
378455
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
379456
@SuppressWarnings("unchecked")

packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,52 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
169169
completion:^(NSString *_Nullable token, NSError *_Nullable error) {
170170
result(error != nil ? error.flutterError : token);
171171
}];
172+
} else if ([@"reauthenticateWithEmailAndPassword" isEqualToString:call.method]) {
173+
NSString *email = call.arguments[@"email"];
174+
NSString *password = call.arguments[@"password"];
175+
FIRAuthCredential *credential =
176+
[FIREmailAuthProvider credentialWithEmail:email password:password];
177+
[[self getAuth:call.arguments].currentUser
178+
reauthenticateWithCredential:credential
179+
completion:^(NSError *_Nullable error) {
180+
[self sendResult:result forUser:nil error:error];
181+
}];
182+
} else if ([@"reauthenticateWithGoogleCredential" isEqualToString:call.method]) {
183+
NSString *idToken = call.arguments[@"idToken"];
184+
NSString *accessToken = call.arguments[@"accessToken"];
185+
FIRAuthCredential *credential =
186+
[FIRGoogleAuthProvider credentialWithIDToken:idToken accessToken:accessToken];
187+
[[self getAuth:call.arguments].currentUser
188+
reauthenticateWithCredential:credential
189+
completion:^(NSError *_Nullable error) {
190+
[self sendResult:result forUser:nil error:error];
191+
}];
192+
} else if ([@"reauthenticateWithFacebookCredential" isEqualToString:call.method]) {
193+
NSString *accessToken = call.arguments[@"accessToken"];
194+
FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:accessToken];
195+
[[self getAuth:call.arguments].currentUser
196+
reauthenticateWithCredential:credential
197+
completion:^(NSError *_Nullable error) {
198+
[self sendResult:result forUser:nil error:error];
199+
}];
200+
} else if ([@"reauthenticateWithTwitterCredential" isEqualToString:call.method]) {
201+
NSString *authToken = call.arguments[@"authToken"];
202+
NSString *authTokenSecret = call.arguments[@"authTokenSecret"];
203+
FIRAuthCredential *credential =
204+
[FIRTwitterAuthProvider credentialWithToken:authToken secret:authTokenSecret];
205+
[[self getAuth:call.arguments].currentUser
206+
reauthenticateWithCredential:credential
207+
completion:^(NSError *_Nullable error) {
208+
[self sendResult:result forUser:nil error:error];
209+
}];
210+
} else if ([@"reauthenticateWithGithubCredential" isEqualToString:call.method]) {
211+
NSString *token = call.arguments[@"token"];
212+
FIRAuthCredential *credential = [FIRGitHubAuthProvider credentialWithToken:token];
213+
[[self getAuth:call.arguments].currentUser
214+
reauthenticateWithCredential:credential
215+
completion:^(NSError *_Nullable error) {
216+
[self sendResult:result forUser:nil error:error];
217+
}];
172218
} else if ([@"linkWithEmailAndPassword" isEqualToString:call.method]) {
173219
NSString *email = call.arguments[@"email"];
174220
NSString *password = call.arguments[@"password"];

packages/firebase_auth/lib/firebase_auth.dart

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import 'dart:async';
66

7+
import 'package:firebase_core/firebase_core.dart';
78
import 'package:flutter/services.dart';
89
import 'package:meta/meta.dart';
9-
import 'package:firebase_core/firebase_core.dart';
1010

1111
/// Represents user data returned from an identity provider.
1212
@@ -16,6 +16,7 @@ class FirebaseUserMetadata {
1616
final Map<dynamic, dynamic> _data;
1717

1818
int get creationTimestamp => _data['creationTimestamp'];
19+
1920
int get lastSignInTimestamp => _data['lastSignInTimestamp'];
2021
}
2122

@@ -490,6 +491,64 @@ class FirebaseAuth {
490491
return currentUser;
491492
}
492493

494+
Future<void> reauthenticateWithEmailAndPassword({
495+
@required String email,
496+
@required String password,
497+
}) {
498+
assert(email != null);
499+
assert(password != null);
500+
return channel.invokeMethod(
501+
'reauthenticateWithEmailAndPassword',
502+
<String, String>{'email': email, 'password': password, 'app': app.name},
503+
);
504+
}
505+
506+
Future<void> reauthenticateWithGoogleCredential({
507+
@required String idToken,
508+
@required String accessToken,
509+
}) {
510+
assert(idToken != null);
511+
assert(accessToken != null);
512+
return channel.invokeMethod(
513+
'reauthenticateWithGoogleCredential',
514+
<String, String>{
515+
'idToken': idToken,
516+
'accessToken': accessToken,
517+
'app': app.name
518+
},
519+
);
520+
}
521+
522+
Future<void> reauthenticateWithFacebookCredential({
523+
@required String accessToken,
524+
}) {
525+
assert(accessToken != null);
526+
return channel.invokeMethod(
527+
'reauthenticateWithFacebookCredential',
528+
<String, String>{'accessToken': accessToken, 'app': app.name},
529+
);
530+
}
531+
532+
Future<void> reauthenticateWithTwitterCredential({
533+
@required String authToken,
534+
@required String authTokenSecret,
535+
}) {
536+
return channel.invokeMethod(
537+
'reauthenticateWithTwitterCredential',
538+
<String, String>{
539+
'app': app.name,
540+
'authToken': authToken,
541+
'authTokenSecret': authTokenSecret,
542+
},
543+
);
544+
}
545+
546+
Future<void> reauthenticateWithGithubCredential({@required String token}) {
547+
assert(token != null);
548+
return channel.invokeMethod('reauthenticateWithGithubCredential',
549+
<String, String>{'app': app.name, 'token': token});
550+
}
551+
493552
/// Sets the user-facing language code for auth operations that can be
494553
/// internationalized, such as [sendEmailVerification]. This language
495554
/// code should follow the conventions defined by the IETF in BCP47.

packages/firebase_auth/test/firebase_auth_test.dart

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,101 @@ void main() {
227227
]);
228228
});
229229

230+
test('reauthenticateWithEmailAndPassword', () async {
231+
await auth.reauthenticateWithEmailAndPassword(
232+
email: kMockEmail,
233+
password: kMockPassword,
234+
);
235+
expect(
236+
log,
237+
<Matcher>[
238+
isMethodCall(
239+
'reauthenticateWithEmailAndPassword',
240+
arguments: <String, String>{
241+
'email': kMockEmail,
242+
'password': kMockPassword,
243+
'app': auth.app.name,
244+
},
245+
),
246+
],
247+
);
248+
});
249+
test('reauthenticateWithGoogleCredential', () async {
250+
await auth.reauthenticateWithGoogleCredential(
251+
idToken: kMockIdToken,
252+
accessToken: kMockAccessToken,
253+
);
254+
expect(
255+
log,
256+
<Matcher>[
257+
isMethodCall(
258+
'reauthenticateWithGoogleCredential',
259+
arguments: <String, String>{
260+
'idToken': kMockIdToken,
261+
'accessToken': kMockAccessToken,
262+
'app': auth.app.name,
263+
},
264+
),
265+
],
266+
);
267+
});
268+
269+
test('reauthenticateWithFacebookCredential', () async {
270+
await auth.reauthenticateWithFacebookCredential(
271+
accessToken: kMockAccessToken,
272+
);
273+
expect(
274+
log,
275+
<Matcher>[
276+
isMethodCall(
277+
'reauthenticateWithFacebookCredential',
278+
arguments: <String, String>{
279+
'accessToken': kMockAccessToken,
280+
'app': auth.app.name,
281+
},
282+
),
283+
],
284+
);
285+
});
286+
287+
test('reauthenticateWithTwitterCredential', () async {
288+
await auth.reauthenticateWithTwitterCredential(
289+
authToken: kMockAuthToken,
290+
authTokenSecret: kMockAuthTokenSecret,
291+
);
292+
expect(
293+
log,
294+
<Matcher>[
295+
isMethodCall(
296+
'reauthenticateWithTwitterCredential',
297+
arguments: <String, String>{
298+
'authToken': kMockAuthToken,
299+
'authTokenSecret': kMockAuthTokenSecret,
300+
'app': auth.app.name,
301+
},
302+
),
303+
],
304+
);
305+
});
306+
307+
test('reauthenticateWithGithubCredential', () async {
308+
await auth.reauthenticateWithGithubCredential(
309+
token: kMockGithubToken,
310+
);
311+
expect(
312+
log,
313+
<Matcher>[
314+
isMethodCall(
315+
'reauthenticateWithGithubCredential',
316+
arguments: <String, String>{
317+
'app': auth.app.name,
318+
'token': kMockGithubToken,
319+
},
320+
),
321+
],
322+
);
323+
});
324+
230325
test('linkWithGoogleCredential', () async {
231326
final FirebaseUser user = await auth.linkWithGoogleCredential(
232327
idToken: kMockIdToken,

0 commit comments

Comments
 (0)