Is there a way to authenticate users using their email only, not their username?

You don’t really need any javascript.
You can set the email and username to the email and then that will be their login + password