rocket_auth icon indicating copy to clipboard operation
rocket_auth copied to clipboard

Allow option to use username instead of email

Open yzernik opened this issue 2 years ago • 5 comments

Would be nice to have the option to have users defined by username instead of email.

yzernik avatar Jun 20 '22 01:06 yzernik

@tvallotton, if someone were to create a Pull Request for this feature, would you be open to adding it? One idea I also had was to put the existing email and related validation behind a compile-time flag. Interested to know what you think.

MikeCain avatar Jul 05 '22 00:07 MikeCain

@MikeCain I would indeed be interested in seeing this feature under a compile time flag. However, I probably won't be implementing it myself, since I don't consider it a priority at the moment. That is because any lacking field in the User struct can be sidestepped by creating a "profile" table in the database with a foreign key to the user table. Answering your question, I am open to accepting a PR with this feature.

tvallotton avatar Jul 05 '22 00:07 tvallotton

I created a fork that uses username instead of email. I didn't do any compile-time flag or anything like that. Just swapped out the email validation with username validation. I still use the "email" column name internally for now.

This is the diff from master: https://github.com/tvallotton/rocket_auth/compare/master...yzernik:rocket_auth:master

I can publish the crate if anyone wants to use it.

yzernik avatar Jul 08 '22 08:07 yzernik

Hi @yzernik. I took a look at your branch, and I'm a little confused about the username validation part. It looked like it still uses an email regex. Would you mind going over how the validation works?

MikeCain avatar Jul 15 '22 20:07 MikeCain

@MikeCain I copied the regex pattern from the username section of the email validator here: https://github.com/Keats/validator/blob/a001213e42055b2aa8b034cd3f34121c208c3b2f/validator/src/validation/email.rs#L12-L17

In the email validator, they use EMAIL_USER_RE and EMAIL_DOMAIN_RE as regex patterns. I copied only the EMAIL_USER_RE pattern, since I am only interested in matching a valid username.

I could rename the regex to something like USERNAME_RE to make it more clear.

yzernik avatar Jul 15 '22 22:07 yzernik