diff --git a/src/lib.rs b/src/lib.rs index c3ef877..62b7b65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,9 @@ extern crate justerror; pub mod db; pub mod generic_handlers; +pub mod login; pub mod session_store; pub mod signup; pub(crate) mod templates; + +pub use signup::User; diff --git a/src/login.rs b/src/login.rs index e69de29..8010c86 100644 --- a/src/login.rs +++ b/src/login.rs @@ -0,0 +1,67 @@ +use argon2::PasswordVerifier; +use axum::{ + extract::State, + http::StatusCode, + response::{IntoResponse, Response}, +}; +use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore}; +use sqlx::SqlitePool; +use uuid::Uuid; + +use crate::{templates::LoginGet, User}; + +pub type AuthContext = axum_login::extractors::AuthContext>; + +impl AuthUser for User { + fn get_id(&self) -> Uuid { + self.id + } + + fn get_password_hash(&self) -> SecretVec { + SecretVec::new(self.pwhash.as_bytes().to_vec()) + } +} + +//-************************************************************************ +// Login error and success types +//-************************************************************************ + +#[Error] +pub struct LoginError(#[from] LoginErrorKind); + +#[Error] +#[non_exhaustive] +pub enum LoginErrorKind { + BadPassword, + Unknown, +} + +impl IntoResponse for LoginError { + fn into_response(self) -> Response { + match self.0 { + LoginErrorKind::Unknown => ( + StatusCode::INTERNAL_SERVER_ERROR, + "An unknown error occurred; you cursed, brah?", + ) + .into_response(), + _ => (StatusCode::BAD_REQUEST, format!("{self}")).into_response(), + } + } +} + +//-************************************************************************ +// Login handlers +//-************************************************************************ + +/// Handle login queries +#[axum::debug_handler] +pub async fn post_login( + mut auth: AuthContext, + State(pool): State, +) -> Result<(), LoginError> { + Err(LoginErrorKind::Unknown.into()) +} + +pub async fn get_login() -> impl IntoResponse { + LoginGet::default() +} diff --git a/src/main.rs b/src/main.rs index 7cc38cc..638a8bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use witch_watch::{ db, generic_handlers::{handle_slash, handle_slash_redir}, + login::{get_login, post_login}, session_store::SqliteSessionStore, - signup::{get_create_user, get_login, handle_signup_success, post_create_user, post_login}, + signup::{get_create_user, handle_signup_success, post_create_user}, }; #[tokio::main] diff --git a/src/signup.rs b/src/signup.rs index d2d7513..917e233 100644 --- a/src/signup.rs +++ b/src/signup.rs @@ -10,12 +10,11 @@ use axum::{ http::StatusCode, response::{IntoResponse, Response}, }; -use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore}; use sqlx::{query_as, SqlitePool}; use unicode_segmentation::UnicodeSegmentation; use uuid::Uuid; -use crate::templates::{CreateUser, LoginGet}; +use crate::templates::CreateUser; const CREATE_QUERY: &str = "insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)"; @@ -28,7 +27,7 @@ pub struct User { pub displayname: Option, pub email: Option, pub last_seen: Option, - pwhash: String, + pub(crate) pwhash: String, } impl Display for User { @@ -44,18 +43,6 @@ impl Display for User { } } -pub type AuthContext = axum_login::extractors::AuthContext>; - -impl AuthUser for User { - fn get_id(&self) -> Uuid { - self.id - } - - fn get_password_hash(&self) -> SecretVec { - SecretVec::new(self.pwhash.as_bytes().to_vec()) - } -} - //-************************************************************************ // Result types for user creation //-************************************************************************ @@ -199,50 +186,6 @@ pub async fn handle_signup_success( resp } -//-************************************************************************ -// Login error and success types -//-************************************************************************ - -#[Error] -pub struct LoginError(#[from] LoginErrorKind); - -#[Error] -#[non_exhaustive] -pub enum LoginErrorKind { - BadPassword, - Unknown, -} - -impl IntoResponse for LoginError { - fn into_response(self) -> Response { - match self.0 { - LoginErrorKind::Unknown => ( - StatusCode::INTERNAL_SERVER_ERROR, - "An unknown error occurred; you cursed, brah?", - ) - .into_response(), - _ => (StatusCode::BAD_REQUEST, format!("{self}")).into_response(), - } - } -} - -//-************************************************************************ -// Login handlers -//-************************************************************************ - -/// Handle login queries -#[axum::debug_handler] -pub async fn post_login( - mut auth: AuthContext, - State(pool): State, -) -> Result<(), LoginError> { - Err(LoginErrorKind::Unknown.into()) -} - -pub async fn get_login() -> impl IntoResponse { - LoginGet::default() -} - //-************************************************************************ // private fns //-************************************************************************