Service login #6
+14
-10
@@ -45,7 +45,8 @@ pub mod response {
|
||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct ServiceUserLoginResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::user::ServiceUser>,
|
||||
// TODO: Change this to login result
|
||||
pub data: Vec<textsender_models::token::LoginResult>,
|
||||
}
|
||||
|
||||
pub async fn extract(
|
||||
@@ -208,7 +209,10 @@ pub async fn user_login(
|
||||
pub async fn service_user_login(
|
||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<request::ServiceUserLoginRequest>,
|
||||
) -> (axum::http::StatusCode, axum::Json<response::LoginResponse>) {
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<response::ServiceUserLoginResponse>,
|
||||
) {
|
||||
if payload.username.is_empty() || payload.passphrase.is_empty() {
|
||||
let reason = if payload.username.is_empty() {
|
||||
String::from("Username not provided")
|
||||
@@ -218,7 +222,7 @@ pub async fn service_user_login(
|
||||
|
||||
(
|
||||
axum::http::StatusCode::BAD_REQUEST,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: reason,
|
||||
data: Vec::new(),
|
||||
}),
|
||||
@@ -230,7 +234,7 @@ pub async fn service_user_login(
|
||||
println!("User does not exists");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: String::from("Unable to login"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
@@ -244,7 +248,7 @@ pub async fn service_user_login(
|
||||
eprintln!("Error: {err:?}");
|
||||
return (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: String::from("Unable to login"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
@@ -278,7 +282,7 @@ pub async fn service_user_login(
|
||||
|
||||
(
|
||||
axum::http::StatusCode::OK,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: String::from(
|
||||
super::messages::SUCCESSFUL_MESSAGE,
|
||||
),
|
||||
@@ -297,7 +301,7 @@ pub async fn service_user_login(
|
||||
eprintln!("Invalid token");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: String::from("Invalid attempt"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
@@ -307,7 +311,7 @@ pub async fn service_user_login(
|
||||
eprintln!("No match");
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: String::from("Invalid attempt"),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
@@ -316,7 +320,7 @@ pub async fn service_user_login(
|
||||
}
|
||||
Err(err) => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: err.to_string(),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
@@ -326,7 +330,7 @@ pub async fn service_user_login(
|
||||
}
|
||||
Err(err) => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response::LoginResponse {
|
||||
axum::Json(response::ServiceUserLoginResponse {
|
||||
message: err.to_string(),
|
||||
data: Vec::new(),
|
||||
}),
|
||||
|
||||
+36
-13
@@ -221,13 +221,16 @@ async fn login_user(
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||
Ok(body) => {
|
||||
let parsed_body: callers::login::response::LoginResponse =
|
||||
serde_json::from_slice(&body).unwrap();
|
||||
Ok(parsed_body.data[0].clone())
|
||||
match convert_response::<callers::login::response::LoginResponse>(response).await {
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let user = response.data[0].clone();
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,8 +249,19 @@ async fn register_service_user(
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<textsender_models::user::ServiceUser>(response).await {
|
||||
Ok(service_user) => Ok(service_user),
|
||||
match convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let service_user = response.data[0].clone();
|
||||
Ok(service_user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
@@ -256,7 +270,7 @@ async fn register_service_user(
|
||||
}
|
||||
}
|
||||
|
||||
async fn _login_service_user(
|
||||
async fn login_service_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
passphrase: &str,
|
||||
@@ -269,8 +283,19 @@ async fn _login_service_user(
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<textsender_models::token::LoginResult>(response).await {
|
||||
Ok(service_user) => Ok(service_user),
|
||||
match convert_response::<callers::login::response::ServiceUserLoginResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let login_result = response.data[0].clone();
|
||||
Ok(login_result)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
@@ -445,7 +470,6 @@ async fn test_login_service_user() {
|
||||
user.id.is_nil(),
|
||||
"The service user id should not be nil"
|
||||
);
|
||||
/*
|
||||
match login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE).await {
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
@@ -458,7 +482,6 @@ async fn test_login_service_user() {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
|
||||
Reference in New Issue
Block a user