From d2613b0ef98006d1916eb951921f8988a6cdb386 Mon Sep 17 00:00:00 2001
From: Joe Ardent
+Hello, {{ usr.username }}! It's nice to see you.
+
+
Date: Mon, 29 May 2023 14:25:50 -0700
Subject: [PATCH] End-to-end web-based signup, login, and logout flows work.
---
src/generic_handlers.rs | 12 ++++++++----
src/login.rs | 15 +++++++++------
src/main.rs | 14 ++++++++++----
src/templates.rs | 16 ++++++++++++++++
src/users.rs | 3 ++-
templates/index.html | 26 ++++++++++++++++++++++++++
templates/logout_get.html | 13 +++++++++++++
templates/logout_post.html | 11 +++++++++++
8 files changed, 95 insertions(+), 15 deletions(-)
create mode 100644 templates/index.html
create mode 100644 templates/logout_get.html
create mode 100644 templates/logout_post.html
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
Welcome to Witch Watch
+
+{% match user %}
+ {% when Some with (usr) %}
+
+ 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 %} + +Good bye! May we suggest checking out our home page?
+ +{% endblock %}