I'm assuming that this script is attached to the LineEdit, so that "text" is the LineEdit's text property.
This is untested, but should work (Godot 4.3):
func _on_username_input_text_changed(new_text: String) -> void:
# Remove non-printable characters (tabs, line feeds, etc.) and spaces.
text = new_text.strip_escapes().replace(" ", "")
Reference: https://docs.godotengine.org/en/4.3/classes/class_string.html#methods
This could also be done with a RegEx. That's my preference (because regular expressions are cool, and using them makes you feel superior to people who can't use them), but it's tricky if you're not familiar with RegEx.