4 Commits

Author SHA1 Message Date
spinline
10262142fc Fix unused OpenApi import warning
Some checks are pending
Build MIPS Binary / build (push) Waiting to run
2026-02-07 19:34:41 +03:00
spinline
858a1c9b63 Fix compilation errors: Restore missing delete_session method and ApiDoc struct
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 19:28:47 +03:00
spinline
edfb7458f8 Add CLI password reset feature: --reset-password <USERNAME>
Some checks failed
Build MIPS Binary / build (push) Failing after 3m24s
2026-02-07 19:18:10 +03:00
spinline
575cfa4b38 Add 'Remember Me' feature to login (extends session to 30 days)
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 19:05:52 +03:00
4 changed files with 115 additions and 10 deletions

View File

@@ -111,4 +111,21 @@ impl Db {
.await?;
Ok(())
}
pub async fn update_password(&self, user_id: i64, password_hash: &str) -> Result<()> {
sqlx::query("UPDATE users SET password_hash = ? WHERE id = ?")
.bind(password_hash)
.bind(user_id)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn delete_all_sessions_for_user(&self, user_id: i64) -> Result<()> {
sqlx::query("DELETE FROM sessions WHERE user_id = ?")
.bind(user_id)
.execute(&self.pool)
.await?;
Ok(())
}
}

View File

@@ -13,6 +13,8 @@ use time::Duration;
pub struct LoginRequest {
username: String,
password: String,
#[serde(default)]
remember_me: bool,
}
#[derive(Serialize, ToSchema)]
@@ -61,8 +63,13 @@ pub async fn login_handler(
rand::thread_rng().sample(Alphanumeric) as char
}).collect();
// Expires in 30 days
let expires_in = 60 * 60 * 24 * 30;
// Expiration: 30 days if remember_me is true, else 1 day
let expires_in = if payload.remember_me {
60 * 60 * 24 * 30
} else {
60 * 60 * 24
};
let expires_at = time::OffsetDateTime::now_utc().unix_timestamp() + expires_in;
if let Err(e) = state.db.create_session(user_id, &token, expires_at).await {
@@ -70,13 +77,14 @@ pub async fn login_handler(
return (StatusCode::INTERNAL_SERVER_ERROR, "Failed to create session").into_response();
}
let cookie = Cookie::build(("auth_token", token))
let mut cookie = Cookie::build(("auth_token", token))
.path("/")
.http_only(true)
.same_site(SameSite::Lax)
.max_age(Duration::seconds(expires_in))
.build();
cookie.set_max_age(Duration::seconds(expires_in));
tracing::info!("Session created and cookie set for user: {}", payload.username);
(StatusCode::OK, jar.add(cookie), Json(UserResponse { username: payload.username })).into_response()
}

View File

@@ -30,7 +30,6 @@ use tower_http::{
cors::CorsLayer,
trace::TraceLayer,
};
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
#[derive(Clone)]
@@ -90,6 +89,10 @@ struct Args {
/// Database URL
#[arg(long, env = "DATABASE_URL", default_value = "sqlite:vibetorrent.db")]
db_url: String,
/// Reset password for the specified user
#[arg(long)]
reset_password: Option<String>,
}
#[cfg(feature = "push-notifications")]
@@ -130,7 +133,8 @@ struct Args {
push::PushKeys,
handlers::auth::LoginRequest,
handlers::setup::SetupRequest,
handlers::setup::SetupStatusResponse
handlers::setup::SetupStatusResponse,
handlers::auth::UserResponse
)
),
tags(
@@ -173,7 +177,8 @@ struct ApiDoc;
shared::GlobalLimitRequest,
handlers::auth::LoginRequest,
handlers::setup::SetupRequest,
handlers::setup::SetupStatusResponse
handlers::setup::SetupStatusResponse,
handlers::auth::UserResponse
)
),
tags(
@@ -197,9 +202,6 @@ async fn main() {
// Parse CLI Args
let args = Args::parse();
tracing::info!("Starting VibeTorrent Backend...");
tracing::info!("Socket: {}", args.socket);
tracing::info!("Port: {}", args.port);
// Initialize Database
tracing::info!("Connecting to database: {}", args.db_url);
@@ -224,6 +226,68 @@ async fn main() {
};
tracing::info!("Database connected successfully.");
// Handle Password Reset
if let Some(username) = args.reset_password {
tracing::info!("Resetting password for user: {}", username);
// Check if user exists
let user_result = db.get_user_by_username(&username).await;
match user_result {
Ok(Some((user_id, _))) => {
// Generate random password
use rand::{distributions::Alphanumeric, Rng};
let new_password: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(12)
.map(char::from)
.collect();
// Hash password (low cost for performance)
let password_hash = match bcrypt::hash(&new_password, 6) {
Ok(h) => h,
Err(e) => {
tracing::error!("Failed to hash password: {}", e);
std::process::exit(1);
}
};
// Update in DB (using a direct query since db.rs doesn't have update_password yet)
// We should add `update_password` to db.rs for cleaner code, but for now direct query is fine or we can extend Db.
// Let's extend Db.rs first to be clean.
if let Err(e) = db.update_password(user_id, &password_hash).await {
tracing::error!("Failed to update password in DB: {}", e);
std::process::exit(1);
}
println!("--------------------------------------------------");
println!("Password reset successfully for user: {}", username);
println!("New Password: {}", new_password);
println!("--------------------------------------------------");
// Invalidate existing sessions for security
if let Err(e) = db.delete_all_sessions_for_user(user_id).await {
tracing::warn!("Failed to invalidate existing sessions: {}", e);
}
std::process::exit(0);
},
Ok(None) => {
tracing::error!("User '{}' not found.", username);
std::process::exit(1);
},
Err(e) => {
tracing::error!("Database error: {}", e);
std::process::exit(1);
}
}
}
tracing::info!("Starting VibeTorrent Backend...");
tracing::info!("Socket: {}", args.socket);
tracing::info!("Port: {}", args.port);
// ... rest of the main function ...
// Startup Health Check
let socket_path = std::path::Path::new(&args.socket);
if !socket_path.exists() {

View File

@@ -6,12 +6,14 @@ use serde::Serialize;
struct LoginRequest {
username: String,
password: String,
remember_me: bool,
}
#[component]
pub fn Login() -> impl IntoView {
let (username, set_username) = create_signal(String::new());
let (password, set_password) = create_signal(String::new());
let (remember_me, set_remember_me) = create_signal(false);
let (error, set_error) = create_signal(Option::<String>::None);
let (loading, set_loading) = create_signal(false);
@@ -26,6 +28,7 @@ pub fn Login() -> impl IntoView {
let req = LoginRequest {
username: username.get(),
password: password.get(),
remember_me: remember_me.get(),
};
let client = gloo_net::http::Request::post("/api/auth/login")
@@ -89,6 +92,19 @@ pub fn Login() -> impl IntoView {
/>
</div>
<div class="form-control mt-4">
<label class="label cursor-pointer justify-start gap-3">
<input
type="checkbox"
class="checkbox checkbox-primary checkbox-sm"
prop:checked=remember_me
on:change=move |ev| set_remember_me.set(event_target_checked(&ev))
disabled=move || loading.get()
/>
<span class="label-text">"Beni Hatırla"</span>
</label>
</div>
<Show when=move || error.get().is_some()>
<div class="alert alert-error mt-4 text-sm py-2">
<svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>