Merge remote-tracking branch 'origin/main' into 22-create-home-page-2
This commit is contained in:
commit
8e44a48b18
2081
Cargo.lock
generated
2081
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
55
Cargo.toml
55
Cargo.toml
@ -8,31 +8,16 @@ build = "src/build.rs"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
actix-files = { version = "0.6", optional = true }
|
||||
actix-web = { version = "4", optional = true, features = ["macros"] }
|
||||
console_error_panic_hook = "0.1"
|
||||
cfg-if = "1"
|
||||
http = { version = "0.2", optional = true }
|
||||
leptos = { version = "0.5", features = ["nightly"] }
|
||||
leptos_meta = { version = "0.5", features = ["nightly"] }
|
||||
leptos_actix = { version = "0.5", optional = true }
|
||||
leptos_router = { version = "0.5", features = ["nightly"] }
|
||||
http = "1.0"
|
||||
leptos = { version = "0.6", features = ["nightly"] }
|
||||
leptos_meta = { version = "0.6", features = ["nightly"] }
|
||||
leptos_axum = { version = "0.6", optional = true }
|
||||
leptos_router = { version = "0.6", features = ["nightly"] }
|
||||
wasm-bindgen = "=0.2.92"
|
||||
leptos_icons = { version = "0.1.0", default_features = false, features = [
|
||||
"BsPlayFill",
|
||||
"BsPauseFill",
|
||||
"BsSkipStartFill",
|
||||
"BsSkipEndFill",
|
||||
"RiPlayListMediaFill",
|
||||
"CgTrash",
|
||||
"IoReturnUpBackSharp",
|
||||
"AiEyeFilled",
|
||||
"AiEyeInvisibleFilled",
|
||||
"OcHomeFillLg",
|
||||
"BiSearchRegular",
|
||||
"CgProfile",
|
||||
"IoAddSharp"
|
||||
] }
|
||||
leptos_icons = { version = "0.3.0" }
|
||||
icondata = { version = "0.3.0" }
|
||||
dotenv = { version = "0.15.0", optional = true }
|
||||
diesel = { version = "2.1.4", features = ["postgres", "r2d2", "time"], optional = true }
|
||||
lazy_static = { version = "1.4.0", optional = true }
|
||||
@ -40,18 +25,26 @@ serde = { versions = "1.0.195", features = ["derive"] }
|
||||
openssl = { version = "0.10.63", optional = true }
|
||||
time = { version = "0.3.34", features = ["serde"] }
|
||||
diesel_migrations = { version = "2.1.0", optional = true }
|
||||
actix-identity = { version = "0.7.0", optional = true }
|
||||
actix-session = { version = "0.9.0", features = ["redis-rs-session"], optional = true }
|
||||
pbkdf2 = { version = "0.12.2", features = ["simple"], optional = true }
|
||||
futures = { version = "0.3.30", default-features = false, optional = true }
|
||||
tokio = { version = "1", optional = true, features = ["rt-multi-thread"] }
|
||||
axum = { version = "0.7.5", optional = true }
|
||||
tower = { veresion = "0.4.13", optional = true }
|
||||
tower-http = { version = "0.5", optional = true, features = ["fs"] }
|
||||
thiserror = "1.0.57"
|
||||
tower-sessions = { version = "0.11", default-features = false }
|
||||
tower-sessions-redis-store = { version = "0.11", optional = true }
|
||||
async-trait = "0.1.79"
|
||||
axum-login = { version = "0.14.0", optional = true }
|
||||
|
||||
[patch.crates-io]
|
||||
gloo-net = { git = "https://github.com/rustwasm/gloo.git", rev = "a823fab7ecc4068e9a28bd669da5eaf3f0a56380" }
|
||||
|
||||
[features]
|
||||
csr = ["leptos/csr", "leptos_meta/csr", "leptos_router/csr"]
|
||||
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
|
||||
ssr = [
|
||||
"dep:actix-files",
|
||||
"dep:actix-web",
|
||||
"dep:leptos_actix",
|
||||
"dep:leptos_axum",
|
||||
"leptos/ssr",
|
||||
"leptos_meta/ssr",
|
||||
"leptos_router/ssr",
|
||||
@ -60,10 +53,14 @@ ssr = [
|
||||
"lazy_static",
|
||||
"openssl",
|
||||
"diesel_migrations",
|
||||
"actix-identity",
|
||||
"actix-session",
|
||||
"pbkdf2",
|
||||
"futures",
|
||||
"tokio",
|
||||
"axum",
|
||||
"tower",
|
||||
"tower-http",
|
||||
"tower-sessions-redis-store",
|
||||
"axum-login",
|
||||
]
|
||||
|
||||
# Defines a size-optimized profile for the WASM bundle in release mode
|
||||
|
15
src/app.rs
15
src/app.rs
@ -6,6 +6,7 @@ use leptos_meta::*;
|
||||
use leptos_router::*;
|
||||
use crate::pages::login::*;
|
||||
use crate::pages::signup::*;
|
||||
use crate::error_template::{AppError, ErrorTemplate};
|
||||
|
||||
|
||||
#[component]
|
||||
@ -22,11 +23,17 @@ pub fn App() -> impl IntoView {
|
||||
<Title text="LibreTunes"/>
|
||||
|
||||
// content for this welcome page
|
||||
<Router>
|
||||
<Router fallback=|| {
|
||||
let mut outside_errors = Errors::default();
|
||||
outside_errors.insert_with_default_key(AppError::NotFound);
|
||||
view! {
|
||||
<ErrorTemplate outside_errors/>
|
||||
}
|
||||
.into_view()
|
||||
}>
|
||||
<main>
|
||||
<Routes>
|
||||
<Route path="" view=HomePage/>
|
||||
<Route path="/*any" view=NotFound/>
|
||||
<Route path="/login" view=Login />
|
||||
<Route path="/signup" view=Signup />
|
||||
</Routes>
|
||||
@ -77,8 +84,8 @@ fn NotFound() -> impl IntoView {
|
||||
{
|
||||
// this can be done inline because it's synchronous
|
||||
// if it were async, we'd use a server function
|
||||
let resp = expect_context::<leptos_actix::ResponseOptions>();
|
||||
resp.set_status(actix_web::http::StatusCode::NOT_FOUND);
|
||||
let resp = expect_context::<leptos_axum::ResponseOptions>();
|
||||
resp.set_status(axum::http::StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
view! {
|
||||
|
111
src/auth.rs
111
src/auth.rs
@ -1,5 +1,18 @@
|
||||
use leptos::*;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
use leptos_axum::extract;
|
||||
use axum_login::AuthSession;
|
||||
use crate::auth_backend::AuthBackend;
|
||||
}
|
||||
}
|
||||
|
||||
use crate::models::User;
|
||||
use crate::users::UserCredentials;
|
||||
|
||||
/// Create a new user and log them in
|
||||
/// Takes in a NewUser struct, with the password in plaintext
|
||||
@ -8,10 +21,6 @@ use crate::models::User;
|
||||
pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
|
||||
use crate::users::create_user;
|
||||
|
||||
use leptos_actix::extract;
|
||||
use actix_web::{HttpMessage, HttpRequest};
|
||||
use actix_identity::Identity;
|
||||
|
||||
// Ensure the user has no id
|
||||
let new_user = User {
|
||||
id: None,
|
||||
@ -19,53 +28,95 @@ pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
|
||||
};
|
||||
|
||||
create_user(&new_user).await
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error creating user: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error creating user: {}", e)))?;
|
||||
|
||||
extract(|request: HttpRequest| async move {
|
||||
Identity::login(&request.extensions(), new_user.username.clone())
|
||||
}).await??;
|
||||
let mut auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
let credentials = UserCredentials {
|
||||
username_or_email: new_user.username.clone(),
|
||||
password: new_user.password.clone().unwrap()
|
||||
};
|
||||
|
||||
match auth_session.authenticate(credentials).await {
|
||||
Ok(Some(user)) => {
|
||||
auth_session.login(&user).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error logging in user: {}", e)))
|
||||
},
|
||||
Ok(None) => {
|
||||
Err(ServerFnError::<NoCustomError>::ServerError("Error authenticating user: User not found".to_string()))
|
||||
},
|
||||
Err(e) => {
|
||||
Err(ServerFnError::<NoCustomError>::ServerError(format!("Error authenticating user: {}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a user in
|
||||
/// Takes in a username or email and a password in plaintext
|
||||
/// Returns a Result with a boolean indicating if the login was successful
|
||||
#[server(endpoint = "login")]
|
||||
pub async fn login(username_or_email: String, password: String) -> Result<bool, ServerFnError> {
|
||||
pub async fn login(credentials: UserCredentials) -> Result<bool, ServerFnError> {
|
||||
use crate::users::validate_user;
|
||||
use actix_web::{HttpMessage, HttpRequest};
|
||||
use actix_identity::Identity;
|
||||
use leptos_actix::extract;
|
||||
|
||||
let possible_user = validate_user(username_or_email, password).await
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error validating user: {}", e)))?;
|
||||
let mut auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
let user = match possible_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(false)
|
||||
};
|
||||
|
||||
extract(|request: HttpRequest| async move {
|
||||
Identity::login(&request.extensions(), user.username.clone())
|
||||
}).await??;
|
||||
let user = validate_user(credentials).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error validating user: {}", e)))?;
|
||||
|
||||
if let Some(user) = user {
|
||||
auth_session.login(&user).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error logging in user: {}", e)))?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a user out
|
||||
/// Returns a Result with the error message if the user could not be logged out
|
||||
#[server(endpoint = "logout")]
|
||||
pub async fn logout() -> Result<(), ServerFnError> {
|
||||
use leptos_actix::extract;
|
||||
use actix_identity::Identity;
|
||||
let mut auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
extract(|user: Option<Identity>| async move {
|
||||
if let Some(user) = user {
|
||||
user.logout();
|
||||
}
|
||||
}).await?;
|
||||
auth_session.logout().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if a user is logged in
|
||||
/// Returns a Result with a boolean indicating if the user is logged in
|
||||
#[server(endpoint = "check_auth")]
|
||||
pub async fn check_auth() -> Result<bool, ServerFnError> {
|
||||
let auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
Ok(auth_session.user.is_some())
|
||||
}
|
||||
|
||||
/// Require that a user is logged in
|
||||
/// Returns a Result with the error message if the user is not logged in
|
||||
/// Intended to be used at the start of a protected route, to ensure the user is logged in:
|
||||
/// ```rust
|
||||
/// use leptos::*;
|
||||
/// use libretunes::auth::require_auth;
|
||||
/// #[server(endpoint = "protected_route")]
|
||||
/// pub async fn protected_route() -> Result<(), ServerFnError> {
|
||||
/// require_auth().await?;
|
||||
/// // Continue with protected route
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn require_auth() -> Result<(), ServerFnError> {
|
||||
check_auth().await.and_then(|logged_in| {
|
||||
if logged_in {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ServerFnError::<NoCustomError>::ServerError(format!("Unauthorized")))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
41
src/auth_backend.rs
Normal file
41
src/auth_backend.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use async_trait::async_trait;
|
||||
use axum_login::{AuthnBackend, AuthUser, UserId};
|
||||
use crate::users::UserCredentials;
|
||||
use leptos::server_fn::error::ServerFnErrorErr;
|
||||
|
||||
use crate::models::User;
|
||||
|
||||
impl AuthUser for User {
|
||||
type Id = i32;
|
||||
|
||||
// TODO: Ideally, we shouldn't have to unwrap here
|
||||
|
||||
fn id(&self) -> Self::Id {
|
||||
self.id.unwrap()
|
||||
}
|
||||
|
||||
fn session_auth_hash(&self) -> &[u8] {
|
||||
self.password.as_ref().unwrap().as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AuthBackend;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
#[async_trait]
|
||||
impl AuthnBackend for AuthBackend {
|
||||
type User = User;
|
||||
type Credentials = UserCredentials;
|
||||
type Error = ServerFnErrorErr;
|
||||
|
||||
async fn authenticate(&self, creds: Self::Credentials) -> Result<Option<Self::User>, Self::Error> {
|
||||
crate::users::validate_user(creds).await
|
||||
.map_err(|e| ServerFnErrorErr::ServerError(format!("Error validating user: {}", e)))
|
||||
}
|
||||
|
||||
async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> {
|
||||
crate::users::find_user_by_id(*user_id).await
|
||||
.map_err(|e| ServerFnErrorErr::ServerError(format!("Error getting user: {}", e)))
|
||||
}
|
||||
}
|
73
src/error_template.rs
Normal file
73
src/error_template.rs
Normal file
@ -0,0 +1,73 @@
|
||||
use http::status::StatusCode;
|
||||
use leptos::*;
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
use leptos_axum::ResponseOptions;
|
||||
|
||||
#[derive(Clone, Debug, Error)]
|
||||
pub enum AppError {
|
||||
#[error("Not Found")]
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl AppError {
|
||||
pub fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
AppError::NotFound => StatusCode::NOT_FOUND,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A basic function to display errors served by the error boundaries.
|
||||
// Feel free to do more complicated things here than just displaying the error.
|
||||
#[component]
|
||||
pub fn ErrorTemplate(
|
||||
#[prop(optional)] outside_errors: Option<Errors>,
|
||||
#[prop(optional)] errors: Option<RwSignal<Errors>>,
|
||||
) -> impl IntoView {
|
||||
let errors = match outside_errors {
|
||||
Some(e) => create_rw_signal(e),
|
||||
None => match errors {
|
||||
Some(e) => e,
|
||||
None => panic!("No Errors found and we expected errors!"),
|
||||
},
|
||||
};
|
||||
// Get Errors from Signal
|
||||
let errors = errors.get_untracked();
|
||||
|
||||
// Downcast lets us take a type that implements `std::error::Error`
|
||||
let errors: Vec<AppError> = errors
|
||||
.into_iter()
|
||||
.filter_map(|(_k, v)| v.downcast_ref::<AppError>().cloned())
|
||||
.collect();
|
||||
|
||||
// Only the response code for the first error is actually sent from the server
|
||||
// this may be customized by the specific application
|
||||
#[cfg(feature = "ssr")]
|
||||
{
|
||||
let response = use_context::<ResponseOptions>();
|
||||
if let Some(response) = response {
|
||||
response.set_status(errors[0].status_code());
|
||||
}
|
||||
}
|
||||
|
||||
view! {
|
||||
<h1>{if errors.len() > 1 {"Errors"} else {"Error"}}</h1>
|
||||
<For
|
||||
// a function that returns the items we're iterating over; a signal is fine
|
||||
each= move || {errors.clone().into_iter().enumerate()}
|
||||
// a unique key for each item as a reference
|
||||
key=|(index, _error)| *index
|
||||
// renders each item to a view
|
||||
children=move |error| {
|
||||
let error_string = error.1.to_string();
|
||||
let error_code= error.1.status_code();
|
||||
view! {
|
||||
<h2>{error_code.to_string()}</h2>
|
||||
<p>"Error: " {error_string}</p>
|
||||
}
|
||||
}
|
||||
/>
|
||||
}
|
||||
}
|
40
src/fileserv.rs
Normal file
40
src/fileserv.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::State,
|
||||
response::IntoResponse,
|
||||
http::{Request, Response, StatusCode, Uri},
|
||||
};
|
||||
use axum::response::Response as AxumResponse;
|
||||
use tower::ServiceExt;
|
||||
use tower_http::services::ServeDir;
|
||||
use leptos::*;
|
||||
use crate::app::App;
|
||||
|
||||
pub async fn file_and_error_handler(uri: Uri, State(options): State<LeptosOptions>, req: Request<Body>) -> AxumResponse {
|
||||
let root = options.site_root.clone();
|
||||
let res = get_static_file(uri.clone(), &root).await.unwrap();
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
res.into_response()
|
||||
} else {
|
||||
let handler = leptos_axum::render_app_to_stream(options.to_owned(), App);
|
||||
handler(req).await.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_static_file(uri: Uri, root: &str) -> Result<Response<Body>, (StatusCode, String)> {
|
||||
let req = Request::builder().uri(uri.clone()).body(Body::empty()).unwrap();
|
||||
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
|
||||
// This path is relative to the cargo root
|
||||
match ServeDir::new(root).oneshot(req).await {
|
||||
Ok(res) => Ok(res.into_response()),
|
||||
Err(err) => Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Something went wrong: {err}"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}}
|
@ -11,10 +11,13 @@ pub mod pages;
|
||||
pub mod components;
|
||||
pub mod users;
|
||||
pub mod search;
|
||||
pub mod fileserv;
|
||||
pub mod error_template;
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
pub mod auth_backend;
|
||||
pub mod schema;
|
||||
}
|
||||
}
|
||||
|
84
src/main.rs
84
src/main.rs
@ -12,12 +12,17 @@ extern crate diesel;
|
||||
extern crate diesel_migrations;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_identity::IdentityMiddleware;
|
||||
use actix_session::storage::RedisSessionStore;
|
||||
use actix_session::SessionMiddleware;
|
||||
use actix_web::cookie::Key;
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
use axum::{routing::get, Router};
|
||||
use leptos::*;
|
||||
use leptos_axum::{generate_route_list, LeptosRoutes};
|
||||
use libretunes::app::*;
|
||||
use libretunes::fileserv::{file_and_error_handler, get_static_file};
|
||||
use tower_sessions::SessionManagerLayer;
|
||||
use tower_sessions_redis_store::{fred::prelude::*, RedisStore};
|
||||
use axum_login::AuthManagerLayerBuilder;
|
||||
use libretunes::auth_backend::AuthBackend;
|
||||
|
||||
use dotenv::dotenv;
|
||||
dotenv().ok();
|
||||
@ -25,60 +30,35 @@ async fn main() -> std::io::Result<()> {
|
||||
// Bring the database up to date
|
||||
libretunes::database::migrate();
|
||||
|
||||
let session_secret_key = if let Ok(key) = std::env::var("SESSION_SECRET_KEY") {
|
||||
Key::from(key.as_bytes())
|
||||
} else {
|
||||
Key::generate()
|
||||
};
|
||||
|
||||
let redis_url = std::env::var("REDIS_URL").expect("REDIS_URL must be set");
|
||||
let redis_store = RedisSessionStore::new(redis_url).await.unwrap();
|
||||
let redis_config = RedisConfig::from_url(&redis_url).expect(&format!("Unable to parse Redis URL: {}", redis_url));
|
||||
let redis_pool = RedisPool::new(redis_config, None, None, None, 1).expect("Unable to create Redis pool");
|
||||
redis_pool.connect();
|
||||
redis_pool.wait_for_connect().await.expect("Unable to connect to Redis");
|
||||
|
||||
use actix_files::Files;
|
||||
use actix_web::*;
|
||||
use leptos::*;
|
||||
use leptos_actix::{generate_route_list, LeptosRoutes};
|
||||
use libretunes::app::*;
|
||||
let session_store = RedisStore::new(redis_pool);
|
||||
let session_layer = SessionManagerLayer::new(session_store);
|
||||
|
||||
let auth_backend = AuthBackend;
|
||||
let auth_layer = AuthManagerLayerBuilder::new(auth_backend, session_layer).build();
|
||||
|
||||
let conf = get_configuration(None).await.unwrap();
|
||||
let addr = conf.leptos_options.site_addr;
|
||||
let leptos_options = conf.leptos_options;
|
||||
let addr = leptos_options.site_addr;
|
||||
// Generate the list of routes in your Leptos App
|
||||
let routes = generate_route_list(App);
|
||||
|
||||
let app = Router::new()
|
||||
.leptos_routes(&leptos_options, routes, App)
|
||||
.route("/assets/*uri", get(|uri| get_static_file(uri, "")))
|
||||
.layer(auth_layer)
|
||||
.fallback(file_and_error_handler)
|
||||
.with_state(leptos_options);
|
||||
|
||||
println!("listening on http://{}", &addr);
|
||||
|
||||
HttpServer::new(move || {
|
||||
let leptos_options = &conf.leptos_options;
|
||||
let site_root = &leptos_options.site_root;
|
||||
|
||||
App::new()
|
||||
.route("/api/{tail:.*}", leptos_actix::handle_server_fns())
|
||||
// serve JS/WASM/CSS from `pkg`
|
||||
.service(Files::new("/pkg", format!("{site_root}/pkg")))
|
||||
// serve other assets from the `assets` directory
|
||||
.service(Files::new("/assets", site_root))
|
||||
// serve the favicon from /favicon.ico
|
||||
.service(favicon)
|
||||
.leptos_routes(leptos_options.to_owned(), routes.to_owned(), App)
|
||||
.app_data(web::Data::new(leptos_options.to_owned()))
|
||||
.wrap(IdentityMiddleware::default())
|
||||
.wrap(SessionMiddleware::new(redis_store.clone(), session_secret_key.clone()))
|
||||
//.wrap(middleware::Compress::default())
|
||||
})
|
||||
.bind(&addr)?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
#[actix_web::get("favicon.ico")]
|
||||
async fn favicon(
|
||||
leptos_options: actix_web::web::Data<leptos::LeptosOptions>,
|
||||
) -> actix_web::Result<actix_files::NamedFile> {
|
||||
let leptos_options = leptos_options.into_inner();
|
||||
let site_root = &leptos_options.site_root;
|
||||
Ok(actix_files::NamedFile::open(format!(
|
||||
"{site_root}/favicon.ico"
|
||||
))?)
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await.expect(&format!("Could not bind to {}", &addr));
|
||||
axum::serve(listener, app.into_make_service()).await.expect("Server failed");
|
||||
}
|
||||
|
||||
#[cfg(not(any(feature = "ssr", feature = "csr")))]
|
||||
|
@ -1,9 +1,8 @@
|
||||
use crate::auth::login;
|
||||
use leptos::leptos_dom::*;
|
||||
use leptos::*;
|
||||
use leptos_icons::AiIcon::*;
|
||||
use leptos_icons::IoIcon::*;
|
||||
use leptos_icons::*;
|
||||
use crate::users::UserCredentials;
|
||||
|
||||
#[component]
|
||||
pub fn Login() -> impl IntoView {
|
||||
@ -24,7 +23,12 @@ pub fn Login() -> impl IntoView {
|
||||
let password1 = password.get();
|
||||
|
||||
spawn_local(async move {
|
||||
let login_result = login(username_or_email1, password1).await;
|
||||
let user_credentials = UserCredentials {
|
||||
username_or_email: username_or_email1,
|
||||
password: password1
|
||||
};
|
||||
|
||||
let login_result = login(user_credentials).await;
|
||||
if let Err(err) = login_result {
|
||||
// Handle the error here, e.g., log it or display to the user
|
||||
log!("Error logging in: {:?}", err);
|
||||
@ -42,7 +46,7 @@ pub fn Login() -> impl IntoView {
|
||||
view! {
|
||||
<div class="auth-page-container">
|
||||
<div class="login-container">
|
||||
<a class="return" href="/"><Icon icon=Icon::from(IoReturnUpBackSharp) /></a>
|
||||
<a class="return" href="/"><Icon icon=icondata::IoReturnUpBackSharp /></a>
|
||||
<div class="header">
|
||||
<h1>LibreTunes</h1>
|
||||
</div>
|
||||
@ -70,11 +74,11 @@ pub fn Login() -> impl IntoView {
|
||||
<Show
|
||||
when=move || {show_password() == false}
|
||||
fallback=move || view!{ <button on:click=toggle_password class="login-password-visibility">
|
||||
<Icon icon=Icon::from(AiEyeInvisibleFilled) />
|
||||
<Icon icon=icondata::AiEyeInvisibleFilled />
|
||||
</button> /> }
|
||||
>
|
||||
<button on:click=toggle_password class="login-password-visibility">
|
||||
<Icon icon=Icon::from(AiEyeFilled) />
|
||||
<Icon icon=icondata::AiEyeFilled />
|
||||
</button>
|
||||
|
||||
</Show>
|
||||
|
@ -2,8 +2,6 @@ use crate::auth::signup;
|
||||
use crate::models::User;
|
||||
use leptos::leptos_dom::*;
|
||||
use leptos::*;
|
||||
use leptos_icons::AiIcon::*;
|
||||
use leptos_icons::IoIcon::*;
|
||||
use leptos_icons::*;
|
||||
|
||||
#[component]
|
||||
@ -46,7 +44,7 @@ pub fn Signup() -> impl IntoView {
|
||||
view! {
|
||||
<div class="auth-page-container">
|
||||
<div class="signup-container">
|
||||
<a class="return" href="/"><Icon icon=Icon::from(IoReturnUpBackSharp) /></a>
|
||||
<a class="return" href="/"><Icon icon=icondata::IoReturnUpBackSharp /></a>
|
||||
<div class="header">
|
||||
<h1>LibreTunes</h1>
|
||||
</div>
|
||||
@ -83,10 +81,10 @@ pub fn Signup() -> impl IntoView {
|
||||
<i></i>
|
||||
<Show
|
||||
when=move || {show_password() == false}
|
||||
fallback=move || view!{ <button on:click=toggle_password class="password-visibility"> <Icon icon=Icon::from(AiEyeInvisibleFilled) /></button> /> }
|
||||
fallback=move || view!{ <button on:click=toggle_password class="password-visibility"> <Icon icon=icondata::AiEyeInvisibleFilled /></button> /> }
|
||||
>
|
||||
<button on:click=toggle_password class="password-visibility">
|
||||
<Icon icon=Icon::from(AiEyeFilled) />
|
||||
<Icon icon=icondata::AiEyeFilled />
|
||||
</button>
|
||||
</Show>
|
||||
</div>
|
||||
|
@ -3,8 +3,6 @@ use leptos::ev::MouseEvent;
|
||||
use leptos::html::{Audio, Div};
|
||||
use leptos::leptos_dom::*;
|
||||
use leptos::*;
|
||||
use leptos_icons::BsIcon::*;
|
||||
use leptos_icons::RiIcon::*;
|
||||
use leptos_icons::*;
|
||||
|
||||
/// Width and height of the forward/backward skip buttons
|
||||
@ -193,9 +191,9 @@ fn PlayControls(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
let icon = Signal::derive(move || {
|
||||
status.with(|status| {
|
||||
if status.playing {
|
||||
Icon::from(BsPauseFill)
|
||||
icondata::BsPauseFill
|
||||
} else {
|
||||
Icon::from(BsPlayFill)
|
||||
icondata::BsPlayFill
|
||||
}
|
||||
})
|
||||
});
|
||||
@ -204,7 +202,7 @@ fn PlayControls(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
<div class="playcontrols" align="center">
|
||||
|
||||
<button on:click=skip_back on:mousedown=prevent_focus>
|
||||
<Icon class="controlbtn" width=SKIP_BTN_SIZE height=SKIP_BTN_SIZE icon=Icon::from(BsSkipStartFill) />
|
||||
<Icon class="controlbtn" width=SKIP_BTN_SIZE height=SKIP_BTN_SIZE icon=icondata::BsSkipStartFill />
|
||||
</button>
|
||||
|
||||
<button on:click=toggle_play on:mousedown=prevent_focus>
|
||||
@ -212,7 +210,7 @@ fn PlayControls(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
</button>
|
||||
|
||||
<button on:click=skip_forward on:mousedown=prevent_focus>
|
||||
<Icon class="controlbtn" width=SKIP_BTN_SIZE height=SKIP_BTN_SIZE icon=Icon::from(BsSkipEndFill) />
|
||||
<Icon class="controlbtn" width=SKIP_BTN_SIZE height=SKIP_BTN_SIZE icon=icondata::BsSkipEndFill />
|
||||
</button>
|
||||
|
||||
</div>
|
||||
@ -337,7 +335,7 @@ fn QueueToggle(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
view! {
|
||||
<div class="queue-toggle">
|
||||
<button on:click=update_queue on:mousedown=prevent_focus>
|
||||
<Icon class="controlbtn" width=QUEUE_BTN_SIZE height=QUEUE_BTN_SIZE icon=Icon::from(RiPlayListMediaFill) />
|
||||
<Icon class="controlbtn" width=QUEUE_BTN_SIZE height=QUEUE_BTN_SIZE icon=icondata::RiPlayListMediaFill />
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ use leptos::ev::MouseEvent;
|
||||
use leptos::leptos_dom::*;
|
||||
use leptos::*;
|
||||
use leptos_icons::*;
|
||||
use leptos_icons::CgIcon::*;
|
||||
use leptos::ev::DragEvent;
|
||||
|
||||
const RM_BTN_SIZE: &str = "2.5rem";
|
||||
@ -106,7 +105,7 @@ pub fn Queue(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
<p>Playing</p>
|
||||
}>
|
||||
<button on:click=move |_| remove_song(index) on:mousedown=prevent_focus>
|
||||
<Icon class="remove-song" width=RM_BTN_SIZE height=RM_BTN_SIZE icon=Icon::from(CgTrash) />
|
||||
<Icon class="remove-song" width=RM_BTN_SIZE height=RM_BTN_SIZE icon=icondata::CgTrash />
|
||||
</button>
|
||||
</Show>
|
||||
</div>
|
||||
|
48
src/users.rs
48
src/users.rs
@ -14,19 +14,42 @@ cfg_if::cfg_if! {
|
||||
}
|
||||
|
||||
use leptos::*;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::models::User;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct UserCredentials {
|
||||
pub username_or_email: String,
|
||||
pub password: String
|
||||
}
|
||||
|
||||
/// Get a user from the database by username or email
|
||||
/// Returns a Result with the user if found, None if not found, or an error if there was a problem
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn find_user(username_or_email: String) -> Result<Option<User>, ServerFnError> {
|
||||
use crate::schema::users::dsl::*;
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
|
||||
// Look for either a username or email that matches the input, and return an option with None if no user is found
|
||||
let db_con = &mut get_db_conn();
|
||||
let user = users.filter(username.eq(username_or_email.clone())).or_filter(email.eq(username_or_email))
|
||||
.first::<User>(db_con).optional()
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
/// Get a user from the database by ID
|
||||
/// Returns a Result with the user if found, None if not found, or an error if there was a problem
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn find_user_by_id(user_id: i32) -> Result<Option<User>, ServerFnError> {
|
||||
use crate::schema::users::dsl::*;
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let user = users.filter(id.eq(user_id))
|
||||
.first::<User>(db_con).optional()
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
@ -36,13 +59,14 @@ pub async fn find_user(username_or_email: String) -> Result<Option<User>, Server
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
|
||||
use crate::schema::users::dsl::*;
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
|
||||
let new_password = new_user.password.clone()
|
||||
.ok_or(ServerFnError::ServerError(format!("No password provided for user {}", new_user.username)))?;
|
||||
.ok_or(ServerFnError::<NoCustomError>::ServerError(format!("No password provided for user {}", new_user.username)))?;
|
||||
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
let password_hash = Pbkdf2.hash_password(new_password.as_bytes(), &salt)
|
||||
.map_err(|_| ServerFnError::ServerError("Error hashing password".to_string()))?.to_string();
|
||||
.map_err(|_| ServerFnError::<NoCustomError>::ServerError("Error hashing password".to_string()))?.to_string();
|
||||
|
||||
let new_user = User {
|
||||
password: Some(password_hash),
|
||||
@ -52,7 +76,7 @@ pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
|
||||
let db_con = &mut get_db_conn();
|
||||
|
||||
diesel::insert_into(users).values(&new_user).execute(db_con)
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error creating user: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error creating user: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -60,9 +84,11 @@ pub async fn create_user(new_user: &User) -> Result<(), ServerFnError> {
|
||||
/// Validate a user's credentials
|
||||
/// Returns a Result with the user if the credentials are valid, None if not valid, or an error if there was a problem
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn validate_user(username_or_email: String, password: String) -> Result<Option<User>, ServerFnError> {
|
||||
let db_user = find_user(username_or_email.clone()).await
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||
pub async fn validate_user(credentials: UserCredentials) -> Result<Option<User>, ServerFnError> {
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
|
||||
let db_user = find_user(credentials.username_or_email.clone()).await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user from database: {}", e)))?;
|
||||
|
||||
// If the user is not found, return None
|
||||
let db_user = match db_user {
|
||||
@ -71,18 +97,18 @@ pub async fn validate_user(username_or_email: String, password: String) -> Resul
|
||||
};
|
||||
|
||||
let db_password = db_user.password.clone()
|
||||
.ok_or(ServerFnError::ServerError(format!("No password found for user {}", db_user.username)))?;
|
||||
.ok_or(ServerFnError::<NoCustomError>::ServerError(format!("No password found for user {}", db_user.username)))?;
|
||||
|
||||
let password_hash = PasswordHash::new(&db_password)
|
||||
.map_err(|e| ServerFnError::ServerError(format!("Error hashing supplied password: {}", e)))?;
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error hashing supplied password: {}", e)))?;
|
||||
|
||||
match Pbkdf2.verify_password(password.as_bytes(), &password_hash) {
|
||||
match Pbkdf2.verify_password(credentials.password.as_bytes(), &password_hash) {
|
||||
Ok(()) => {},
|
||||
Err(Error::Password) => {
|
||||
return Ok(None);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(ServerFnError::ServerError(format!("Error verifying password: {}", e)));
|
||||
return Err(ServerFnError::<NoCustomError>::ServerError(format!("Error verifying password: {}", e)));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user