diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 9abe9938..0b094df2 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -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; + async fn get_password(&self, l: &Login) -> DBResult; } /// Trait to clone MCDatabase diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index ea77f779..12917797 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -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"