Saving code
Rust Build / Check (pull_request) Failing after 1m30s
Rust Build / Test Suite (pull_request) Failing after 1m33s
Rust Build / Rustfmt (pull_request) Failing after 33s
Rust Build / Clippy (pull_request) Failing after 1m17s
Rust Build / build (pull_request) Failing after 1m41s
Rust Build / Check (pull_request) Failing after 1m30s
Rust Build / Test Suite (pull_request) Failing after 1m33s
Rust Build / Rustfmt (pull_request) Failing after 33s
Rust Build / Clippy (pull_request) Failing after 1m17s
Rust Build / build (pull_request) Failing after 1m41s
This commit is contained in:
@@ -0,0 +1,128 @@
|
|||||||
|
use crate::repo;
|
||||||
|
use crate::hashing;
|
||||||
|
use crate::token_stuff;
|
||||||
|
|
||||||
|
pub mod request {
|
||||||
|
use serde::{Deserialize};
|
||||||
|
#[derive(Default, Deserialize, utoipa::ToSchema)]
|
||||||
|
pub struct LoginRequest {
|
||||||
|
pub username: String,
|
||||||
|
pub password: String,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod response {
|
||||||
|
use serde::{Deserialize};
|
||||||
|
#[derive(Default, Deserialize, utoipa::ToSchema)]
|
||||||
|
pub struct LoginResponse {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<textsender_models::token::LoginResult>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Endpoint for a user login
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::endpoints::LOGIN,
|
||||||
|
request_body(
|
||||||
|
content = request::LoginRequest,
|
||||||
|
description = "Data required for a user to lgoin",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "User login successful", body = response::LoginResponse),
|
||||||
|
(status = 400, description = "Bad data", body = response::LoginResponse),
|
||||||
|
(status = 500, description = "Something went wrong", body = response::LoginResponse)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn login(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() {
|
||||||
|
let reason = if payload.username.is_empty() {
|
||||||
|
String::from("Username not provided")
|
||||||
|
} else {
|
||||||
|
String::from("Password not provided")
|
||||||
|
};
|
||||||
|
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response::LoginResponse {
|
||||||
|
message: reason,
|
||||||
|
data: Vec::new(),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
match repo::user::exists(&pool, &payload.username).await {
|
||||||
|
Ok(exists) => {
|
||||||
|
if !exists {
|
||||||
|
println!("User does not exists");
|
||||||
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
||||||
|
message: String::from("Unable to login"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
let user = match repo::user::get(&pool, &payload.username).await {
|
||||||
|
Ok(user) => {
|
||||||
|
user
|
||||||
|
}
|
||||||
|
Err(_err) => {
|
||||||
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
||||||
|
message: String::from("Unable to login"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let hashed_password = user.password.clone();
|
||||||
|
match hashing::verify_password(&payload.password, hashed_password) {
|
||||||
|
Ok(matches) => {
|
||||||
|
if matches {
|
||||||
|
// Create token
|
||||||
|
let key = textsender_models::envy::environment::get_secret_key().await.value;
|
||||||
|
let (token_literal, duration) =
|
||||||
|
token_stuff::create_token(&key, &user.id).unwrap();
|
||||||
|
|
||||||
|
if token_stuff::verify_token(&key, &token_literal) {
|
||||||
|
let current_time = time::OffsetDateTime::now_utc();
|
||||||
|
let _ = repo::user::update_last_login(&pool, &user, ¤t_time).await;
|
||||||
|
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::OK,
|
||||||
|
axum::Json(response::LoginResponse {
|
||||||
|
message: String::from("Successful"),
|
||||||
|
data: vec![textsender_models::token::LoginResult {
|
||||||
|
user_id: user.id,
|
||||||
|
access_token: token_literal,
|
||||||
|
token_type: String::from(textsender_models::token::TOKEN_TYPE),
|
||||||
|
expires_in: duration,
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
||||||
|
message: String::from("Invalid attempt"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
||||||
|
message: String::from("Invalid attempt"),
|
||||||
|
data: Vec::new(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::LoginResponse {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
pub mod common;
|
pub mod common;
|
||||||
|
pub mod login;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
|
|
||||||
pub mod endpoints {
|
pub mod endpoints {
|
||||||
pub const ROOT: &str = "/";
|
pub const ROOT: &str = "/";
|
||||||
pub const REGISTER: &str = "/api/v1/register";
|
pub const REGISTER: &str = "/api/v1/register";
|
||||||
|
/// Endpoint for a user to login
|
||||||
|
pub const LOGIN: &str = "/api/v1/login";
|
||||||
pub const DBTEST: &str = "/api/v1/test/db";
|
pub const DBTEST: &str = "/api/v1/test/db";
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-2
@@ -27,17 +27,19 @@ mod init {
|
|||||||
|
|
||||||
use super::callers;
|
use super::callers;
|
||||||
use callers::common as common_callers;
|
use callers::common as common_callers;
|
||||||
|
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 register_caller::response as register_responses;
|
||||||
|
use login_caller::response as login_responses;
|
||||||
|
|
||||||
#[derive(utoipa::OpenApi)]
|
#[derive(utoipa::OpenApi)]
|
||||||
#[openapi(
|
#[openapi(
|
||||||
paths(
|
paths(
|
||||||
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
||||||
register_caller::register_user,
|
register_caller::register_user, login_caller::login,
|
||||||
),
|
),
|
||||||
components(schemas(common_callers::response::TestResult,
|
components(schemas(common_callers::response::TestResult,
|
||||||
register_responses::Response)),
|
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")
|
||||||
)
|
)
|
||||||
@@ -108,6 +110,10 @@ mod init {
|
|||||||
callers::endpoints::REGISTER,
|
callers::endpoints::REGISTER,
|
||||||
post(callers::register::register_user),
|
post(callers::register::register_user),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
callers::endpoints::LOGIN,
|
||||||
|
post(callers::login::login),
|
||||||
|
)
|
||||||
.layer(cors::configure_cors().await)
|
.layer(cors::configure_cors().await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user