Compare commits

..

1 Commits

Author SHA1 Message Date
91cd6737f9 Fixed enter key issue 2024-12-11 04:46:27 +00:00
9 changed files with 33 additions and 78 deletions

View File

@ -18,4 +18,3 @@ DATABASE_URL=postgresql://libretunes:password@localhost:5432/libretunes
LIBRETUNES_AUDIO_PATH=assets/audio LIBRETUNES_AUDIO_PATH=assets/audio
LIBRETUNES_IMAGE_PATH=assets/images LIBRETUNES_IMAGE_PATH=assets/images
LIBRETUNES_DISABLE_SIGNUP=true

View File

@ -1,4 +1,4 @@
FROM rust:slim AS builder FROM rust:slim as builder
WORKDIR /app WORKDIR /app

View File

@ -15,7 +15,6 @@ services:
POSTGRES_DB: ${POSTGRES_DB} POSTGRES_DB: ${POSTGRES_DB}
LIBRETUNES_AUDIO_PATH: /assets/audio LIBRETUNES_AUDIO_PATH: /assets/audio
LIBRETUNES_IMAGE_PATH: /assets/images LIBRETUNES_IMAGE_PATH: /assets/images
LIBRETUNES_DISABLE_SIGNUP: "true"
volumes: volumes:
- libretunes-audio:/assets/audio - libretunes-audio:/assets/audio
- libretunes-images:/assets/images - libretunes-images:/assets/images

View File

