Skip to content

Commit 2a1f910

Browse files
authored
feat(auth, web): expose reauthenticateWithRedirect and reauthenticateWithPopup (#9696)
1 parent dea46c4 commit 2a1f910

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed

docs/auth/federated-auth.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ final appleProvider = AppleAuthProvider();
422422
423423
if (kIsWeb) {
424424
await FirebaseAuth.instance.currentUser?.reauthenticateWithPopup(appleProvider);
425+
426+
// Or you can reauthenticate with a redirection
427+
// await FirebaseAuth.instance.currentUser?.reauthenticateWithRedirect(appleProvider);
425428
} else {
426429
await FirebaseAuth.instance.currentUser?.reauthenticateWithProvider(appleProvider);
427430
}

packages/firebase_auth/firebase_auth/example/lib/auth.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,6 @@ class _AuthGateState extends State<AuthGate> {
583583
} else {
584584
await _auth.signInWithProvider(microsoftProvider);
585585
}
586-
587-
await FirebaseAuth.instance.currentUser?.reauthenticateWithProvider(
588-
microsoftProvider,
589-
);
590586
}
591587
}
592588

packages/firebase_auth/firebase_auth/lib/src/user.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,79 @@ class User {
279279
);
280280
}
281281

282+
/// Re-authenticates a user using a popup on Web.
283+
///
284+
/// Use before operations such as [User.updatePassword] that require tokens
285+
/// from recent sign-in attempts.
286+
///
287+
/// A [FirebaseAuthException] maybe thrown with the following error code:
288+
/// - **user-mismatch**:
289+
/// - Thrown if the credential given does not correspond to the user.
290+
/// - **user-not-found**:
291+
/// - Thrown if the credential given does not correspond to any existing
292+
/// user.
293+
/// - **invalid-credential**:
294+
/// - Thrown if the provider's credential is not valid. This can happen if it
295+
/// has already expired when calling link, or if it used invalid token(s).
296+
/// See the Firebase documentation for your provider, and make sure you
297+
/// pass in the correct parameters to the credential method.
298+
/// - **invalid-email**:
299+
/// - Thrown if the email used in a [EmailAuthProvider.credential] is
300+
/// invalid.
301+
/// - **wrong-password**:
302+
/// - Thrown if the password used in a [EmailAuthProvider.credential] is not
303+
/// correct or when the user associated with the email does not have a
304+
/// password.
305+
/// - **invalid-verification-code**:
306+
/// - Thrown if the credential is a [PhoneAuthProvider.credential] and the
307+
/// verification code of the credential is not valid.
308+
/// - **invalid-verification-id**:
309+
/// - Thrown if the credential is a [PhoneAuthProvider.credential] and the
310+
/// verification ID of the credential is not valid.
311+
Future<UserCredential> reauthenticateWithPopup(
312+
AuthProvider provider,
313+
) async {
314+
return UserCredential._(
315+
_auth,
316+
await _delegate.reauthenticateWithPopup(provider),
317+
);
318+
}
319+
320+
/// Re-authenticates a user using a redirection on Web.
321+
///
322+
/// Use before operations such as [User.updatePassword] that require tokens
323+
/// from recent sign-in attempts.
324+
///
325+
/// A [FirebaseAuthException] maybe thrown with the following error code:
326+
/// - **user-mismatch**:
327+
/// - Thrown if the credential given does not correspond to the user.
328+
/// - **user-not-found**:
329+
/// - Thrown if the credential given does not correspond to any existing
330+
/// user.
331+
/// - **invalid-credential**:
332+
/// - Thrown if the provider's credential is not valid. This can happen if it
333+
/// has already expired when calling link, or if it used invalid token(s).
334+
/// See the Firebase documentation for your provider, and make sure you
335+
/// pass in the correct parameters to the credential method.
336+
/// - **invalid-email**:
337+
/// - Thrown if the email used in a [EmailAuthProvider.credential] is
338+
/// invalid.
339+
/// - **wrong-password**:
340+
/// - Thrown if the password used in a [EmailAuthProvider.credential] is not
341+
/// correct or when the user associated with the email does not have a
342+
/// password.
343+
/// - **invalid-verification-code**:
344+
/// - Thrown if the credential is a [PhoneAuthProvider.credential] and the
345+
/// verification code of the credential is not valid.
346+
/// - **invalid-verification-id**:
347+
/// - Thrown if the credential is a [PhoneAuthProvider.credential] and the
348+
/// verification ID of the credential is not valid.
349+
Future<void> reauthenticateWithRedirect(
350+
AuthProvider provider,
351+
) async {
352+
await _delegate.reauthenticateWithRedirect(provider);
353+
}
354+
282355
/// Links the user account with the given provider.
283356
///
284357
/// A [FirebaseAuthException] maybe thrown with the following error code:

packages/firebase_auth/firebase_auth_platform_interface/lib/src/platform_interface/platform_interface_user.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ abstract class UserPlatform extends PlatformInterface {
277277
throw UnimplementedError('reauthenticateWithPopup() is not implemented');
278278
}
279279

280+
/// Renews the user’s authentication using the provided auth provider instance.
281+
/// On mobile you should use [reauthenticateWithProvider] instead.
282+
///
283+
/// A [FirebaseAuthException] maybe thrown with the following error code:
284+
/// - **invalid-credential**:
285+
/// - Thrown if the provider's credential is not valid. This can happen if it
286+
/// has already expired when calling link, or if it used invalid token(s).
287+
/// See the Firebase documentation for your provider, and make sure you
288+
/// pass in the correct parameters to the credential method.
289+
/// - **operation-not-allowed**:
290+
/// - Thrown if you have not enabled the provider in the Firebase Console. Go
291+
/// to the Firebase Console for your project, in the Auth section and the
292+
/// Sign in Method tab and configure the provider.
293+
Future<void> reauthenticateWithRedirect(
294+
AuthProvider provider,
295+
) {
296+
throw UnimplementedError('reauthenticateWithRedirect() is not implemented');
297+
}
298+
280299
/// Links the user account with the given provider.
281300
///
282301
/// A [FirebaseAuthException] maybe thrown with the following error code:

packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_user.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ class UserWeb extends UserPlatform {
164164
}
165165
}
166166

167+
@override
168+
Future<void> reauthenticateWithRedirect(AuthProvider provider) async {
169+
_assertIsSignedOut(auth);
170+
try {
171+
return _webUser
172+
.reauthenticateWithRedirect(convertPlatformAuthProvider(provider));
173+
} catch (e) {
174+
throw getFirebaseAuthException(e);
175+
}
176+
}
177+
167178
@override
168179
Future<void> reload() async {
169180
_assertIsSignedOut(auth);

0 commit comments

Comments
 (0)