feat(client >> session): implemented acount registration

This commit is contained in:
antifallobst 2024-03-31 18:12:37 +02:00
parent c88da7fddb
commit a3b0263f97
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
3 changed files with 47 additions and 8 deletions

View File

@ -28,7 +28,7 @@ mod tests {
let mut session = Session::new(&host, None).await.unwrap(); let mut session = Session::new(&host, None).await.unwrap();
session session
.auth_with_credentials(Uuid::from_str(&userid).unwrap(), "test".to_string()) .auth_with_credentials(Uuid::from_str(&userid).unwrap(), "test")
.await .await
.unwrap() .unwrap()
} }

View File

@ -1,4 +1,4 @@
use serde::Deserialize; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub enum Permission { pub enum Permission {
@ -14,12 +14,11 @@ pub mod api {
pub mod auth { pub mod auth {
use super::*; use super::*;
use serde::Serialize;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct Request { pub struct Request<'a> {
pub userid: String, pub userid: &'a str,
pub password: String, pub password: &'a str,
} }
#[derive(Debug, Deserialize)] #[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 { pub mod info {
use super::*; use super::*;

View File

@ -103,13 +103,13 @@ impl Session {
pub async fn auth_with_credentials( pub async fn auth_with_credentials(
&mut self, &mut self,
userid: Uuid, userid: Uuid,
password: String, password: &str,
) -> Result<(), Error> { ) -> Result<(), Error> {
let request = self let request = self
.client .client
.post(format!("{host}/account/auth", host = self.host)) .post(format!("{host}/account/auth", host = self.host))
.json(&data::api::account::auth::Request { .json(&data::api::account::auth::Request {
userid: userid.to_string(), userid: &userid.to_string(),
password, password,
}) })
.send(); .send();
@ -123,4 +123,29 @@ impl Session {
Ok(()) 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::<data::api::account::register::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(())
}
} }