feat: get password using either username or email

This commit is contained in:
realaravinth 2022-05-11 18:26:35 +05:30
parent fdf4f0bef9
commit 7e2be86c12
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
2 changed files with 20 additions and 2 deletions

View file

@ -80,6 +80,15 @@ pub struct UpdateEmail<'a> {
pub new_email: &'a str,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
/// types of credentials used as identifiers during login
pub enum Login<'a> {
/// username as login
Username(&'a str),
/// email as login
Email(&'a str),
}
#[async_trait]
/// mCaptcha's database requirements. To implement support for $Database, kindly implement this
/// trait.
@ -103,7 +112,7 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
async fn update_email(&self, p: &UpdateEmail) -> DBResult<()>;
/// get a user's password
async fn get_password(&self, username: &str) -> DBResult<String>;
async fn get_password(&self, l: &Login) -> DBResult<String>;
}
/// Trait to clone MCDatabase

View file

@ -30,10 +30,19 @@ pub async fn database_works<'a, T: MCDatabase>(db: &T, p: &Register<'a>) {
db.register(p).await.unwrap();
assert_eq!(
db.get_password(p.username).await.unwrap(),
db.get_password(&Login::Username(p.username)).await.unwrap(),
p.hash,
"user password matches"
);
assert_eq!(
db.get_password(&Login::Email(p.email.as_ref().unwrap()))
.await
.unwrap(),
p.hash,
"user password matches"
);
assert!(
db.email_exists(p.email.as_ref().unwrap()).await.unwrap(),
"user is registered so email should exsit"