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();
session
.auth_with_credentials(Uuid::from_str(&userid).unwrap(), "test".to_string())
.auth_with_credentials(Uuid::from_str(&userid).unwrap(), "test")
.await
.unwrap()
}

View File

@ -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::*;

View File

@ -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::<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(())
}
}