diff --git a/backend/src/cookie/jwt.rs b/backend/src/cookie/jwt.rs index 2281777..8f37315 100644 --- a/backend/src/cookie/jwt.rs +++ b/backend/src/cookie/jwt.rs @@ -1,5 +1,5 @@ use axum::{Json, http::StatusCode}; -use jsonwebtoken::{DecodingKey, EncodingKey, Header, Validation, decode, encode}; +use jsonwebtoken::{DecodingKey, EncodingKey, Header, decode, encode}; use serde::{Deserialize, Serialize}; use crate::models::Claims; @@ -65,7 +65,7 @@ pub fn decode_token(token: String, key: &DecodingKey) -> Result(&token, key, &validation) .map_err(|err| { let message = format!("Invalid Token: {}", err); diff --git a/backend/src/cookie/validation.rs b/backend/src/cookie/validation.rs index 53166f4..e1d238c 100644 --- a/backend/src/cookie/validation.rs +++ b/backend/src/cookie/validation.rs @@ -12,12 +12,7 @@ use axum_extra::extract::CookieJar; use jsonwebtoken::DecodingKey; use serde_json::json; -use crate::{ - AppState, - cookie::jwt::decode_token, - handlers::auth::filter_user, - models::{LoginScheme, User}, -}; +use crate::{AppState, cookie::jwt::decode_token, handlers::auth::filter_user, models::User}; /// Axum middleware to validate a JWT token present in cookies or Authorization header. /// diff --git a/backend/src/handlers/auth.rs b/backend/src/handlers/auth.rs index 2be8fc1..4d251cb 100644 --- a/backend/src/handlers/auth.rs +++ b/backend/src/handlers/auth.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, usize}; +use std::sync::Arc; use argon2::{ Argon2, PasswordHash, PasswordHasher, PasswordVerifier, @@ -11,7 +11,6 @@ use axum::{ response::IntoResponse, }; use axum_extra::extract::cookie::{Cookie, SameSite}; -use chrono::format; use jsonwebtoken::{EncodingKey, Header}; use serde_json::json; @@ -485,15 +484,16 @@ pub async fn update_user( pub async fn check_admin_exists( State(data): State>, ) -> Result)> { - let admin_count = sqlx::query_scalar::<_, i64>(r#"SELECT COUNT(*) FROM users WHERE is_admin = true"#) - .fetch_one(&data.db) - .await - .map_err(|e| { - ( - StatusCode::INTERNAL_SERVER_ERROR, - Json(json!({"status": "error", "message": format!("{:?}", e)})), - ) - })?; + let admin_count = + sqlx::query_scalar::<_, i64>(r#"SELECT COUNT(*) FROM users WHERE is_admin = true"#) + .fetch_one(&data.db) + .await + .map_err(|e| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({"status": "error", "message": format!("{:?}", e)})), + ) + })?; let has_admin = admin_count > 0; Ok(Json(json!({"has_admin": has_admin}))) @@ -528,15 +528,16 @@ pub async fn setup_initial_admin( Json(request): Json, ) -> Result)> { // Check if any admin already exists - let admin_count = sqlx::query_scalar::<_, i64>(r#"SELECT COUNT(*) FROM users WHERE is_admin = true"#) - .fetch_one(&data.db) - .await - .map_err(|e| { - ( - StatusCode::INTERNAL_SERVER_ERROR, - Json(json!({"status": "error", "message": format!("{:?}", e)})), - ) - })?; + let admin_count = + sqlx::query_scalar::<_, i64>(r#"SELECT COUNT(*) FROM users WHERE is_admin = true"#) + .fetch_one(&data.db) + .await + .map_err(|e| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({"status": "error", "message": format!("{:?}", e)})), + ) + })?; if admin_count > 0 { return Err(( @@ -580,7 +581,9 @@ pub async fn setup_initial_admin( Json(json!({"status": "error", "message": "Error creating admin user"})), )); } else { - Ok(Json(json!({"status": "success", "result": "Admin user created"}))) + Ok(Json( + json!({"status": "success", "result": "Admin user created"}), + )) } } diff --git a/backend/src/main.rs b/backend/src/main.rs index 44ccb83..04c0dd9 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,5 +1,3 @@ -#![allow(unused_imports)] - /// Cookie and JWT authentication utilities mod cookie; /// Environment configuration loading @@ -13,18 +11,12 @@ mod router; use std::sync::Arc; -use axum::{ - Router, - http::{ - HeaderValue, Method, - header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}, - }, - routing, +use axum::http::{ + HeaderValue, Method, + header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}, }; use dotenv::dotenv; -use models::*; use router::create_router; -use serde::{Deserialize, Serialize}; use sqlx::{PgPool, postgres::PgPoolOptions}; use tower_http::cors::CorsLayer; @@ -65,7 +57,7 @@ async fn main() { dotenv().ok(); let env = Env::load(); let database_url = &env.db_url; - + // Establish connection pool to PostgreSQL let pool = match PgPoolOptions::new().connect(&database_url).await { Ok(pool) => { diff --git a/backend/src/models.rs b/backend/src/models.rs index a8054d8..184670d 100644 --- a/backend/src/models.rs +++ b/backend/src/models.rs @@ -1,7 +1,4 @@ -use std::fmt::Display; - use serde::{Deserialize, Serialize}; -use sqlx::{Decode, prelude::Type}; /// API response for a ticket with user information. ///