deleteUserAccount method

Future<AuthStateModel> deleteUserAccount()

Implementation

Future<AuthStateModel> deleteUserAccount() async {
  if (state.user == null) {
    return AuthStateModel(
      state: AuthState.error,
      errorMessage: AuthException.unknownCode,
    );
  }

  var currentState = AuthStateModel(
    state: state.state,
    user: state.user,
    errorMessage: state.errorMessage,
  );

  currentState = currentState.copyWith(state: AuthState.updating);
  state = currentState;

  try {
    await _auth.deleteAccount();

    return AuthStateModel(
      state: AuthState.unauthenticated,
      errorMessage: null,
    );
  } on RecentLoginRequiredException catch (e) {
    error('Account deletion error: ${e.message}', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } on UserNotFoundException catch (e) {
    error('Account deletion error: ${e.message}', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } on AuthException catch (e) {
    error('Account deletion error: ${e.message}', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } catch (e) {
    error('Account deletion error: ${e.toString()}', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: AuthException.unknownCode,
    );
    state = currentState;
  }

  return state;
}