diff --git a/docs/auth/federated-auth.md b/docs/auth/federated-auth.md index 7d5a22367e7b..a999afda070a 100644 --- a/docs/auth/federated-auth.md +++ b/docs/auth/federated-auth.md @@ -422,6 +422,9 @@ final appleProvider = AppleAuthProvider(); if (kIsWeb) { await FirebaseAuth.instance.currentUser?.reauthenticateWithPopup(appleProvider); + + // Or you can reauthenticate with a redirection + // await FirebaseAuth.instance.currentUser?.reauthenticateWithRedirect(appleProvider); } else { await FirebaseAuth.instance.currentUser?.reauthenticateWithProvider(appleProvider); } diff --git a/packages/firebase_auth/firebase_auth/example/lib/auth.dart b/packages/firebase_auth/firebase_auth/example/lib/auth.dart index 3224bd1880d2..96c47f8c4761 100644 --- a/packages/firebase_auth/firebase_auth/example/lib/auth.dart +++ b/packages/firebase_auth/firebase_auth/example/lib/auth.dart @@ -583,10 +583,6 @@ class _AuthGateState extends State { } else { await _auth.signInWithProvider(microsoftProvider); } - - await FirebaseAuth.instance.currentUser?.reauthenticateWithProvider( - microsoftProvider, - ); } } diff --git a/packages/firebase_auth/firebase_auth/lib/src/user.dart b/packages/firebase_auth/firebase_auth/lib/src/user.dart index 7a13e774f61f..ce6391269d3b 100644 --- a/packages/firebase_auth/firebase_auth/lib/src/user.dart +++ b/packages/firebase_auth/firebase_auth/lib/src/user.dart @@ -279,6 +279,79 @@ class User { ); } + /// Re-authenticates a user using a popup on Web. + /// + /// Use before operations such as [User.updatePassword] that require tokens + /// from recent sign-in attempts. + /// + /// A [FirebaseAuthException] maybe thrown with the following error code: + /// - **user-mismatch**: + /// - Thrown if the credential given does not correspond to the user. + /// - **user-not-found**: + /// - Thrown if the credential given does not correspond to any existing + /// user. + /// - **invalid-credential**: + /// - Thrown if the provider's credential is not valid. This can happen if it + /// has already expired when calling link, or if it used invalid token(s). + /// See the Firebase documentation for your provider, and make sure you + /// pass in the correct parameters to the credential method. + /// - **invalid-email**: + /// - Thrown if the email used in a [EmailAuthProvider.credential] is + /// invalid. + /// - **wrong-password**: + /// - Thrown if the password used in a [EmailAuthProvider.credential] is not + /// correct or when the user associated with the email does not have a + /// password. + /// - **invalid-verification-code**: + /// - Thrown if the credential is a [PhoneAuthProvider.credential] and the + /// verification code of the credential is not valid. + /// - **invalid-verification-id**: + /// - Thrown if the credential is a [PhoneAuthProvider.credential] and the + /// verification ID of the credential is not valid. + Future reauthenticateWithPopup( + AuthProvider provider, + ) async { + return UserCredential._( + _auth, + await _delegate.reauthenticateWithPopup(provider), + ); + } + + /// Re-authenticates a user using a redirection on Web. + /// + /// Use before operations such as [User.updatePassword] that require tokens + /// from recent sign-in attempts. + /// + /// A [FirebaseAuthException] maybe thrown with the following error code: + /// - **user-mismatch**: + /// - Thrown if the credential given does not correspond to the user. + /// - **user-not-found**: + /// - Thrown if the credential given does not correspond to any existing + /// user. + /// - **invalid-credential**: + /// - Thrown if the provider's credential is not valid. This can happen if it + /// has already expired when calling link, or if it used invalid token(s). + /// See the Firebase documentation for your provider, and make sure you + /// pass in the correct parameters to the credential method. + /// - **invalid-email**: + /// - Thrown if the email used in a [EmailAuthProvider.credential] is + /// invalid. + /// - **wrong-password**: + /// - Thrown if the password used in a [EmailAuthProvider.credential] is not + /// correct or when the user associated with the email does not have a + /// password. + /// - **invalid-verification-code**: + /// - Thrown if the credential is a [PhoneAuthProvider.credential] and the + /// verification code of the credential is not valid. + /// - **invalid-verification-id**: + /// - Thrown if the credential is a [PhoneAuthProvider.credential] and the + /// verification ID of the credential is not valid. + Future reauthenticateWithRedirect( + AuthProvider provider, + ) async { + await _delegate.reauthenticateWithRedirect(provider); + } + /// Links the user account with the given provider. /// /// A [FirebaseAuthException] maybe thrown with the following error code: diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/platform_interface/platform_interface_user.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/platform_interface/platform_interface_user.dart index e3208ebf47c8..88bda67eb3d5 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/platform_interface/platform_interface_user.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/platform_interface/platform_interface_user.dart @@ -277,6 +277,25 @@ abstract class UserPlatform extends PlatformInterface { throw UnimplementedError('reauthenticateWithPopup() is not implemented'); } + /// Renews the user’s authentication using the provided auth provider instance. + /// On mobile you should use [reauthenticateWithProvider] instead. + /// + /// A [FirebaseAuthException] maybe thrown with the following error code: + /// - **invalid-credential**: + /// - Thrown if the provider's credential is not valid. This can happen if it + /// has already expired when calling link, or if it used invalid token(s). + /// See the Firebase documentation for your provider, and make sure you + /// pass in the correct parameters to the credential method. + /// - **operation-not-allowed**: + /// - Thrown if you have not enabled the provider in the Firebase Console. Go + /// to the Firebase Console for your project, in the Auth section and the + /// Sign in Method tab and configure the provider. + Future reauthenticateWithRedirect( + AuthProvider provider, + ) { + throw UnimplementedError('reauthenticateWithRedirect() is not implemented'); + } + /// Links the user account with the given provider. /// /// A [FirebaseAuthException] maybe thrown with the following error code: diff --git a/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_user.dart b/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_user.dart index 29b5523cd4ce..f196da71992e 100644 --- a/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_user.dart +++ b/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_user.dart @@ -164,6 +164,17 @@ class UserWeb extends UserPlatform { } } + @override + Future reauthenticateWithRedirect(AuthProvider provider) async { + _assertIsSignedOut(auth); + try { + return _webUser + .reauthenticateWithRedirect(convertPlatformAuthProvider(provider)); + } catch (e) { + throw getFirebaseAuthException(e); + } + } + @override Future reload() async { _assertIsSignedOut(auth);