diff --git a/client/src/lib.rs b/client/src/lib.rs index a0d7160..65db2ff 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -28,7 +28,7 @@ mod tests { let mut session = Session::new(&host, None).await.unwrap(); session - .auth_with_credentials(Uuid::from_str(&userid).unwrap(), "test".to_string()) + .auth_with_credentials(Uuid::from_str(&userid).unwrap(), "test") .await .unwrap() } diff --git a/client/src/session/data.rs b/client/src/session/data.rs index ca7a3c8..e01c007 100644 --- a/client/src/session/data.rs +++ b/client/src/session/data.rs @@ -1,4 +1,4 @@ -use serde::Deserialize; +use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize)] pub enum Permission { @@ -14,12 +14,11 @@ pub mod api { pub mod auth { use super::*; - use serde::Serialize; #[derive(Debug, Serialize)] - pub struct Request { - pub userid: String, - pub password: String, + pub struct Request<'a> { + pub userid: &'a str, + pub password: &'a str, } #[derive(Debug, Deserialize)] @@ -28,6 +27,21 @@ pub mod api { } } + pub mod register { + use super::*; + + #[derive(Debug, Serialize)] + pub struct Request<'a> { + pub token: &'a str, + pub password: &'a str, + } + + #[derive(Debug, Deserialize)] + pub struct Response { + pub userid: String, + } + } + pub mod info { use super::*; diff --git a/client/src/session/mod.rs b/client/src/session/mod.rs index 5adf1b0..92e1814 100644 --- a/client/src/session/mod.rs +++ b/client/src/session/mod.rs @@ -103,13 +103,13 @@ impl Session { pub async fn auth_with_credentials( &mut self, userid: Uuid, - password: String, + password: &str, ) -> Result<(), Error> { let request = self .client .post(format!("{host}/account/auth", host = self.host)) .json(&data::api::account::auth::Request { - userid: userid.to_string(), + userid: &userid.to_string(), password, }) .send(); @@ -123,4 +123,29 @@ impl Session { Ok(()) } + + /// Tries to register a new account on the server. If the server doesn't accept the invite-token, + /// this leads to an [crate::error::Error::InvalidToken] or [crate::error::Error::TokenExpired] error. + pub async fn register(&mut self, token: &str, password: &str) -> Result<(), Error> { + let request = self + .client + .post(format!("{host}/account/register", host = self.host)) + .json(&data::api::account::register::Request { + token: &token, + password: &password, + }) + .send(); + + let response = + parse_response::(request.await).await?; + + self.auth_with_credentials( + Uuid::from_str(&response.userid) + .map_err(|_| Error::BadResponse("Failed to parse userid".to_string()))?, + password, + ) + .await?; + + Ok(()) + } }