Code cleanup
Rust Build / Check (pull_request) Failing after 1m12s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Test Suite (pull_request) Failing after 2m1s
Rust Build / Clippy (pull_request) Failing after 1m23s
Rust Build / build (pull_request) Failing after 1m35s
Rust Build / Check (pull_request) Failing after 1m12s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Test Suite (pull_request) Failing after 2m1s
Rust Build / Clippy (pull_request) Failing after 1m23s
Rust Build / build (pull_request) Failing after 1m35s
This commit is contained in:
+53
-29
@@ -1,9 +1,9 @@
|
|||||||
use crate::repo;
|
|
||||||
use crate::hashing;
|
use crate::hashing;
|
||||||
|
use crate::repo;
|
||||||
use crate::token_stuff;
|
use crate::token_stuff;
|
||||||
|
|
||||||
pub mod request {
|
pub mod request {
|
||||||
use serde::{Deserialize};
|
use serde::Deserialize;
|
||||||
#[derive(Default, Deserialize, utoipa::ToSchema)]
|
#[derive(Default, Deserialize, utoipa::ToSchema)]
|
||||||
pub struct LoginRequest {
|
pub struct LoginRequest {
|
||||||
pub username: String,
|
pub username: String,
|
||||||
@@ -12,7 +12,7 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
use serde::{Deserialize};
|
use serde::Deserialize;
|
||||||
#[derive(Default, Deserialize, utoipa::ToSchema)]
|
#[derive(Default, Deserialize, utoipa::ToSchema)]
|
||||||
pub struct LoginResponse {
|
pub struct LoginResponse {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
@@ -35,8 +35,10 @@ pub mod response {
|
|||||||
(status = 500, description = "Something went wrong", body = response::LoginResponse)
|
(status = 500, description = "Something went wrong", body = response::LoginResponse)
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
pub async fn login(axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
pub async fn user_login(
|
||||||
axum::Json(payload): axum::Json<request::LoginRequest>) -> (axum::http::StatusCode, axum::Json<response::LoginResponse>) {
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::Json(payload): axum::Json<request::LoginRequest>,
|
||||||
|
) -> (axum::http::StatusCode, axum::Json<response::LoginResponse>) {
|
||||||
if payload.username.is_empty() || payload.password.is_empty() {
|
if payload.username.is_empty() || payload.password.is_empty() {
|
||||||
let reason = if payload.username.is_empty() {
|
let reason = if payload.username.is_empty() {
|
||||||
String::from("Username not provided")
|
String::from("Username not provided")
|
||||||
@@ -44,43 +46,55 @@ pub async fn login(axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
|||||||
String::from("Password not provided")
|
String::from("Password not provided")
|
||||||
};
|
};
|
||||||
|
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response::LoginResponse {
|
(
|
||||||
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
|
axum::Json(response::LoginResponse {
|
||||||
message: reason,
|
message: reason,
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}))
|
}),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
match repo::user::exists(&pool, &payload.username).await {
|
match repo::user::exists(&pool, &payload.username).await {
|
||||||
Ok(exists) => {
|
Ok(exists) => {
|
||||||
if !exists {
|
if !exists {
|
||||||
println!("User does not exists");
|
println!("User does not exists");
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::LoginResponse {
|
||||||
message: String::from("Unable to login"),
|
message: String::from("Unable to login"),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}));
|
}),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
let user = match repo::user::get(&pool, &payload.username).await {
|
let user = match repo::user::get(&pool, &payload.username).await {
|
||||||
Ok(user) => {
|
Ok(user) => user,
|
||||||
user
|
|
||||||
}
|
|
||||||
Err(_err) => {
|
Err(_err) => {
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
return (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::LoginResponse {
|
||||||
message: String::from("Unable to login"),
|
message: String::from("Unable to login"),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}));
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let hashed_password = user.password.clone();
|
let hashed_password = user.password.clone();
|
||||||
|
|
||||||
match hashing::verify_password(&payload.password, hashed_password) {
|
match hashing::verify_password(&payload.password, hashed_password) {
|
||||||
Ok(matches) => {
|
Ok(matches) => {
|
||||||
if matches {
|
if matches {
|
||||||
// Create token
|
// Create token
|
||||||
let key = textsender_models::envy::environment::get_secret_key().await.value;
|
let key = textsender_models::envy::environment::get_secret_key()
|
||||||
|
.await
|
||||||
|
.value;
|
||||||
let (token_literal, duration) =
|
let (token_literal, duration) =
|
||||||
token_stuff::create_token(&key, &user.id).unwrap();
|
token_stuff::create_token(&key, &user.id).unwrap();
|
||||||
|
|
||||||
if token_stuff::verify_token(&key, &token_literal) {
|
if token_stuff::verify_token(&key, &token_literal) {
|
||||||
let current_time = time::OffsetDateTime::now_utc();
|
let current_time = time::OffsetDateTime::now_utc();
|
||||||
let _ = repo::user::update_last_login(&pool, &user, ¤t_time).await;
|
let _ =
|
||||||
|
repo::user::update_last_login(&pool, &user, ¤t_time)
|
||||||
|
.await;
|
||||||
|
|
||||||
(
|
(
|
||||||
axum::http::StatusCode::OK,
|
axum::http::StatusCode::OK,
|
||||||
@@ -89,40 +103,50 @@ pub async fn login(axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
|||||||
data: vec![textsender_models::token::LoginResult {
|
data: vec![textsender_models::token::LoginResult {
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
access_token: token_literal,
|
access_token: token_literal,
|
||||||
token_type: String::from(textsender_models::token::TOKEN_TYPE),
|
token_type: String::from(
|
||||||
|
textsender_models::token::TOKEN_TYPE,
|
||||||
|
),
|
||||||
expires_in: duration,
|
expires_in: duration,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}],
|
}],
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::LoginResponse {
|
||||||
message: String::from("Invalid attempt"),
|
message: String::from("Invalid attempt"),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}));
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::LoginResponse {
|
||||||
message: String::from("Invalid attempt"),
|
message: String::from("Invalid attempt"),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}));
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => (
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response::LoginResponse {
|
||||||
message: err.to_string(),
|
message: err.to_string(),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}));
|
}),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Err(err) => (
|
||||||
Err(err) => {
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
axum::Json(response::LoginResponse {
|
||||||
message: err.to_string(),
|
message: err.to_string(),
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
}));
|
}),
|
||||||
}
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-6
@@ -29,8 +29,8 @@ mod init {
|
|||||||
use callers::common as common_callers;
|
use callers::common as common_callers;
|
||||||
use callers::login as login_caller;
|
use callers::login as login_caller;
|
||||||
use callers::register as register_caller;
|
use callers::register as register_caller;
|
||||||
use register_caller::response as register_responses;
|
|
||||||
use login_caller::response as login_responses;
|
use login_caller::response as login_responses;
|
||||||
|
use register_caller::response as register_responses;
|
||||||
|
|
||||||
#[derive(utoipa::OpenApi)]
|
#[derive(utoipa::OpenApi)]
|
||||||
#[openapi(
|
#[openapi(
|
||||||
@@ -39,7 +39,7 @@ mod init {
|
|||||||
register_caller::register_user, login_caller::login,
|
register_caller::register_user, login_caller::login,
|
||||||
),
|
),
|
||||||
components(schemas(common_callers::response::TestResult,
|
components(schemas(common_callers::response::TestResult,
|
||||||
register_responses::Response, login_responses::LoginResponse,)),
|
register_responses::Response, login_responses::LoginResponse)),
|
||||||
tags(
|
tags(
|
||||||
(name = "TextSender Auth API", description = "Auth API for TextSender API")
|
(name = "TextSender Auth API", description = "Auth API for TextSender API")
|
||||||
)
|
)
|
||||||
@@ -110,10 +110,7 @@ mod init {
|
|||||||
callers::endpoints::REGISTER,
|
callers::endpoints::REGISTER,
|
||||||
post(callers::register::register_user),
|
post(callers::register::register_user),
|
||||||
)
|
)
|
||||||
.route(
|
.route(callers::endpoints::LOGIN, post(callers::login::login))
|
||||||
callers::endpoints::LOGIN,
|
|
||||||
post(callers::login::login),
|
|
||||||
)
|
|
||||||
.layer(cors::configure_cors().await)
|
.layer(cors::configure_cors().await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user