addUserEmail method

  1. @override
Future<void> addUserEmail(
  1. UserEmail email, {
  2. ConfirmationCode? confirmation,
  3. String? locale,
})
override

Adds a new email address for the authenticated MyUser.

Sets the given email address as an MyUserEmails.unconfirmed sub-field of a MyUser.emails field and sends to this address an email message with a ConfirmationCode.

Implementation

@override
Future<void> addUserEmail(
  UserEmail email, {
  ConfirmationCode? confirmation,
  String? locale,
}) async {
  Log.debug(
    'addUserEmail($email, confirmation: $confirmation, locale: $locale)',
    '$runtimeType',
  );

  // TODO: Add optimism.
  final events = await _graphQlProvider.addUserEmail(
    email,
    confirmation: confirmation,
    locale: locale,
  );

  for (var e in events?.events ?? []) {
    final event = _myUserEvent(e);

    if (event is EventUserEmailAdded) {
      if (event.confirmed) {
        myUser.value?.emails.confirmed.addIf(
          myUser.value?.emails.confirmed.contains(email) == false,
          email,
        );
        if (myUser.value?.emails.unconfirmed == email) {
          myUser.value?.emails.unconfirmed = null;
        }
      } else {
        myUser.value?.emails.unconfirmed = email;
      }

      myUser.refresh();
    }
  }
}