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)]
mod tests {
mod test {
use axum_test::TestServer;
use crate::db;

View file

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

View file

@ -78,8 +78,11 @@ pub async fn post_create_user(
let email = &signup.email;
let password = &signup.password;
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();
// we are not ascii exclusivists around here
if !(1..=20).contains(&name_len) {
@ -165,7 +168,7 @@ pub async fn handle_signup_success(
// private fns
//-************************************************************************
async fn create_user(
pub(crate) async fn create_user(
username: &str,
displayname: &Option<String>,
email: &Option<String>,
@ -181,14 +184,21 @@ async fn create_user(
.to_string();
let id = Uuid::new_v4();
let res = sqlx::query(CREATE_QUERY)
let query = sqlx::query(CREATE_QUERY)
.bind(id)
.bind(username)
.bind(displayname)
.bind(email)
.bind(&pwhash)
.execute(pool)
.await;
.bind(&pwhash);
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 {
Ok(_) => {

View file

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