tvallotton / rocket_auth

An implementation for an authentication API for Rocket applications.
https://docs.rs/rocket_auth/
Apache License 2.0
73 stars 35 forks source link

Allow option to use username instead of email #43

Open yzernik opened 2 years ago

yzernik commented 2 years ago

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

MikeCain commented 2 years ago

@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.

tvallotton commented 2 years ago

@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.

yzernik commented 2 years ago

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.

MikeCain commented 2 years ago

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?

yzernik commented 2 years ago

@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.