Minor tweaks.

This commit is contained in:
Joe Ardent 2023-06-02 14:15:13 -07:00
parent 659bc0d008
commit 569b19e837
4 changed files with 26 additions and 15 deletions

View File

@ -19,7 +19,7 @@ pub async fn handle_slash(auth: AuthContext) -> impl IntoResponse {
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod test {
use axum_test::TestServer; use axum_test::TestServer;
use crate::db; use crate::db;

View File

@ -39,11 +39,12 @@ pub enum LoginErrorKind {
impl IntoResponse for LoginError { impl IntoResponse for LoginError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match self.0 { match self.0 {
LoginErrorKind::Unknown | LoginErrorKind::Internal => ( LoginErrorKind::Internal => (
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
"An unknown error occurred; you cursed, brah?", "An unknown error occurred; you cursed, brah?",
) )
.into_response(), .into_response(),
LoginErrorKind::Unknown => (StatusCode::OK, "Not successful.").into_response(),
_ => (StatusCode::OK, format!("{self}")).into_response(), _ => (StatusCode::OK, format!("{self}")).into_response(),
} }
} }
@ -79,7 +80,7 @@ pub async fn post_login(
.await .await
.map_err(|_| LoginErrorKind::Internal)?; .map_err(|_| LoginErrorKind::Internal)?;
Ok(Redirect::temporary("/")) Ok(Redirect::to("/"))
} }
_ => Err(LoginErrorKind::BadPassword.into()), _ => Err(LoginErrorKind::BadPassword.into()),
} }

View File

@ -78,8 +78,11 @@ pub async fn post_create_user(
let email = &signup.email; let email = &signup.email;
let password = &signup.password; let password = &signup.password;
let verify = &signup.pw_verify; let verify = &signup.pw_verify;
let username = username.trim();
let username = urlencoding::decode(username)
.map_err(|_| CreateUserErrorKind::BadUsername)?
.to_string();
let username = username.trim();
let name_len = username.graphemes(true).size_hint().1.unwrap(); let name_len = username.graphemes(true).size_hint().1.unwrap();
// we are not ascii exclusivists around here // we are not ascii exclusivists around here
if !(1..=20).contains(&name_len) { if !(1..=20).contains(&name_len) {
@ -165,7 +168,7 @@ pub async fn handle_signup_success(
// private fns // private fns
//-************************************************************************ //-************************************************************************
async fn create_user( pub(crate) async fn create_user(
username: &str, username: &str,
displayname: &Option<String>, displayname: &Option<String>,
email: &Option<String>, email: &Option<String>,
@ -181,14 +184,21 @@ async fn create_user(
.to_string(); .to_string();
let id = Uuid::new_v4(); let id = Uuid::new_v4();
let res = sqlx::query(CREATE_QUERY) let query = sqlx::query(CREATE_QUERY)
.bind(id) .bind(id)
.bind(username) .bind(username)
.bind(displayname) .bind(displayname)
.bind(email) .bind(email)
.bind(&pwhash) .bind(&pwhash);
.execute(pool)
.await; let res = {
let txn = pool.begin().await.expect("Could not beign transaction");
let r = query.execute(pool).await;
txn.commit()
.await
.expect("Should be able to commit transaction");
r
};
match res { match res {
Ok(_) => { Ok(_) => {

View File

@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::User; use crate::User;
#[derive(Debug, Default, Template, Deserialize, Serialize)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)]
#[template(path = "signup.html")] #[template(path = "signup.html")]
pub struct CreateUser { pub struct CreateUser {
pub username: String, pub username: String,
@ -13,29 +13,29 @@ pub struct CreateUser {
pub pw_verify: String, pub pw_verify: String,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)]
#[template(path = "login_post.html")] #[template(path = "login_post.html")]
pub struct LoginPost { pub struct LoginPost {
pub username: String, pub username: String,
pub password: String, pub password: String,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)]
#[template(path = "login_get.html")] #[template(path = "login_get.html")]
pub struct LoginGet { pub struct LoginGet {
pub username: String, pub username: String,
pub password: String, pub password: String,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)]
#[template(path = "logout_get.html")] #[template(path = "logout_get.html")]
pub struct LogoutGet; pub struct LogoutGet;
#[derive(Debug, Default, Template, Deserialize, Serialize)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)]
#[template(path = "logout_post.html")] #[template(path = "logout_post.html")]
pub struct LogoutPost; pub struct LogoutPost;
#[derive(Debug, Default, Template, Deserialize, Serialize)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)]
#[template(path = "index.html")] #[template(path = "index.html")]
pub struct Index { pub struct Index {
pub user: Option<User>, pub user: Option<User>,