create logs for login

This commit is contained in:
philipp 2023-07-25 11:31:51 +02:00
parent c92452a085
commit 1436feaffd

View File

@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
use sqlx::{FromRow, SqlitePool}; use sqlx::{FromRow, SqlitePool};
use super::{tripdetails::TripDetails, Day}; use super::{log::Log, tripdetails::TripDetails, Day};
#[derive(FromRow, Debug, Serialize, Deserialize)] #[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct User { pub struct User {
@ -148,15 +148,15 @@ ORDER BY last_access DESC
} }
pub async fn login(db: &SqlitePool, name: &str, pw: &str) -> Result<Self, LoginError> { pub async fn login(db: &SqlitePool, name: &str, pw: &str) -> Result<Self, LoginError> {
info!("User '{name}' is trying to login..."); Log::create(db, "User '{name}' is trying to login...".into()).await;
let name = name.trim(); // just to make sure... let name = name.trim(); // just to make sure...
let Some(user) = User::find_by_name(db, name).await else { let Some(user) = User::find_by_name(db, name).await else {
info!("Username ({name}) not found"); Log::create(db, "Username ({name}) not found".into()).await;
return Err(LoginError::InvalidAuthenticationCombo); // Username not found return Err(LoginError::InvalidAuthenticationCombo); // Username not found
}; };
if user.deleted { if user.deleted {
info!("User ({name}) already deleted."); Log::create(db, "User ({name}) already deleted.".into()).await;
return Err(LoginError::InvalidAuthenticationCombo); //User existed sometime ago; has return Err(LoginError::InvalidAuthenticationCombo); //User existed sometime ago; has
//been deleted //been deleted
} }
@ -165,11 +165,10 @@ ORDER BY last_access DESC
Some(user_pw) => { Some(user_pw) => {
let password_hash = &Self::get_hashed_pw(pw); let password_hash = &Self::get_hashed_pw(pw);
if password_hash == user_pw { if password_hash == user_pw {
info!("User {name} successfully logged in"); Log::create(db, "User {name} successfully logged in".into()).await;
return Ok(user); return Ok(user);
} }
info!("User {name} supplied the wrong PW"); Log::create(db, "User {name} supplied the wrong PW".into()).await;
Err(LoginError::InvalidAuthenticationCombo) Err(LoginError::InvalidAuthenticationCombo)
} }
None => { None => {