diff --git a/src/generic_handlers.rs b/src/generic_handlers.rs index 1e21dc2..8791922 100644 --- a/src/generic_handlers.rs +++ b/src/generic_handlers.rs @@ -1,15 +1,19 @@ use axum::response::{IntoResponse, Redirect}; -use crate::AuthContext; +use crate::{templates::Index, AuthContext}; pub async fn handle_slash_redir() -> impl IntoResponse { Redirect::temporary("/") } pub async fn handle_slash(auth: AuthContext) -> impl IntoResponse { - if let Some(user) = auth.current_user { - tracing::debug!("Logged in as: {user}"); + if let Some(ref user) = auth.current_user { + let name = &user.username; + tracing::debug!("Logged in as: {name}"); } else { - tracing::debug!("Not logged in.") + tracing::debug!("Not logged in."); + } + Index { + user: auth.current_user, } } diff --git a/src/login.rs b/src/login.rs index d9d3603..225b216 100644 --- a/src/login.rs +++ b/src/login.rs @@ -11,7 +11,7 @@ use axum::{ use sqlx::SqlitePool; use crate::{ - templates::{LoginGet, LoginPost}, + templates::{LoginGet, LoginPost, LogoutGet, LogoutPost}, util::form_decode, AuthContext, User, }; @@ -41,12 +41,12 @@ pub enum LoginErrorKind { impl IntoResponse for LoginError { fn into_response(self) -> Response { match self.0 { - LoginErrorKind::Unknown => ( + LoginErrorKind::Unknown | LoginErrorKind::Internal => ( StatusCode::INTERNAL_SERVER_ERROR, "An unknown error occurred; you cursed, brah?", ) .into_response(), - _ => (StatusCode::BAD_REQUEST, format!("{self}")).into_response(), + _ => (StatusCode::OK, format!("{self}")).into_response(), } } } @@ -99,9 +99,12 @@ pub async fn get_login() -> impl IntoResponse { } pub async fn get_logout() -> impl IntoResponse { - todo!() + LogoutGet } -pub async fn post_logout() -> impl IntoResponse { - todo!() +pub async fn post_logout(mut auth: AuthContext) -> impl IntoResponse { + if auth.current_user.is_some() { + auth.logout().await; + } + LogoutPost } diff --git a/src/main.rs b/src/main.rs index 39903e5..35ab102 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ -use std::net::SocketAddr; +use std::{net::SocketAddr, time::Duration}; use axum::{routing::get, Router}; -use axum_login::{axum_sessions::SessionLayer, AuthLayer, SqliteStore}; +use axum_login::{ + axum_sessions::{PersistencePolicy, SessionLayer}, + AuthLayer, SqliteStore, +}; use rand_core::{OsRng, RngCore}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use witch_watch::{ @@ -26,7 +29,7 @@ async fn main() { let pool = db::get_pool().await; let secret = { - let mut bytes = [0u8; 128]; + let mut bytes = [0u8; 64]; let mut rng = OsRng; rng.fill_bytes(&mut bytes); bytes @@ -35,7 +38,10 @@ async fn main() { let session_layer = { let store = SqliteSessionStore::from_client(pool.clone()); store.migrate().await.expect("Could not migrate session DB"); - SessionLayer::new(store, &secret).with_secure(true) + SessionLayer::new(store, &secret) + .with_secure(true) + .with_persistence_policy(PersistencePolicy::ExistingOnly) + .with_session_ttl(Some(Duration::from_secs(3600 * 24 * 366))) }; let auth_layer = { diff --git a/src/templates.rs b/src/templates.rs index f8ad15f..a40b25d 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,6 +1,8 @@ use askama::Template; use serde::{Deserialize, Serialize}; +use crate::User; + #[derive(Debug, Default, Template, Deserialize, Serialize)] #[template(path = "signup.html")] pub struct CreateUser { @@ -24,3 +26,17 @@ pub struct LoginGet { pub username: String, pub password: String, } + +#[derive(Debug, Default, Template, Deserialize, Serialize)] +#[template(path = "logout_get.html")] +pub struct LogoutGet; + +#[derive(Debug, Default, Template, Deserialize, Serialize)] +#[template(path = "logout_post.html")] +pub struct LogoutPost; + +#[derive(Debug, Default, Template, Deserialize, Serialize)] +#[template(path = "index.html")] +pub struct Index { + pub user: Option, +} diff --git a/src/users.rs b/src/users.rs index 4ef50fd..baeaf2f 100644 --- a/src/users.rs +++ b/src/users.rs @@ -1,12 +1,13 @@ use std::fmt::Display; use axum_login::{secrecy::SecretVec, AuthUser}; +use serde::{Deserialize, Serialize}; use sqlx::SqlitePool; use uuid::Uuid; const USERNAME_QUERY: &str = "select * from witches where username = $1"; -#[derive(Debug, Default, Clone, PartialEq, Eq, sqlx::FromRow)] +#[derive(Debug, Default, Clone, PartialEq, Eq, sqlx::FromRow, Serialize, Deserialize)] pub struct User { pub id: Uuid, pub username: String, diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..e7c47f2 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,26 @@ +{% extends "base.html" %} + +{% block title %}Welcome to Witch Watch, Bish{% endblock %} + +{% block content %} + +

Welcome to Witch Watch

+ +{% match user %} + {% when Some with (usr) %} +

+Hello, {{ usr.username }}! It's nice to see you. +

+
+

+

+ +
+

+ {% else %} +

+ Heya, why don't you log in or sign up? +

+{% endmatch %} + +{% endblock %} diff --git a/templates/logout_get.html b/templates/logout_get.html new file mode 100644 index 0000000..4ed382a --- /dev/null +++ b/templates/logout_get.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block title %}Logout of Witch Watch, Bish{% endblock %} + +{% block content %} + +

+

+ +
+

+ +{% endblock %} diff --git a/templates/logout_post.html b/templates/logout_post.html new file mode 100644 index 0000000..69dffba --- /dev/null +++ b/templates/logout_post.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block title %}Thanks for Signing Up for Witch Watch, Bish{% endblock %} + +{% block content %} + +

Goodbye

+ +

Good bye! May we suggest checking out our home page?

+ +{% endblock %}