feat(client >> session): implemented acount registration
This commit is contained in:
parent
c88da7fddb
commit
a3b0263f97
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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::*;
|
||||
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue