better signup pages

This commit is contained in:
Joe Ardent 2023-05-18 15:49:33 -07:00
parent 79ecd74500
commit fee7fff3df
4 changed files with 46 additions and 7 deletions

1
.env Normal file
View file

@ -0,0 +1 @@
DATABASE_URL=sqlite://${HOME}/.witch-watch.db

View file

@ -1,7 +1,10 @@
use std::fmt::Display;
use argon2::{ use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString}, password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2, Argon2,
}; };
use askama::Template;
use axum::{ use axum::{
extract::{Form, State}, extract::{Form, State},
http::StatusCode, http::StatusCode,
@ -24,6 +27,19 @@ pub struct User {
email: Option<String>, email: Option<String>,
} }
impl Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let uname = &self.username;
let dname = if let Some(ref n) = self.displayname {
n
} else {
""
};
let email = if let Some(ref e) = self.email { e } else { "" };
write!(f, "Username: {uname}\nDisplayname: {dname}\nEmail: {email}")
}
}
#[derive(Debug, Clone, sqlx::FromRow, sqlx::Encode)] #[derive(Debug, Clone, sqlx::FromRow, sqlx::Encode)]
pub(crate) struct DbUser { pub(crate) struct DbUser {
id: Uuid, id: Uuid,
@ -34,6 +50,10 @@ pub(crate) struct DbUser {
pwhash: String, pwhash: String,
} }
#[derive(Debug, Clone, Template)]
#[template(path = "signup_success.html")]
pub struct CreateUserSuccess(User);
impl From<DbUser> for User { impl From<DbUser> for User {
fn from(dbu: DbUser) -> Self { fn from(dbu: DbUser) -> Self {
User { User {
@ -56,7 +76,7 @@ pub async fn get_create_user() -> CreateUser {
pub async fn post_create_user( pub async fn post_create_user(
State(pool): State<SqlitePool>, State(pool): State<SqlitePool>,
Form(signup): Form<CreateUser>, Form(signup): Form<CreateUser>,
) -> Result<(), CreateUserError> { ) -> Result<CreateUserSuccess, CreateUserError> {
let username = &signup.username; let username = &signup.username;
let displayname = &signup.displayname; let displayname = &signup.displayname;
let email = &signup.email; let email = &signup.email;
@ -102,7 +122,7 @@ pub async fn post_create_user(
let user = create_user(username, displayname, email, password, &pool).await?; let user = create_user(username, displayname, email, password, &pool).await?;
tracing::debug!("created {user:?}"); tracing::debug!("created {user:?}");
Ok(()) Ok(CreateUserSuccess(user))
} }
async fn create_user( async fn create_user(

View file

@ -4,18 +4,20 @@
{% block content %} {% block content %}
<p>
<form action="/signup" enctype="application/x-www-form-urlencoded" method="post"> <form action="/signup" enctype="application/x-www-form-urlencoded" method="post">
<label for="username">Username</label> <label for="username">Username</label>
<input type="text" name="username" id="username" minlength="1" maxlength="20" required> <input type="text" name="username" id="username" minlength="1" maxlength="20" required></br>
<label for="displayname">Displayname (optional)</label> <label for="displayname">Displayname (optional)</label>
<input type="text" name="displayname" id="displayname"> <input type="text" name="displayname" id="displayname"></br>
<label for="email">Email (optional)</label> <label for="email">Email (optional)</label>
<input type="text" name="email"> <input type="text" name="email"></br>
<label for="password">Password</label> <label for="password">Password</label>
<input type="password" name="password" id="password" required> <input type="password" name="password" id="password" required></br>
<label for="confirm_password">Confirm Password</label> <label for="confirm_password">Confirm Password</label>
<input type="password" name="pw_verify" id="pw_verify" required> <input type="password" name="pw_verify" id="pw_verify" required></br>
<input type="submit" value="Signup"> <input type="submit" value="Signup">
</form> </form>
</p>
{% endblock %} {% endblock %}

View file

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block title %}Thanks for Signing Up for Witch Watch, Bish{% endblock %}
{% block content %}
<h1>You did it!</h1>
<div id="signup_success"><p>
{{ self.0 }}
</p>
</div>
<p>Now, head on over to <a href="/login">the login page</a> and get watchin'!</p>
{% endblock %}