I think you need to rethink your logic that decides to set the empty nickname to the email.
You need to do this, in this order:
- if the nickname is empty, set it to the email (and strip out the part after the ‘@’ if you want.
- then you should have values for display name (whatever they entered), and nickname (whatever they entered, or their email if no entry)
- and then you can store the values
I note that there might be problems with the resultant data, though. The email address might not have a good ‘visible’ (recognizable?) value for the nickname. There might be some values of emails that wouldn’t look good as a nickname.
So perhaps requiring a nickname might be better, or letting the nickname be blank. Perhaps asking for ‘first/last name’, email, and nickname (visible name). If the visible name is empty, then put in the first/last name. But alert the user that their first/last name will be used, so they may want to enter a nickname. (And then there is the GDPR stuff…)