@ -19,11 +19,6 @@ use crate::users::UserCredentials;
/// Returns a Result with the error message if the user could not be created /// Returns a Result with the error message if the user could not be created
#[server(endpoint = "signup")] #[server(endpoint = "signup")]
pub async fn signup(new_user: User) -> Result<(), ServerFnError> { pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
// Check LIBRETUNES_DISABLE_SIGNUP env var
if std::env::var("LIBRETUNES_DISABLE_SIGNUP").is_ok_and(|v| v == "true") {
return Err(ServerFnError::<NoCustomError>::ServerError("Signup is disabled".to_string()));
}
use crate::users::create_user; use crate::users::create_user;
// Ensure the user has no id, and is not a self-proclaimed admin // Ensure the user has no id, and is not a self-proclaimed admin

View File

@ -14,11 +14,10 @@ extern crate diesel_migrations;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
use axum::{routing::get, Router, extract::Path, middleware::from_fn}; use axum::{routing::get, Router, extract::Path};
use leptos::*; use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes}; use leptos_axum::{generate_route_list, LeptosRoutes};
use libretunes::app::*; use libretunes::app::*;
use libretunes::util::require_auth::require_auth_middleware;
use libretunes::fileserv::{file_and_error_handler, get_asset_file, get_static_file, AssetType}; use libretunes::fileserv::{file_and_error_handler, get_asset_file, get_static_file, AssetType};
use axum_login::tower_sessions::SessionManagerLayer; use axum_login::tower_sessions::SessionManagerLayer;
use tower_sessions_redis_store::{fred::prelude::*, RedisStore}; use tower_sessions_redis_store::{fred::prelude::*, RedisStore};
@ -64,7 +63,6 @@ async fn main() {
.route("/assets/audio/:song", get(|Path(song) : Path<String>| get_asset_file(song, AssetType::Audio))) .route("/assets/audio/:song", get(|Path(song) : Path<String>| get_asset_file(song, AssetType::Audio)))
.route("/assets/images/:image", get(|Path(image) : Path<String>| get_asset_file(image, AssetType::Image))) .route("/assets/images/:image", get(|Path(image) : Path<String>| get_asset_file(image, AssetType::Image)))
.route("/assets/*uri", get(|uri| get_static_file(uri, ""))) .route("/assets/*uri", get(|uri| get_static_file(uri, "")))
.layer(from_fn(require_auth_middleware))
.layer(auth_layer) .layer(auth_layer)
.fallback(file_and_error_handler) .fallback(file_and_error_handler)
.with_state(leptos_options); .with_state(leptos_options);

View File

@ -542,14 +542,24 @@ impl Album {
pub fn get_album_data(album_id: i32, conn: &mut PgPooledConn) -> Result<AlbumData, Box<dyn Error>> { pub fn get_album_data(album_id: i32, conn: &mut PgPooledConn) -> Result<AlbumData, Box<dyn Error>> {
use crate::schema::*; use crate::schema::*;
let artist_list: Vec<Artist> = album_artists::table let album: Vec<(Album, std::option::Option<Artist>)> = albums::table
.filter(album_artists::album_id.eq(album_id)) .find(album_id)
.inner_join(artists::table.on(album_artists::artist_id.eq(artists::id))) .left_join(songs::table.on(albums::id.nullable().eq(songs::album_id)))
.select( .left_join(song_artists::table.inner_join(artists::table).on(songs::id.eq(song_artists::song_id)))
artists::all_columns .select((
) albums::all_columns,
artists::all_columns.nullable()
))
.distinct()
.load(conn)?; .load(conn)?;
let mut artist_list: Vec<Artist> = Vec::new();
for (_, artist) in album {
if let Some(artist) = artist {
artist_list.push(artist);
}
}
// Get info of album // Get info of album
let albuminfo = albums::table let albuminfo = albums::table
.filter(albums::id.eq(album_id)) .filter(albums::id.eq(album_id))
@ -661,7 +671,7 @@ impl Album {
// Sort the songs by date // Sort the songs by date
let mut songdata: Vec<SongData> = album_songs.into_values().collect(); let mut songdata: Vec<SongData> = album_songs.into_values().collect();
songdata.sort_by(|a, b| a.track.cmp(&b.track)); songdata.sort_by(|a, b| b.track.cmp(&a.track));
Ok(songdata) Ok(songdata)
} }
} }

View File

@ -16,9 +16,10 @@ pub fn Login() -> impl IntoView {
let loading = create_rw_signal(false); let loading = create_rw_signal(false);
let error_msg = create_rw_signal(None); let error_msg = create_rw_signal(None);
let toggle_password = move |_| { let toggle_password = move |ev: leptos::ev::MouseEvent| {
ev.prevent_default();
set_show_password.update(|show_password| *show_password = !*show_password); set_show_password.update(|show_password| *show_password = !*show_password);
log!("showing password"); log!("Password visibility toggled");
}; };
let on_submit = move |ev: leptos::ev::SubmitEvent| { let on_submit = move |ev: leptos::ev::SubmitEvent| {
@ -94,16 +95,16 @@ pub fn Login() -> impl IntoView {
/> />
<span>Password</span> <span>Password</span>
<i></i> <i></i>
<Show <Show when=move || { show_password() == false }
when=move || {show_password() == false} fallback=move || view! {
fallback=move || view!{ <button on:click=toggle_password class="login-password-visibility"> <button on:click=toggle_password class="login-password-visibility">
<Icon icon=icondata::AiEyeInvisibleFilled /> <Icon icon=icondata::AiEyeInvisibleFilled />
</button> /> } </button>
}
> >
<button on:click=toggle_password class="login-password-visibility"> <button on:click=toggle_password class="login-password-visibility">
<Icon icon=icondata::AiEyeFilled /> <Icon icon=icondata::AiEyeFilled />
</button> </button>
</Show> </Show>
</div> </div>
<a href="" class="forgot-pw">Forgot Password?</a> <a href="" class="forgot-pw">Forgot Password?</a>

View File

@ -3,7 +3,6 @@ use cfg_if::cfg_if;
cfg_if! { cfg_if! {
if #[cfg(feature = "ssr")] { if #[cfg(feature = "ssr")] {
pub mod audio; pub mod audio;
pub mod require_auth;
} }
} }

View File

@ -1,46 +0,0 @@
use axum::extract::Request;
use axum::response::Response;
use axum::body::Body;
use axum::middleware::Next;
use axum_login::AuthSession;
use http::StatusCode;
use crate::auth_backend::AuthBackend;
use axum::extract::FromRequestParts;
// Things in pkg/ are allowed automatically. This includes the CSS/JS/WASM files
const ALLOWED_PATHS: [&str; 5] = ["/login", "/signup", "/api/login", "/api/signup", "/favicon.ico"];
/**
* Middleware to require authentication for all paths except those in ALLOWED_PATHS
*
* If a user is not authenticated, they will be redirected to the login page
*/
pub async fn require_auth_middleware(req: Request, next: Next) -> Result<Response<Body>, (StatusCode, &'static str)> {
let path = req.uri().path();
if !ALLOWED_PATHS.iter().any(|&x| x == path) {
let (mut parts, body) = req.into_parts();
let auth_session = AuthSession::<AuthBackend>::from_request_parts(&mut parts, &())
.await?;
if auth_session.user.is_none() {
let response = Response::builder()
.status(StatusCode::TEMPORARY_REDIRECT)
.header("Location", "/login")
.body(Body::empty())
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Failed to build response"))?;
return Ok(response);
}
let req = Request::from_parts(parts, body);
let response = next.run(req).await;
Ok(response)
} else {
let response = next.run(req).await;
Ok(response)
}
}