pre-metrics
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[derive(Deserialize, ToSchema)]
|
||||
pub struct LoginRequest {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub struct LoginResponse {
|
||||
pub token: String,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub struct CheckResponse {
|
||||
pub authenticated: bool,
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
use super::dto::{CheckResponse, LoginRequest, LoginResponse};
|
||||
use crate::auth::token::create_jwt;
|
||||
use crate::core::AppState;
|
||||
use crate::http::context::CurrentUser;
|
||||
use crate::http::error::HTTPError;
|
||||
use axum::extract::State;
|
||||
use axum::Json;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/auth/login",
|
||||
responses(
|
||||
(status = 200, description = "Login successful", body = LoginResponse),
|
||||
(status = 401, description = "Unauthorized")
|
||||
)
|
||||
)]
|
||||
pub async fn login_user_pw(
|
||||
State(state): State<AppState>,
|
||||
Json(payload): Json<LoginRequest>,
|
||||
) -> Result<Json<LoginResponse>, HTTPError> {
|
||||
let user = state
|
||||
.repositories
|
||||
.user
|
||||
.check_password(&payload.username, &payload.password)
|
||||
.await
|
||||
.map_err(|_| HTTPError::Unauthorized)?;
|
||||
|
||||
let token = create_jwt(
|
||||
user.id,
|
||||
&user.username,
|
||||
&state.config.jwt.secret,
|
||||
state.config.jwt.duration,
|
||||
)
|
||||
.map_err(|_| HTTPError::InternalServerError("Failed to create JWT token".to_string()))?;
|
||||
|
||||
Ok(Json(LoginResponse {
|
||||
username: user.username,
|
||||
token,
|
||||
}))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/auth/check",
|
||||
responses(
|
||||
(status = 200, description = "Login successful", body = LoginResponse),
|
||||
(status = 401, description = "Unauthorized")
|
||||
)
|
||||
)]
|
||||
pub async fn check(
|
||||
State(state): State<AppState>,
|
||||
user: CurrentUser,
|
||||
) -> Result<Json<CheckResponse>, HTTPError> {
|
||||
Ok(Json(CheckResponse {
|
||||
authenticated: true,
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
pub mod domain;
|
||||
pub mod dto;
|
||||
pub mod handlers;
|
||||
pub mod mapper;
|
||||
pub mod routes;
|
||||
pub mod service;
|
||||
@@ -0,0 +1,16 @@
|
||||
use crate::http::OxRouter;
|
||||
use crate::routes::auth::handlers;
|
||||
use axum::routing::post;
|
||||
use axum::Router;
|
||||
|
||||
pub fn router() -> OxRouter {
|
||||
Router::new().route("/login", post(handlers::login_user_pw))
|
||||
|
||||
// .route("/categorys", get(handlers::get_all).post(handlers::create))
|
||||
// .route(
|
||||
// "/categorys/:id",
|
||||
// get(handlers::get_by_id)
|
||||
// .put(handlers::update)
|
||||
// .delete(handlers::delete),
|
||||
// )
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
use serde::Deserialize;
|
||||
use utoipa::ToSchema;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Deserialize, Validate, ToSchema)]
|
||||
pub struct JoinRequest {
|
||||
#[validate(length(min = 3, message = "Username must be at least 3 characters long"))]
|
||||
pub username: String,
|
||||
#[validate(length(min = 8, message = "Password must be at least 8 characters long"))]
|
||||
pub password: String,
|
||||
#[validate(must_match(other = "password", message = "Passwords do not match"))]
|
||||
pub password_valid: String,
|
||||
pub super_admin_token: Option<String>,
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use crate::core::AppState;
|
||||
use crate::http::error::HTTPError;
|
||||
use crate::http::validation::ValidatedJson;
|
||||
use crate::routes::core::dto::JoinRequest;
|
||||
use crate::routes::core::mapper::join_request_to_user_am;
|
||||
use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
|
||||
pub async fn join(
|
||||
State(state): State<AppState>,
|
||||
ValidatedJson(payload): ValidatedJson<JoinRequest>,
|
||||
) -> Result<impl IntoResponse, HTTPError> {
|
||||
let user_exists = state
|
||||
.repositories
|
||||
.user
|
||||
.username_exists(&payload.username)
|
||||
.await?;
|
||||
|
||||
if user_exists {
|
||||
return Err(HTTPError::validation_error(
|
||||
"username",
|
||||
"Username already exists",
|
||||
));
|
||||
};
|
||||
|
||||
let user_am = join_request_to_user_am(payload)?;
|
||||
let user = state.repositories.user.create(user_am).await?;
|
||||
state
|
||||
.repositories
|
||||
.server
|
||||
.add_user(state.default_server.id, user.id)
|
||||
.await?;
|
||||
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
use crate::auth::password::hash_password;
|
||||
use crate::models::user;
|
||||
use crate::routes::core::dto::JoinRequest;
|
||||
use anyhow::Result as AnyResult;
|
||||
use sea_orm::Set;
|
||||
|
||||
pub fn join_request_to_user_am(payload: JoinRequest) -> AnyResult<user::ActiveModel> {
|
||||
Ok(user::ActiveModel {
|
||||
id: Default::default(),
|
||||
username: Set(payload.username),
|
||||
password: Set(hash_password(&payload.password)?),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
pub mod domain;
|
||||
pub mod dto;
|
||||
pub mod handlers;
|
||||
pub mod mapper;
|
||||
pub mod routes;
|
||||
pub mod service;
|
||||
@@ -0,0 +1,7 @@
|
||||
use super::handlers;
|
||||
use crate::http::OxRouter;
|
||||
use axum::{routing::get, Router};
|
||||
|
||||
pub fn router() -> OxRouter {
|
||||
Router::new().route("/join", get(handlers::join))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
+12
-9
@@ -1,20 +1,23 @@
|
||||
use crate::http::OxRouter;
|
||||
use axum::Router;
|
||||
|
||||
pub mod attachment;
|
||||
pub mod auth;
|
||||
pub mod category;
|
||||
pub mod channel;
|
||||
pub mod core;
|
||||
pub mod group;
|
||||
pub mod message;
|
||||
pub mod server;
|
||||
pub mod user;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.merge(user::routes::router())
|
||||
.merge(server::routes::router())
|
||||
.merge(channel::routes::router())
|
||||
.merge(message::routes::router())
|
||||
.merge(group::routes::router())
|
||||
.merge(category::routes::router())
|
||||
.merge(attachment::routes::router())
|
||||
pub fn router() -> OxRouter {
|
||||
Router::new().merge(auth::routes::router())
|
||||
// .merge(user::routes::router())
|
||||
// .merge(server::routes::router())
|
||||
// .merge(channel::routes::router())
|
||||
// .merge(message::routes::router())
|
||||
// .merge(group::routes::router())
|
||||
// .merge(category::routes::router())
|
||||
// .merge(attachment::routes::router())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user