Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth, web): expose reauthenticateWithRedirect and reauthenticateWithPopup #9696

Merged
merged 1 commit into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -579,10 +579,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