Skip to content

Commit

Permalink
feat(auth, web): expose reauthenticateWithRedirect and reauthenticate…
Browse files Browse the repository at this point in the history
…WithPopup (#9696)
  • Loading branch information
Lyokone committed Oct 24, 2022
1 parent dea46c4 commit 2a1f910
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/auth/federated-auth.md
Expand Up @@ -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);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/firebase_auth/firebase_auth/example/lib/auth.dart
Expand Up @@ -583,10 +583,6 @@ class _AuthGateState extends State<AuthGate> {
} else {
await _auth.signInWithProvider(microsoftProvider);
}

await FirebaseAuth.instance.currentUser?.reauthenticateWithProvider(
microsoftProvider,
);
}
}

Expand Down
73 changes: 73 additions & 0 deletions packages/firebase_auth/firebase_auth/lib/src/user.dart
Expand Up @@ -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<UserCredential> 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<void> 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:
Expand Down
Expand Up @@ -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<void> 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:
Expand Down
Expand Up @@ -164,6 +164,17 @@ class UserWeb extends UserPlatform {
}
}

@override
Future<void> reauthenticateWithRedirect(AuthProvider provider) async {
_assertIsSignedOut(auth);
try {
return _webUser
.reauthenticateWithRedirect(convertPlatformAuthProvider(provider));
} catch (e) {
throw getFirebaseAuthException(e);
}
}

@override
Future<void> reload() async {
_assertIsSignedOut(auth);
Expand Down

0 comments on commit 2a1f910

Please sign in to comment.