reauthenticateWithCredential method

Future<UserCredential> reauthenticateWithCredential(
  1. AuthCredential credential
)

Re-authenticates a user using a fresh credential.

Use before operations such as User.updatePassword that require tokens from recent sign-in attempts.

A FirebaseAuthException maybe thrown with the following error code:

Implementation

Future<UserCredential> reauthenticateWithCredential(
    AuthCredential credential) async {
  _assertSignedOut(_auth);

  try {
    if (credential is EmailAuthCredential) {
      if (credential.email != email) {
        throw FirebaseAuthException(AuthErrorCode.USER_MISMATCH);
      }

      final response = await _auth._api.emailAndPasswordAuth
          .signInWithEmailAndPassword(credential.email, credential.password!);

      _setIdToken(response.idToken);
      await reload();

      return UserCredential._(
        auth: _auth,
        credential: credential,
        additionalUserInfo: AdditionalUserInfo(
          isNewUser: false,
          profile: {
            'displayName': response.displayName,
            'photoURL': response.photoUrl
          },
          providerId: credential.providerId,
        ),
      );
    } else if (credential is GoogleAuthCredential) {
      assert(_auth.app.options.authDomain != null,
          'You should provide authDomain when trying to add Google as auth provider.');

      final response = await _auth._api.idpAuth.signInWithOAuthCredential(
        providerId: credential.providerId,
        providerIdToken: credential.idToken,
        providerAccessToken: credential.accessToken,
        requestUri: _auth.app.options.authDomain,
      );

      _setIdToken(response.idToken);
      await reload();

      return UserCredential._(
        auth: _auth,
        credential: credential,
        additionalUserInfo: AdditionalUserInfo(
          isNewUser: response.isNewUser ?? false,
          profile: {
            'displayName': response.displayName,
            'photoURL': response.photoUrl
          },
          providerId: response.providerId,
          username: response.screenName,
        ),
      );
    } else if (credential is PhoneAuthCredential) {
      final response = await _auth._api.smsAuth.confirmPhoneNumber(
        phoneNumber: phoneNumber,
        smsCode: credential.smsCode,
        idToken: _idToken,
        verificationId: credential.verificationId,
      );

      _setIdToken(response.idToken);
      await reload();

      return UserCredential._(
        auth: _auth,
        credential: credential,
        additionalUserInfo: AdditionalUserInfo(
          isNewUser: response.isNewUser,
          providerId: credential.providerId,
        ),
      );
    } else {
      throw UnsupportedError('${credential.providerId} is not supported.');
    }
  } catch (e) {
    rethrow;
  }
}