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();
|
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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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::*;
|
||||||
|
|
||||||
|
|
|
@ -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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue