updateUserProfile method

Future<AuthStateModel> updateUserProfile({
  1. String? username,
  2. String? email,
})

Implementation

Future<AuthStateModel> updateUserProfile({
  String? username,
  String? email,
}) 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,
  );

  try {
    final updatedUser = await _auth.updateUserProfile(
      username: username,
      email: email,
    );

    currentState = currentState.copyWith(user: updatedUser);

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

  return state;
}