"serde_json",
"serde_path_to_error",
"serde_urlencoded",
- "sync_wrapper 1.0.2",
+ "sync_wrapper",
"tokio",
"tower",
"tower-layer",
"mime",
"pin-project-lite",
"rustversion",
- "sync_wrapper 1.0.2",
+ "sync_wrapper",
"tower-layer",
"tower-service",
"tracing",
[[package]]
name = "cc"
-version = "1.2.2"
+version = "1.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc"
+checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf"
dependencies = [
"shlex",
]
[[package]]
name = "chrono"
-version = "0.4.38"
+version = "0.4.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
+checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825"
dependencies = [
"android-tzdata",
"iana-time-zone",
[[package]]
name = "clap"
-version = "4.5.22"
+version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "69371e34337c4c984bbe322360c2547210bf632eb2814bbe78a6e87a2935bd2b"
+checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84"
dependencies = [
"clap_builder",
"clap_derive",
[[package]]
name = "clap_builder"
-version = "4.5.22"
+version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6e24c1b4099818523236a8ca881d2b45db98dadfb4625cf6608c12069fcbbde1"
+checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838"
dependencies = [
"anstream",
"anstyle",
[[package]]
name = "clap_lex"
-version = "0.7.3"
+version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7"
+checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]]
name = "colorchoice"
[[package]]
name = "crossbeam-queue"
-version = "0.3.11"
+version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35"
+checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
-version = "0.8.20"
+version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
+checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "crypto-common"
[[package]]
name = "fastrand"
-version = "2.2.0"
+version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4"
+checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]]
name = "flume"
[[package]]
name = "home"
-version = "0.5.9"
+version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
+checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf"
dependencies = [
- "windows-sys 0.52.0",
+ "windows-sys 0.59.0",
]
[[package]]
[[package]]
name = "hyper"
-version = "1.5.1"
+version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "97818827ef4f364230e16705d4706e2897df2bb60617d6ca15d598025a3c481f"
+checksum = "256fb8d4bd6413123cc9d91832d78325c48ff41677595be797d90f42969beae0"
dependencies = [
"bytes",
"futures-channel",
[[package]]
name = "js-sys"
-version = "0.3.74"
+version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705"
+checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7"
dependencies = [
"once_cell",
"wasm-bindgen",
[[package]]
name = "lettre"
-version = "0.11.10"
+version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0161e452348e399deb685ba05e55ee116cae9410f4f51fe42d597361444521d9"
+checksum = "ab4c9a167ff73df98a5ecc07e8bf5ce90b583665da3d1762eb1f775ad4d0d6f5"
dependencies = [
"async-trait",
"base64 0.22.1",
[[package]]
name = "libc"
-version = "0.2.167"
+version = "0.2.168"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
+checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
[[package]]
name = "libm"
[[package]]
name = "miniz_oxide"
-version = "0.8.0"
+version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1"
+checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394"
dependencies = [
"adler2",
]
"ron",
"serde",
"sqlx",
- "thiserror 2.0.4",
+ "thiserror 2.0.7",
"tokio",
"tower",
"tower-http",
[[package]]
name = "redox_syscall"
-version = "0.5.7"
+version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f"
+checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
dependencies = [
"bitflags",
]
[[package]]
name = "rustix"
-version = "0.38.41"
+version = "0.38.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6"
+checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
- "windows-sys 0.52.0",
+ "windows-sys 0.59.0",
]
[[package]]
name = "rustls"
-version = "0.23.19"
+version = "0.23.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "934b404430bb06b3fae2cba809eb45a1ab1aecd64491213d7c3301b88393f8d1"
+checksum = "5065c3f250cbd332cd894be57c40fa52387247659b14a2d6041d121547903b1b"
dependencies = [
"log",
"once_cell",
[[package]]
name = "rustls-pki-types"
-version = "1.10.0"
+version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "16f1201b3c9a7ee8039bcadc17b7e605e2945b27eee7631788c1bd2b0643674b"
+checksum = "d2bf47e6ff922db3825eb750c4e2ff784c6ff8fb9e13046ef6a1d1c5401b0b37"
[[package]]
name = "rustls-webpki"
[[package]]
name = "serde"
-version = "1.0.215"
+version = "1.0.216"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
+checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
-version = "1.0.215"
+version = "1.0.216"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
+checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
-[[package]]
-name = "sync_wrapper"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
-
[[package]]
name = "sync_wrapper"
version = "1.0.2"
[[package]]
name = "thiserror"
-version = "2.0.4"
+version = "2.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2f49a1853cf82743e3b7950f77e0f4d622ca36cf4317cba00c767838bac8d490"
+checksum = "93605438cbd668185516ab499d589afb7ee1859ea3d5fc8f6b0755e1c7443767"
dependencies = [
- "thiserror-impl 2.0.4",
+ "thiserror-impl 2.0.7",
]
[[package]]
[[package]]
name = "thiserror-impl"
-version = "2.0.4"
+version = "2.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8381894bb3efe0c4acac3ded651301ceee58a15d47c2e34885ed1908ad667061"
+checksum = "e1d8749b4531af2117677a5fcd12b1348a3fe2b81e36e61ffeac5c4aa3273e36"
dependencies = [
"proc-macro2",
"quote",
[[package]]
name = "tokio-rustls"
-version = "0.26.0"
+version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4"
+checksum = "5f6d0975eaace0cf0fcadee4e4aaa5da15b5c079146f2cffb67c113be122bf37"
dependencies = [
"rustls",
- "rustls-pki-types",
"tokio",
]
[[package]]
name = "tokio-stream"
-version = "0.1.16"
+version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1"
+checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047"
dependencies = [
"futures-core",
"pin-project-lite",
[[package]]
name = "tower"
-version = "0.5.1"
+version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f"
+checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9"
dependencies = [
"futures-core",
"futures-util",
"pin-project-lite",
- "sync_wrapper 0.1.2",
+ "sync_wrapper",
"tokio",
"tower-layer",
"tower-service",
[[package]]
name = "unicode-bidi"
-version = "0.3.17"
+version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893"
+checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5"
[[package]]
name = "unicode-ident"
[[package]]
name = "wasm-bindgen"
-version = "0.2.97"
+version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c"
+checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396"
dependencies = [
"cfg-if",
"once_cell",
[[package]]
name = "wasm-bindgen-backend"
-version = "0.2.97"
+version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd"
+checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79"
dependencies = [
"bumpalo",
"log",
- "once_cell",
"proc-macro2",
"quote",
"syn",
[[package]]
name = "wasm-bindgen-futures"
-version = "0.4.47"
+version = "0.4.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9dfaf8f50e5f293737ee323940c7d8b08a66a95a419223d9f41610ca08b0833d"
+checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2"
dependencies = [
"cfg-if",
"js-sys",
[[package]]
name = "wasm-bindgen-macro"
-version = "0.2.97"
+version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051"
+checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
[[package]]
name = "wasm-bindgen-macro-support"
-version = "0.2.97"
+version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d"
+checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
dependencies = [
"proc-macro2",
"quote",
[[package]]
name = "wasm-bindgen-shared"
-version = "0.2.97"
+version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49"
+checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6"
[[package]]
name = "web-sys"
-version = "0.3.74"
+version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c"
+checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc"
dependencies = [
"js-sys",
"wasm-bindgen",
* Node install: https://nodejs.org/en/download/
+# Tools
+Benchmarking: https://crates.io/crates/oha
+HTTP API tool: https://www.usebruno.com/
\ No newline at end of file
+* Finish updating profile
+ * check password and message error
+ * user can change email: add a field + revalidation of new email
+* Check position of message error in profile/sign in/sign up with flex grid layout
* Review the recipe model (SQL)
* Describe the use cases in details.
* Define the UI (mockups).
.service(services::webapi::set_recipe_title)
.service(services::webapi::set_recipe_description)
* Add support to translations into db model.
+* Make a Text database (a bit like d-lan.net) and think about translation.
[ok] Try using WASM for all the client logic (test on editing/creating a recipe)
[ok] How to log error to journalctl or elsewhere + debug log?
[password] TEXT NOT NULL, -- argon2(password_plain, salt).
- [creation_datetime] TEXT NOT NULL, -- Updated when the validation email is sent.
+ [validation_token_datetime] TEXT NOT NULL, -- Updated when the validation email is sent.
[validation_token] TEXT, -- If not null then the user has not validated his account yet.
[password_reset_token] TEXT, -- If not null then the user can reset its password.
UserCreatedWaitingForValidation(String), // Validation token.
}
+#[derive(Debug)]
+pub enum UpdateUserResult {
+ EmailAlreadyTaken,
+ UserUpdatedWaitingForRevalidation(String), // Validation token.
+ Ok,
+}
+
#[derive(Debug)]
pub enum ValidationResult {
UnknownUser,
Self::new_from_file(path).await
}
- // For tests.
- #[warn(dead_code)]
+ #[cfg(test)]
pub async fn new_in_memory() -> Result<Connection> {
Self::create_connection(SqlitePoolOptions::new().connect("sqlite::memory:").await?).await
}
.map_err(DBError::from)
}
- // For tests.
- #[warn(dead_code)]
+ #[cfg(test)]
pub async fn get_user_login_info(&self, token: &str) -> Result<model::UserLoginInfo> {
sqlx::query_as(
r#"
.map_err(DBError::from)
}
+ /// If a new email is given and it doesn't match the current one then it has to be
+ /// Revalidated.
pub async fn update_user(
&self,
user_id: i64,
new_email: Option<&str>,
new_name: Option<&str>,
new_password: Option<&str>,
- ) -> Result<()> {
+ ) -> Result<UpdateUserResult> {
let mut tx = self.tx().await?;
let hashed_new_password = new_password.map(|p| hash(p).unwrap());
- let (email, name, password) = sqlx::query_as::<_, (String, String, String)>(
+ let (email, name, hashed_password) = sqlx::query_as::<_, (String, String, String)>(
"SELECT [email], [name], [password] FROM [User] WHERE [id] = $1",
)
.bind(user_id)
.fetch_one(&mut *tx)
.await?;
+ let email_changed = new_email.is_some_and(|new_email| new_email != email);
+
+ // Check if email not already taken.
+ let validation_token = if email_changed {
+ if sqlx::query_scalar::<_, i64>(
+ r#"
+SELECT COUNT(*)
+FROM [User]
+WHERE [email] = $1
+ "#,
+ )
+ .bind(new_email.unwrap())
+ .fetch_one(&mut *tx)
+ .await?
+ > 0
+ {
+ return Ok(UpdateUserResult::EmailAlreadyTaken);
+ }
+
+ let token = Some(generate_token());
+ sqlx::query(
+ r#"
+UPDATE [User]
+SET [validation_token] = $2, [validation_token_datetime] = $3
+WHERE [id] = $1
+ "#,
+ )
+ .bind(user_id)
+ .bind(&token)
+ .bind(Utc::now())
+ .execute(&mut *tx)
+ .await?;
+ token
+ } else {
+ None
+ };
+
sqlx::query(
r#"
UPDATE [User]
.bind(user_id)
.bind(new_email.unwrap_or(&email))
.bind(new_name.unwrap_or(&name))
- .bind(hashed_new_password.unwrap_or(password))
+ .bind(hashed_new_password.unwrap_or(hashed_password))
.execute(&mut *tx)
.await?;
tx.commit().await?;
- Ok(())
+ Ok(if let Some(validation_token) = validation_token {
+ UpdateUserResult::UserUpdatedWaitingForRevalidation(validation_token)
+ } else {
+ UpdateUserResult::Ok
+ })
}
pub async fn sign_up(&self, email: &str, password: &str) -> Result<SignUpResult> {
sqlx::query(
r#"
UPDATE [User]
-SET [validation_token] = $2, [creation_datetime] = $3, [password] = $4
+SET [validation_token] = $2, [validation_token_datetime] = $3, [password] = $4
WHERE [id] = $1
"#,
)
sqlx::query(
r#"
INSERT INTO [User]
-([email], [validation_token], [creation_datetime], [password])
+([email], [validation_token], [validation_token_datetime], [password])
VALUES ($1, $2, $3, $4)
"#,
)
// There is no index on [validation_token]. Is it useful?
let user_id = match sqlx::query_as::<_, (i64, DateTime<Utc>)>(
- "SELECT [id], [creation_datetime] FROM [User] WHERE [validation_token] = $1",
+ "SELECT [id], [validation_token_datetime] FROM [User] WHERE [validation_token] = $1",
)
.bind(token)
.fetch_optional(&mut *tx)
.await?
{
- Some((id, creation_datetime)) => {
- if Utc::now() - creation_datetime > validation_time {
+ Some((id, validation_token_datetime)) => {
+ if Utc::now() - validation_token_datetime > validation_time {
return Ok(ValidationResult::ValidationExpired);
}
sqlx::query("UPDATE [User] SET [validation_token] = NULL WHERE [id] = $1")
SELECT [password_reset_datetime]
FROM [User]
WHERE [email] = $1
- "#,
+ "#,
)
.bind(email)
.fetch_optional(&mut *tx)
SELECT [id], [password_reset_datetime]
FROM [User]
WHERE [password_reset_token] = $1
- "#,
+ "#,
)
.bind(token)
.fetch_one(&mut *tx)
UPDATE [User]
SET [password] = $2, [password_reset_token] = NULL, [password_reset_datetime] = NULL
WHERE [id] = $1
- "#,
+ "#,
)
.bind(user_id)
.bind(hashed_new_password)
sqlx::query(
r#"
INSERT INTO
- [User] ([id], [email], [name], [password], [creation_datetime], [validation_token])
+ [User] ([id], [email], [name], [password], [validation_token_datetime], [validation_token])
VALUES (
1,
'paul@atreides.com',
sqlx::query(
r#"
INSERT INTO [User]
- ([id], [email], [name], [password], [creation_datetime], [validation_token])
+ ([id], [email], [name], [password], [validation_token_datetime], [validation_token])
VALUES (
1,
'paul@atreides.com',
};
// Validation.
- let (authentication_token, user_id) = match connection
+ let (authentication_token, _user_id) = match connection
.validation(
&validation_token,
Duration::hours(1),
sqlx::query(
r#"
INSERT INTO [User]
- ([id], [email], [name], [password], [creation_datetime], [validation_token])
+ ([id], [email], [name], [password], [validation_token_datetime], [validation_token])
VALUES
($1, $2, $3, $4, $5, $6)
"#
assert_eq!(user.name, "paul");
assert_eq!(user.email, "paul@atreides.com");
- connection
+ if let UpdateUserResult::UserUpdatedWaitingForRevalidation(token) = connection
.update_user(
1,
Some("muaddib@fremen.com"),
Some("muaddib"),
Some("Chani"),
)
- .await?;
+ .await?
+ {
+ let (_authentication_token_1, user_id_1) = match connection
+ .validation(&token, Duration::hours(1), "127.0.0.1", "Mozilla/5.0")
+ .await?
+ {
+ ValidationResult::Ok(token, user_id) => (token, user_id),
+ other => panic!("{:?}", other),
+ };
+ assert_eq!(user_id_1, 1);
+ } else {
+ panic!("A revalidation token must be created when changin e-mail");
+ }
let user = connection.load_user(1).await?.unwrap();
assert_eq!(user.name, "muaddib");
assert_eq!(user.email, "muaddib@fremen.com");
- // Tets if password has been updated correctly.
+ // Tests if password has been updated correctly.
if let SignInResult::Ok(_token, id) = connection
.sign_in("muaddib@fremen.com", "Chani", "127.0.0.1", "Mozilla/5.0")
.await?
sqlx::query(
r#"
INSERT INTO [User]
- ([id], [email], [name], [password], [creation_datetime], [validation_token])
+ ([id], [email], [name], [password], [validation_token_datetime], [validation_token])
VALUES
($1, $2, $3, $4, $5, $6)
"#
#[derive(Template)]
#[template(path = "message.html")]
-pub struct MessageTemplate<'a> {
+pub struct MessageTemplate {
pub user: Option<model::User>,
- pub message: &'a str,
+ pub message: String,
pub as_code: bool, // Display the message in <pre> markup.
}
+impl MessageTemplate {
+ pub fn new(message: &str) -> MessageTemplate {
+ MessageTemplate {
+ user: None,
+ message: message.to_string(),
+ as_code: false,
+ }
+ }
+
+ pub fn new_with_user(message: &str, user: Option<model::User>) -> MessageTemplate {
+ MessageTemplate {
+ user,
+ message: message.to_string(),
+ as_code: false,
+ }
+ }
+}
+
#[derive(Template)]
#[template(path = "sign_up_form.html")]
pub struct SignUpFormTemplate {
#[template(path = "profile.html")]
pub struct ProfileTemplate {
pub user: Option<model::User>,
+ pub username: String,
+ pub email: String,
+ pub message: String,
+ pub message_email: String,
+ pub message_password: String,
}
http::StatusCode,
middleware::{self, Next},
response::{Response, Result},
- routing::{get, put},
+ routing::get,
Router,
};
use axum_extra::extract::cookie::CookieJar;
db_connection,
};
- // TODO: Add fallback fo ron_api_routes.
let ron_api_routes = Router::new()
- .route("/user/update", put(services::ron::update_user))
+ // Disabled: update user profile is now made with a post data ('edit_user_post').
+ // .route("/user/update", put(services::ron::update_user))
.fallback(services::ron::not_found);
let html_routes = Router::new()
get(services::sign_up_get).post(services::sign_up_post),
)
.route("/validation", get(services::sign_up_validation))
+ .route("/revalidation", get(services::email_revalidation))
.route(
"/signin",
get(services::sign_in_get).post(services::sign_in_post),
// Recipes.
.route("/recipe/view/:id", get(services::view_recipe))
// User.
- .route("/user/edit", get(services::edit_user))
+ .route(
+ "/user/edit",
+ get(services::edit_user_get).post(services::edit_user_post),
+ )
.route_layer(middleware::from_fn(services::ron_error_to_html));
let app = Router::new()
}
#[derive(Parser, Debug)]
+#[command(
+ author = "Greg Burri",
+ version = "1.0",
+ about = "A little cooking recipes website"
+)]
struct Args {
/// Will clear the database and insert some test data. (A backup is made first).
#[arg(long)]
use axum::{
- async_trait,
body::Bytes,
- extract::{FromRequest, Request},
http::{header, HeaderValue, StatusCode},
response::{IntoResponse, Response},
};
};
return Ok(MessageTemplate {
user: None,
- message: &message,
+ message,
as_code: true,
}
.into_response());
}
}
-///// MESSAGE /////
-
-impl<'a> MessageTemplate<'a> {
- pub fn new(message: &'a str) -> MessageTemplate<'a> {
- MessageTemplate {
- user: None,
- message,
- as_code: false,
- }
- }
-
- pub fn new_with_user(message: &'a str, user: Option<model::User>) -> MessageTemplate<'a> {
- MessageTemplate {
- user,
- message,
- as_code: false,
- }
- }
-}
-
//// SIGN UP /////
#[debug_handler]
}
Ok(db::SignUpResult::UserCreatedWaitingForValidation(token)) => {
let url = utils::get_url_from_host(&host);
-
let email = form_data.email.clone();
match email::send_email(
&email,
{
Ok(()) => Ok(
MessageTemplate::new_with_user(
- "An email has been sent, follow the link to validate your account.",
+ "An email has been sent, follow the link to validate your account",
user).into_response()),
Err(_) => {
// error!("Email validation error: {}", error); // TODO: log
}
}
Err(_) => {
- // error!("Signup database error: {}", error);
+ // error!("Signup database error: {}", error); // TODO: log
error_response(SignUpError::DatabaseError, &form_data, user)
}
}
///// EDIT PROFILE /////
#[debug_handler]
-pub async fn edit_user(
+pub async fn edit_user_get(Extension(user): Extension<Option<model::User>>) -> Response {
+ if let Some(user) = user {
+ ProfileTemplate {
+ username: user.name.clone(),
+ email: user.email.clone(),
+ user: Some(user),
+ message: String::new(),
+ message_email: String::new(),
+ message_password: String::new(),
+ }
+ .into_response()
+ } else {
+ MessageTemplate::new("Not logged in").into_response()
+ }
+}
+
+#[derive(Deserialize, Debug)]
+pub struct EditUserForm {
+ name: String,
+ email: String,
+ password_1: String,
+ password_2: String,
+}
+enum ProfileUpdateError {
+ InvalidEmail,
+ EmailAlreadyTaken,
+ PasswordsNotEqual,
+ InvalidPassword,
+ DatabaseError,
+ UnableSendEmail,
+}
+
+// TODO: A lot of code are similar to 'sign_up_post', maybe find a way to factorize some.
+#[debug_handler(state = AppState)]
+pub async fn edit_user_post(
+ Host(host): Host,
State(connection): State<db::Connection>,
+ State(config): State<Config>,
Extension(user): Extension<Option<model::User>>,
-) -> Response {
- if user.is_some() {
- ProfileTemplate { user }.into_response()
+ Form(form_data): Form<EditUserForm>,
+) -> Result<Response> {
+ if let Some(user) = user {
+ fn error_response(
+ error: ProfileUpdateError,
+ form_data: &EditUserForm,
+ user: model::User,
+ ) -> Result<Response> {
+ Ok(ProfileTemplate {
+ user: Some(user),
+ username: form_data.name.clone(),
+ email: form_data.email.clone(),
+ message_email: match error {
+ ProfileUpdateError::InvalidEmail => "Invalid email",
+ ProfileUpdateError::EmailAlreadyTaken => "Email already taken",
+ _ => "",
+ }
+ .to_string(),
+ message_password: match error {
+ ProfileUpdateError::PasswordsNotEqual => "Passwords don't match",
+ ProfileUpdateError::InvalidPassword => {
+ "Password must have at least eight characters"
+ }
+ _ => "",
+ }
+ .to_string(),
+ message: match error {
+ ProfileUpdateError::DatabaseError => "Database error",
+ ProfileUpdateError::UnableSendEmail => "Unable to send the validation email",
+ _ => "",
+ }
+ .to_string(),
+ }
+ .into_response())
+ }
+
+ if let common::utils::EmailValidation::NotValid =
+ common::utils::validate_email(&form_data.email)
+ {
+ return error_response(ProfileUpdateError::InvalidEmail, &form_data, user);
+ }
+
+ let new_password = if !form_data.password_1.is_empty() || !form_data.password_2.is_empty() {
+ if form_data.password_1 != form_data.password_2 {
+ return error_response(ProfileUpdateError::PasswordsNotEqual, &form_data, user);
+ }
+ if let common::utils::PasswordValidation::TooShort =
+ common::utils::validate_password(&form_data.password_1)
+ {
+ return error_response(ProfileUpdateError::InvalidPassword, &form_data, user);
+ }
+ Some(form_data.password_1.as_ref())
+ } else {
+ None
+ };
+
+ let email_trimmed = form_data.email.trim();
+ let message: &str;
+
+ match connection
+ .update_user(
+ user.id,
+ Some(&email_trimmed),
+ Some(&form_data.name),
+ new_password,
+ )
+ .await
+ {
+ Ok(db::UpdateUserResult::EmailAlreadyTaken) => {
+ return error_response(ProfileUpdateError::EmailAlreadyTaken, &form_data, user);
+ }
+ Ok(db::UpdateUserResult::UserUpdatedWaitingForRevalidation(token)) => {
+ let url = utils::get_url_from_host(&host);
+ let email = form_data.email.clone();
+ match email::send_email(
+ &email,
+ &format!(
+ "Follow this link to validate this email address: {}/revalidation?validation_token={}",
+ url, token
+ ),
+ &config.smtp_relay_address,
+ &config.smtp_login,
+ &config.smtp_password,
+ )
+ .await
+ {
+ Ok(()) => {
+ message =
+ "An email has been sent, follow the link to validate your new email";
+ }
+ Err(_) => {
+ // error!("Email validation error: {}", error); // TODO: log
+ return error_response(ProfileUpdateError::UnableSendEmail, &form_data, user);
+ }
+ }
+ }
+ Ok(db::UpdateUserResult::Ok) => {
+ message = "Profile saved";
+ }
+ Err(_) => return error_response(ProfileUpdateError::DatabaseError, &form_data, user),
+ }
+
+ // Reload after update.
+ let user = connection.load_user(user.id).await?;
+
+ Ok(ProfileTemplate {
+ user,
+ username: form_data.name,
+ email: form_data.email,
+ message: message.to_string(),
+ message_email: String::new(),
+ message_password: String::new(),
+ }
+ .into_response())
} else {
- MessageTemplate::new("Not logged in").into_response()
+ Ok(MessageTemplate::new("Not logged in").into_response())
+ }
+}
+
+#[debug_handler]
+pub async fn email_revalidation(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ConnectInfo(addr): ConnectInfo<SocketAddr>,
+ Query(query): Query<HashMap<String, String>>,
+ headers: HeaderMap,
+) -> Result<(CookieJar, impl IntoResponse)> {
+ let mut jar = CookieJar::from_headers(&headers);
+ if user.is_some() {
+ return Ok((
+ jar,
+ MessageTemplate::new_with_user("User already exists", user),
+ ));
+ }
+ let (client_ip, client_user_agent) = utils::get_ip_and_user_agent(&headers, addr);
+ match query.get("validation_token") {
+ // 'validation_token' exists only when a user must validate a new email.
+ Some(token) => {
+ match connection
+ .validation(
+ token,
+ Duration::seconds(consts::VALIDATION_TOKEN_DURATION),
+ &client_ip,
+ &client_user_agent,
+ )
+ .await?
+ {
+ db::ValidationResult::Ok(token, user_id) => {
+ let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token);
+ jar = jar.add(cookie);
+ let user = connection.load_user(user_id).await?;
+ Ok((
+ jar,
+ MessageTemplate::new_with_user("Email validation successful", user),
+ ))
+ }
+ db::ValidationResult::ValidationExpired => Ok((
+ jar,
+ MessageTemplate::new_with_user(
+ "The validation has expired. Try to sign up again with the same email",
+ user,
+ ),
+ )),
+ db::ValidationResult::UnknownUser => Ok((
+ jar,
+ MessageTemplate::new_with_user(
+ "Validation error. Try to sign up again with the same email",
+ user,
+ ),
+ )),
+ }
+ }
+ None => Ok((
+ jar,
+ MessageTemplate::new_with_user("Validation error", user),
+ )),
}
}
http::StatusCode,
response::{ErrorResponse, IntoResponse, Result},
};
-use tracing::{event, Level};
+// use tracing::{event, Level};
use crate::{data::db, model, ron_extractor::ExtractRon, ron_utils::ron_error};
+#[allow(dead_code)]
#[debug_handler]
pub async fn update_user(
State(connection): State<db::Connection>,
connection
.update_user(
user.id,
- ron.email.as_deref(),
+ ron.email.as_deref().map(str::trim),
ron.name.as_deref(),
ron.password.as_deref(),
)
///// 404 /////
#[debug_handler]
-pub async fn not_found(Extension(user): Extension<Option<model::User>>) -> impl IntoResponse {
+pub async fn not_found(Extension(_user): Extension<Option<model::User>>) -> impl IntoResponse {
ron_error(StatusCode::NOT_FOUND, "Not found")
}
{% match user %}
{% when Some with (user) %}
<a class="create-recipe" href="/recipe/new" >Create a new recipe</a>
- <span><a href="/user/edit">{{ user.email }}</a> / <a href="/signout" />Sign out</a></span>
+ <span><a href="/user/edit">
+ {% if user.name == "" %}
+ {{ user.email }}
+ {% else %}
+ {{ user.name }}
+ {% endif %}
+ </a> / <a href="/signout" />Sign out</a></span>
{% when None %}
<span>
<a href="/signin" >Sign in</a>/<a href="/signup">Sign up</a>/<a href="/ask_reset_password">Lost password</a>
<h1>Profile</h1>
- <form id="user-edit">
+ <form action="/user/edit" method="post">
<label for="input-name">Name</label>
<input
id="input-name"
type="text"
name="name"
- value="{{ user.name }}"
+ value="{{ username }}"
autocapitalize="none"
autocomplete="title"
autofocus="autofocus" />
+ <label for="input-email">Email (need to be revalidated if changed)</label>
+ <input id="input-email" type="email" name="email" value="{{ email }}" autocapitalize="none" autocomplete="email" autofocus="autofocus" />
+
+ {{ message_email }}
<label for="input-password-1">New password (minimum 8 characters)</label>
- <input id="input-password-1" type="password" name="password_1" />
+ <input id="input-password-1" type="password" name="password_1" autocomplete="new-password" />
<label for="input-password-2">Re-enter password</label>
- <input id="input-password-2" type="password" name="password_2" />
+ <input id="input-password-2" type="password" name="password_2" autocomplete="new-password" />
- <input type="button" value="Save" />
- </form>
+ {{ message_password }}
+ <input type="submit" name="commit" value="Save" />
+ </form>
+ {{ message }}
</div>
{% when None %}
<form action="/signup" method="post">
<label for="input-email">Your email address</label>
<input id="input-email" type="email" name="email" value="{{ email }}" autocapitalize="none" autocomplete="email" autofocus="autofocus" />
+
{{ message_email }}
<label for="input-password-1">Choose a password (minimum 8 characters)</label>
-use ron::{
- de::from_bytes,
- ser::{to_string_pretty, PrettyConfig},
-};
-use serde::{de::DeserializeOwned, Deserialize, Serialize};
+use ron::ser::{to_string_pretty, PrettyConfig};
+use serde::{Deserialize, Serialize};
///// RECIPE /////
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
- // let body = document.body().expect("document should have a body");
let location = window.location().pathname()?;
let path: Vec<&str> = location.split('/').skip(1).collect();
- /*
- * TODO:
- * [ok] get url (/recipe/edit/{id}) and extract the id
- * - Add a handle (event?) to the title field (when edited/changed?):
- * - Call (as AJAR) /ron-api/set-title and set the body to a serialized RON of the type common::ron_api::SetTitle
- * - Display error message if needed
- */
match path[..] {
["recipe", "edit", id] => {
let id = id.parse::<i64>().unwrap(); // TODO: remove unwrap.
handles::recipe_edit(document)?;
}
- ["user", "edit"] => {
- handles::user_edit(document)?;
- }
+ // Disable: user editing data are now submitted as classic form data.
+ // ["user", "edit"] => {
+ // handles::user_edit(document)?;
+ // }
_ => (),
}
- // TEST
- // let val = document.create_element("p")?;
- // val.set_inner_html("Hello from Rust!");
- // body.append_child(&val)?;
-
Ok(())
}