mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Remove warnings and add some debug logging
This commit is contained in:
@@ -29,7 +29,7 @@ pub async fn start_api(address: &str) {
|
||||
|
||||
pub async fn protected(
|
||||
Path(path): Path<String>,
|
||||
Extension(currentUser): Extension<CurrentUser>,
|
||||
Extension(_currentUser): Extension<CurrentUser>,
|
||||
) -> impl IntoResponse {
|
||||
info!("{}", path);
|
||||
// Json(UserResponse {
|
||||
|
||||
@@ -8,7 +8,7 @@ use bcrypt::{DEFAULT_COST, hash, verify};
|
||||
use chrono::Utc;
|
||||
use jsonwebtoken::{Header, TokenData, Validation, decode, encode};
|
||||
use serde_json::{Value, json};
|
||||
use unshell_lib::info;
|
||||
use unshell_lib::{debug, info};
|
||||
|
||||
use crate::api::{
|
||||
EXPIRE_DURATION, JWT_DECODING_KEY, JWT_ENCODING_KEY,
|
||||
@@ -86,7 +86,13 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
|
||||
// 1. Retrieve user from the database
|
||||
let user = match retrieve_user_by_email(&user_data.username) {
|
||||
Some(user) => user,
|
||||
None => return Err(StatusCode::UNAUTHORIZED), // User not found
|
||||
None => {
|
||||
debug!(
|
||||
"Denied user {}: Could not find user data",
|
||||
user_data.username
|
||||
);
|
||||
return Err(StatusCode::UNAUTHORIZED);
|
||||
} // User not found
|
||||
};
|
||||
|
||||
// 2. Compare the password
|
||||
@@ -94,6 +100,7 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
// Handle bcrypt errors
|
||||
{
|
||||
debug!("Denied user {}: Incorrect password hash", user.username);
|
||||
return Err(StatusCode::UNAUTHORIZED); // Wrong password
|
||||
}
|
||||
|
||||
@@ -104,7 +111,7 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
|
||||
|
||||
// 3. Generate JWT
|
||||
let (token, experation) =
|
||||
encode_jwt(user.email).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
encode_jwt(user.username).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
// 4. Return the token
|
||||
Ok(Json(json!({
|
||||
@@ -115,9 +122,7 @@ pub async fn sign_in(Json(user_data): Json<SignInData>) -> Result<Json<Value>, S
|
||||
|
||||
fn retrieve_user_by_email(_email: &str) -> Option<CurrentUser> {
|
||||
let current_user: CurrentUser = CurrentUser {
|
||||
email: "foo".to_string(),
|
||||
first_name: "Eze".to_string(),
|
||||
last_name: "Sunday".to_string(),
|
||||
username: "foo".to_string(),
|
||||
password_hash: hash_password("bar").unwrap(),
|
||||
};
|
||||
Some(current_user)
|
||||
|
||||
@@ -6,7 +6,6 @@ extern crate unshell_lib;
|
||||
pub mod app;
|
||||
mod auth;
|
||||
mod structs;
|
||||
mod userdata;
|
||||
|
||||
static EXPIRE_DURATION: Duration = Duration::hours(12);
|
||||
|
||||
|
||||
@@ -15,9 +15,7 @@ pub struct SignInData {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CurrentUser {
|
||||
pub email: String,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub username: String,
|
||||
pub password_hash: String,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
pub struct UserData {
|
||||
username: String,
|
||||
hash: Vec<u8>,
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
// #![macro_use]
|
||||
|
||||
#[cfg(feature = "run")]
|
||||
mod api;
|
||||
#[cfg(feature = "run")]
|
||||
pub use api::app::start_api;
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
use axum;
|
||||
use tokio::net::TcpListener;
|
||||
use unshell_lib::info;
|
||||
|
||||
use unshell_server::start_api;
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
Reference in New Issue
Block a user