Add a toggle between dark and light theme
This commit is contained in:
parent
d22617538e
commit
559ed139aa
34 changed files with 640 additions and 469 deletions
|
|
@ -11,7 +11,7 @@ use axum::{
|
|||
};
|
||||
use axum_extra::extract::{
|
||||
Host, Query,
|
||||
cookie::{Cookie, CookieJar},
|
||||
cookie::{self, Cookie, CookieJar},
|
||||
};
|
||||
use chrono::Duration;
|
||||
use lettre::Address;
|
||||
|
|
@ -19,14 +19,8 @@ use serde::Deserialize;
|
|||
use tracing::{Level, event};
|
||||
|
||||
use crate::{
|
||||
AppState, Result,
|
||||
config::Config,
|
||||
consts,
|
||||
data::{db, model},
|
||||
email,
|
||||
html_templates::*,
|
||||
translation::{self, Sentence},
|
||||
utils,
|
||||
AppState, Context, Result, config::Config, consts, data::db, email, html_templates::*,
|
||||
translation::Sentence, utils,
|
||||
};
|
||||
|
||||
/// SIGN UP ///
|
||||
|
|
@ -34,14 +28,12 @@ use crate::{
|
|||
#[debug_handler]
|
||||
pub async fn sign_up_get(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
) -> Result<Response> {
|
||||
if connection.get_new_user_registration_enabled().await? {
|
||||
Ok(Html(
|
||||
SignUpFormTemplate {
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
email: String::new(),
|
||||
message: "",
|
||||
message_email: "",
|
||||
|
|
@ -51,10 +43,15 @@ pub async fn sign_up_get(
|
|||
)
|
||||
.into_response())
|
||||
} else {
|
||||
Ok(
|
||||
Html(MessageTemplate::new_with_user(tr.t(Sentence::SignUpClosed), tr, user).render()?)
|
||||
.into_response(),
|
||||
Ok(Html(
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::SignUpClosed),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,40 +76,37 @@ pub async fn sign_up_post(
|
|||
Host(host): Host,
|
||||
State(connection): State<db::Connection>,
|
||||
State(config): State<Config>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
Form(form_data): Form<SignUpFormData>,
|
||||
) -> Result<Response> {
|
||||
fn error_response(
|
||||
error: SignUpError,
|
||||
form_data: &SignUpFormData,
|
||||
user: Option<model::User>,
|
||||
tr: translation::Tr,
|
||||
context: Context,
|
||||
) -> Result<Response> {
|
||||
let invalid_password_mess = &tr.tp(
|
||||
let invalid_password_mess = &context.tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
);
|
||||
Ok(Html(
|
||||
SignUpFormTemplate {
|
||||
user,
|
||||
email: form_data.email.clone(),
|
||||
message_email: match error {
|
||||
SignUpError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
SignUpError::InvalidEmail => context.tr.t(Sentence::InvalidEmail),
|
||||
_ => "",
|
||||
},
|
||||
message_password: match error {
|
||||
SignUpError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
SignUpError::PasswordsNotEqual => context.tr.t(Sentence::PasswordDontMatch),
|
||||
SignUpError::InvalidPassword => invalid_password_mess,
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
SignUpError::UserAlreadyExists => tr.t(Sentence::EmailAlreadyTaken),
|
||||
SignUpError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
SignUpError::UnableSendEmail => tr.t(Sentence::UnableToSendEmail),
|
||||
SignUpError::UserAlreadyExists => context.tr.t(Sentence::EmailAlreadyTaken),
|
||||
SignUpError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
||||
SignUpError::UnableSendEmail => context.tr.t(Sentence::UnableToSendEmail),
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
context,
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
|
|
@ -121,24 +115,29 @@ pub async fn sign_up_post(
|
|||
|
||||
if !connection.get_new_user_registration_enabled().await? {
|
||||
return Ok(Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::SignUpClosed), tr, user).render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::SignUpClosed),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response());
|
||||
}
|
||||
|
||||
// Validation of email and password.
|
||||
if form_data.email.parse::<Address>().is_err() {
|
||||
return error_response(SignUpError::InvalidEmail, &form_data, user, tr);
|
||||
return error_response(SignUpError::InvalidEmail, &form_data, context);
|
||||
}
|
||||
|
||||
if form_data.password_1 != form_data.password_2 {
|
||||
return error_response(SignUpError::PasswordsNotEqual, &form_data, user, tr);
|
||||
return error_response(SignUpError::PasswordsNotEqual, &form_data, context);
|
||||
}
|
||||
|
||||
if let common::utils::PasswordValidation::TooShort =
|
||||
common::utils::validate_password(&form_data.password_1)
|
||||
{
|
||||
return error_response(SignUpError::InvalidPassword, &form_data, user, tr);
|
||||
return error_response(SignUpError::InvalidPassword, &form_data, context);
|
||||
}
|
||||
|
||||
match connection
|
||||
|
|
@ -146,14 +145,14 @@ pub async fn sign_up_post(
|
|||
.await
|
||||
{
|
||||
Ok(db::user::SignUpResult::UserAlreadyExists) => {
|
||||
error_response(SignUpError::UserAlreadyExists, &form_data, user, tr)
|
||||
error_response(SignUpError::UserAlreadyExists, &form_data, context)
|
||||
}
|
||||
Ok(db::user::SignUpResult::UserCreatedWaitingForValidation(token)) => {
|
||||
let url = utils::get_url_from_host(&host);
|
||||
let email = form_data.email.clone();
|
||||
match email::send_email(
|
||||
&email,
|
||||
&tr.tp(
|
||||
&context.tr.tp(
|
||||
Sentence::SignUpFollowEmailLink,
|
||||
&[Box::new(format!(
|
||||
"{}/validation?validation_token={}",
|
||||
|
|
@ -167,19 +166,23 @@ pub async fn sign_up_post(
|
|||
.await
|
||||
{
|
||||
Ok(()) => Ok(Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::SignUpEmailSent), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::SignUpEmailSent),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response()),
|
||||
Err(_) => {
|
||||
// error!("Email validation error: {}", error); // TODO: log
|
||||
error_response(SignUpError::UnableSendEmail, &form_data, user, tr)
|
||||
error_response(SignUpError::UnableSendEmail, &form_data, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
// error!("Signup database error: {}", error); // TODO: log
|
||||
error_response(SignUpError::DatabaseError, &form_data, user, tr)
|
||||
error_response(SignUpError::DatabaseError, &form_data, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -187,21 +190,20 @@ pub async fn sign_up_post(
|
|||
#[debug_handler]
|
||||
pub async fn sign_up_validation(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
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() {
|
||||
if context.user.is_some() {
|
||||
return Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::ValidationUserAlreadyExists),
|
||||
tr,
|
||||
user,
|
||||
context.tr.t(Sentence::ValidationUserAlreadyExists),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
|
|
@ -221,15 +223,16 @@ pub async fn sign_up_validation(
|
|||
.await?
|
||||
{
|
||||
db::user::ValidationResult::Ok(token, user_id) => {
|
||||
let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token);
|
||||
let cookie = Cookie::build((consts::COOKIE_AUTH_TOKEN_NAME, token))
|
||||
.same_site(cookie::SameSite::Strict);
|
||||
jar = jar.add(cookie);
|
||||
let user = connection.load_user(user_id).await?;
|
||||
Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::SignUpEmailValidationSuccess),
|
||||
tr,
|
||||
context.tr.t(Sentence::SignUpEmailValidationSuccess),
|
||||
context.tr,
|
||||
user,
|
||||
)
|
||||
.render()?,
|
||||
|
|
@ -240,9 +243,9 @@ pub async fn sign_up_validation(
|
|||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::SignUpValidationExpired),
|
||||
tr,
|
||||
user,
|
||||
context.tr.t(Sentence::SignUpValidationExpired),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
|
|
@ -251,9 +254,9 @@ pub async fn sign_up_validation(
|
|||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::SignUpValidationErrorTryAgain),
|
||||
tr,
|
||||
user,
|
||||
context.tr.t(Sentence::SignUpValidationErrorTryAgain),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
|
|
@ -263,8 +266,12 @@ pub async fn sign_up_validation(
|
|||
None => Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationError), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::ValidationError),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
)),
|
||||
}
|
||||
|
|
@ -273,14 +280,10 @@ pub async fn sign_up_validation(
|
|||
/// SIGN IN ///
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn sign_in_get(
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
pub async fn sign_in_get(Extension(context): Extension<Context>) -> Result<impl IntoResponse> {
|
||||
Ok(Html(
|
||||
SignInFormTemplate {
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
email: "",
|
||||
message: "",
|
||||
}
|
||||
|
|
@ -298,8 +301,7 @@ pub struct SignInFormData {
|
|||
pub async fn sign_in_post(
|
||||
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
headers: HeaderMap,
|
||||
Form(form_data): Form<SignInFormData>,
|
||||
) -> Result<(CookieJar, Response)> {
|
||||
|
|
@ -319,10 +321,9 @@ pub async fn sign_in_post(
|
|||
jar,
|
||||
Html(
|
||||
SignInFormTemplate {
|
||||
user,
|
||||
email: &form_data.email,
|
||||
message: tr.t(Sentence::AccountMustBeValidatedFirst),
|
||||
tr,
|
||||
message: context.tr.t(Sentence::AccountMustBeValidatedFirst),
|
||||
context,
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
|
|
@ -332,20 +333,20 @@ pub async fn sign_in_post(
|
|||
jar,
|
||||
Html(
|
||||
SignInFormTemplate {
|
||||
user,
|
||||
email: &form_data.email,
|
||||
message: tr.t(Sentence::WrongEmailOrPassword),
|
||||
tr,
|
||||
message: context.tr.t(Sentence::WrongEmailOrPassword),
|
||||
context,
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
.into_response(),
|
||||
)),
|
||||
db::user::SignInResult::Ok(token, _user_id) => {
|
||||
let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token);
|
||||
let cookie = Cookie::build((consts::COOKIE_AUTH_TOKEN_NAME, token))
|
||||
.same_site(cookie::SameSite::Strict);
|
||||
Ok((
|
||||
jar.add(cookie),
|
||||
Redirect::to(&format!("/{}/", tr.current_lang_code())).into_response(),
|
||||
Redirect::to(&format!("/{}/", context.tr.current_lang_code())).into_response(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
@ -356,7 +357,7 @@ pub async fn sign_in_post(
|
|||
#[debug_handler]
|
||||
pub async fn sign_out(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
req: Request<Body>,
|
||||
) -> Result<(CookieJar, Redirect)> {
|
||||
let mut jar = CookieJar::from_headers(req.headers());
|
||||
|
|
@ -365,27 +366,30 @@ pub async fn sign_out(
|
|||
jar = jar.remove(consts::COOKIE_AUTH_TOKEN_NAME);
|
||||
connection.sign_out(&token).await?;
|
||||
}
|
||||
Ok((jar, Redirect::to(&format!("/{}/", tr.current_lang_code()))))
|
||||
Ok((
|
||||
jar,
|
||||
Redirect::to(&format!("/{}/", context.tr.current_lang_code())),
|
||||
))
|
||||
}
|
||||
|
||||
/// RESET PASSWORD ///
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn ask_reset_password_get(
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
) -> Result<Response> {
|
||||
if user.is_some() {
|
||||
pub async fn ask_reset_password_get(Extension(context): Extension<Context>) -> Result<Response> {
|
||||
if context.user.is_some() {
|
||||
Ok(Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::AskResetAlreadyLoggedInError), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::AskResetAlreadyLoggedInError),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response())
|
||||
} else {
|
||||
Ok(Html(
|
||||
AskResetPasswordTemplate {
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
email: "",
|
||||
message: "",
|
||||
message_email: "",
|
||||
|
|
@ -414,36 +418,33 @@ pub async fn ask_reset_password_post(
|
|||
Host(host): Host,
|
||||
State(connection): State<db::Connection>,
|
||||
State(config): State<Config>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
Form(form_data): Form<AskResetPasswordForm>,
|
||||
) -> Result<Response> {
|
||||
fn error_response(
|
||||
error: AskResetPasswordError,
|
||||
email: &str,
|
||||
user: Option<model::User>,
|
||||
tr: translation::Tr,
|
||||
context: Context,
|
||||
) -> Result<Response> {
|
||||
Ok(Html(
|
||||
AskResetPasswordTemplate {
|
||||
user,
|
||||
email,
|
||||
message_email: match error {
|
||||
AskResetPasswordError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
AskResetPasswordError::InvalidEmail => context.tr.t(Sentence::InvalidEmail),
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
AskResetPasswordError::EmailAlreadyReset => {
|
||||
tr.t(Sentence::AskResetEmailAlreadyResetError)
|
||||
context.tr.t(Sentence::AskResetEmailAlreadyResetError)
|
||||
}
|
||||
AskResetPasswordError::EmailUnknown => tr.t(Sentence::EmailUnknown),
|
||||
AskResetPasswordError::EmailUnknown => context.tr.t(Sentence::EmailUnknown),
|
||||
AskResetPasswordError::UnableSendEmail => {
|
||||
tr.t(Sentence::UnableToSendResetEmail)
|
||||
context.tr.t(Sentence::UnableToSendResetEmail)
|
||||
}
|
||||
AskResetPasswordError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
AskResetPasswordError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
context,
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
|
|
@ -455,8 +456,7 @@ pub async fn ask_reset_password_post(
|
|||
return error_response(
|
||||
AskResetPasswordError::InvalidEmail,
|
||||
&form_data.email,
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -470,20 +470,18 @@ pub async fn ask_reset_password_post(
|
|||
Ok(db::user::GetTokenResetPasswordResult::PasswordAlreadyReset) => error_response(
|
||||
AskResetPasswordError::EmailAlreadyReset,
|
||||
&form_data.email,
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
),
|
||||
Ok(db::user::GetTokenResetPasswordResult::EmailUnknown) => error_response(
|
||||
AskResetPasswordError::EmailUnknown,
|
||||
&form_data.email,
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
),
|
||||
Ok(db::user::GetTokenResetPasswordResult::Ok(token)) => {
|
||||
let url = utils::get_url_from_host(&host);
|
||||
match email::send_email(
|
||||
&form_data.email,
|
||||
&tr.tp(
|
||||
&context.tr.tp(
|
||||
Sentence::AskResetFollowEmailLink,
|
||||
&[Box::new(format!(
|
||||
"{}/reset_password?reset_token={}",
|
||||
|
|
@ -497,8 +495,12 @@ pub async fn ask_reset_password_post(
|
|||
.await
|
||||
{
|
||||
Ok(()) => Ok(Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::AskResetEmailSent), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::AskResetEmailSent),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response()),
|
||||
Err(_) => {
|
||||
|
|
@ -506,8 +508,7 @@ pub async fn ask_reset_password_post(
|
|||
error_response(
|
||||
AskResetPasswordError::UnableSendEmail,
|
||||
&form_data.email,
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -517,8 +518,7 @@ pub async fn ask_reset_password_post(
|
|||
error_response(
|
||||
AskResetPasswordError::DatabaseError,
|
||||
&form_data.email,
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -527,8 +527,7 @@ pub async fn ask_reset_password_post(
|
|||
#[debug_handler]
|
||||
pub async fn reset_password_get(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
Query(query): Query<HashMap<String, String>>,
|
||||
) -> Result<Response> {
|
||||
if let Some(reset_token) = query.get("reset_token") {
|
||||
|
|
@ -542,8 +541,7 @@ pub async fn reset_password_get(
|
|||
{
|
||||
Ok(Html(
|
||||
ResetPasswordTemplate {
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
reset_token,
|
||||
message: "",
|
||||
message_password: "",
|
||||
|
|
@ -553,15 +551,23 @@ pub async fn reset_password_get(
|
|||
.into_response())
|
||||
} else {
|
||||
Ok(Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::AskResetTokenMissing), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::AskResetTokenMissing),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response())
|
||||
}
|
||||
} else {
|
||||
Ok(Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::AskResetTokenMissing), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::AskResetTokenMissing),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response())
|
||||
}
|
||||
|
|
@ -584,35 +590,36 @@ enum ResetPasswordError {
|
|||
#[debug_handler]
|
||||
pub async fn reset_password_post(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
Form(form_data): Form<ResetPasswordForm>,
|
||||
) -> Result<Response> {
|
||||
fn error_response(
|
||||
error: ResetPasswordError,
|
||||
form_data: &ResetPasswordForm,
|
||||
user: Option<model::User>,
|
||||
tr: translation::Tr,
|
||||
context: Context,
|
||||
) -> Result<Response> {
|
||||
let reset_password_mess = &tr.tp(
|
||||
let reset_password_mess = &context.tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
);
|
||||
Ok(Html(
|
||||
ResetPasswordTemplate {
|
||||
user,
|
||||
reset_token: &form_data.reset_token,
|
||||
message_password: match error {
|
||||
ResetPasswordError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
ResetPasswordError::PasswordsNotEqual => {
|
||||
context.tr.t(Sentence::PasswordDontMatch)
|
||||
}
|
||||
ResetPasswordError::InvalidPassword => reset_password_mess,
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
ResetPasswordError::TokenExpired => tr.t(Sentence::AskResetTokenExpired),
|
||||
ResetPasswordError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
ResetPasswordError::TokenExpired => {
|
||||
context.tr.t(Sentence::AskResetTokenExpired)
|
||||
}
|
||||
ResetPasswordError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
context,
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
|
|
@ -620,13 +627,13 @@ pub async fn reset_password_post(
|
|||
}
|
||||
|
||||
if form_data.password_1 != form_data.password_2 {
|
||||
return error_response(ResetPasswordError::PasswordsNotEqual, &form_data, user, tr);
|
||||
return error_response(ResetPasswordError::PasswordsNotEqual, &form_data, context);
|
||||
}
|
||||
|
||||
if let common::utils::PasswordValidation::TooShort =
|
||||
common::utils::validate_password(&form_data.password_1)
|
||||
{
|
||||
return error_response(ResetPasswordError::InvalidPassword, &form_data, user, tr);
|
||||
return error_response(ResetPasswordError::InvalidPassword, &form_data, context);
|
||||
}
|
||||
|
||||
match connection
|
||||
|
|
@ -638,24 +645,26 @@ pub async fn reset_password_post(
|
|||
.await
|
||||
{
|
||||
Ok(db::user::ResetPasswordResult::Ok) => Ok(Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::PasswordReset), tr, user).render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::PasswordReset),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
)
|
||||
.into_response()),
|
||||
Ok(db::user::ResetPasswordResult::ResetTokenExpired) => {
|
||||
error_response(ResetPasswordError::TokenExpired, &form_data, user, tr)
|
||||
error_response(ResetPasswordError::TokenExpired, &form_data, context)
|
||||
}
|
||||
Err(_) => error_response(ResetPasswordError::DatabaseError, &form_data, user, tr),
|
||||
Err(_) => error_response(ResetPasswordError::DatabaseError, &form_data, context),
|
||||
}
|
||||
}
|
||||
|
||||
/// EDIT PROFILE ///
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn edit_user_get(
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
) -> Result<Response> {
|
||||
Ok(if let Some(user) = user {
|
||||
pub async fn edit_user_get(Extension(context): Extension<Context>) -> Result<Response> {
|
||||
Ok(if let Some(ref user) = context.user {
|
||||
Html(
|
||||
ProfileTemplate {
|
||||
username: &user.name,
|
||||
|
|
@ -664,14 +673,14 @@ pub async fn edit_user_get(
|
|||
message: "",
|
||||
message_email: "",
|
||||
message_password: "",
|
||||
user: Some(user.clone()),
|
||||
tr,
|
||||
context: context.clone(),
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
.into_response()
|
||||
} else {
|
||||
Html(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).render()?).into_response()
|
||||
Html(MessageTemplate::new(context.tr.t(Sentence::NotLoggedIn), context.tr).render()?)
|
||||
.into_response()
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -699,43 +708,46 @@ pub async fn edit_user_post(
|
|||
Host(host): Host,
|
||||
State(connection): State<db::Connection>,
|
||||
State(config): State<Config>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
Form(form_data): Form<EditUserForm>,
|
||||
) -> Result<Response> {
|
||||
if let Some(user) = user {
|
||||
if let Some(ref user) = context.user {
|
||||
fn error_response(
|
||||
error: ProfileUpdateError,
|
||||
form_data: &EditUserForm,
|
||||
user: model::User,
|
||||
tr: translation::Tr,
|
||||
context: Context,
|
||||
) -> Result<Response> {
|
||||
let invalid_password_mess = &tr.tp(
|
||||
let invalid_password_mess = &context.tr.tp(
|
||||
Sentence::InvalidPassword,
|
||||
&[Box::new(common::consts::MIN_PASSWORD_SIZE)],
|
||||
);
|
||||
Ok(Html(
|
||||
ProfileTemplate {
|
||||
user: Some(user),
|
||||
username: &form_data.name,
|
||||
email: &form_data.email,
|
||||
default_servings: form_data.default_servings,
|
||||
message_email: match error {
|
||||
ProfileUpdateError::InvalidEmail => tr.t(Sentence::InvalidEmail),
|
||||
ProfileUpdateError::EmailAlreadyTaken => tr.t(Sentence::EmailAlreadyTaken),
|
||||
ProfileUpdateError::InvalidEmail => context.tr.t(Sentence::InvalidEmail),
|
||||
ProfileUpdateError::EmailAlreadyTaken => {
|
||||
context.tr.t(Sentence::EmailAlreadyTaken)
|
||||
}
|
||||
_ => "",
|
||||
},
|
||||
message_password: match error {
|
||||
ProfileUpdateError::PasswordsNotEqual => tr.t(Sentence::PasswordDontMatch),
|
||||
ProfileUpdateError::PasswordsNotEqual => {
|
||||
context.tr.t(Sentence::PasswordDontMatch)
|
||||
}
|
||||
ProfileUpdateError::InvalidPassword => invalid_password_mess,
|
||||
_ => "",
|
||||
},
|
||||
message: match error {
|
||||
ProfileUpdateError::DatabaseError => tr.t(Sentence::DatabaseError),
|
||||
ProfileUpdateError::UnableSendEmail => tr.t(Sentence::UnableToSendEmail),
|
||||
ProfileUpdateError::DatabaseError => context.tr.t(Sentence::DatabaseError),
|
||||
ProfileUpdateError::UnableSendEmail => {
|
||||
context.tr.t(Sentence::UnableToSendEmail)
|
||||
}
|
||||
_ => "",
|
||||
},
|
||||
tr,
|
||||
context,
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
|
|
@ -743,17 +755,17 @@ pub async fn edit_user_post(
|
|||
}
|
||||
|
||||
if form_data.email.parse::<Address>().is_err() {
|
||||
return error_response(ProfileUpdateError::InvalidEmail, &form_data, user, tr);
|
||||
return error_response(ProfileUpdateError::InvalidEmail, &form_data, context);
|
||||
}
|
||||
|
||||
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, tr);
|
||||
return error_response(ProfileUpdateError::PasswordsNotEqual, &form_data, context);
|
||||
}
|
||||
if let common::utils::PasswordValidation::TooShort =
|
||||
common::utils::validate_password(&form_data.password_1)
|
||||
{
|
||||
return error_response(ProfileUpdateError::InvalidPassword, &form_data, user, tr);
|
||||
return error_response(ProfileUpdateError::InvalidPassword, &form_data, context);
|
||||
}
|
||||
Some(form_data.password_1.as_ref())
|
||||
} else {
|
||||
|
|
@ -774,14 +786,14 @@ pub async fn edit_user_post(
|
|||
.await
|
||||
{
|
||||
Ok(db::user::UpdateUserResult::EmailAlreadyTaken) => {
|
||||
return error_response(ProfileUpdateError::EmailAlreadyTaken, &form_data, user, tr);
|
||||
return error_response(ProfileUpdateError::EmailAlreadyTaken, &form_data, context);
|
||||
}
|
||||
Ok(db::user::UpdateUserResult::UserUpdatedWaitingForRevalidation(token)) => {
|
||||
let url = utils::get_url_from_host(&host);
|
||||
let email = form_data.email.clone();
|
||||
match email::send_email(
|
||||
&email,
|
||||
&tr.tp(
|
||||
&context.tr.tp(
|
||||
Sentence::ProfileFollowEmailLink,
|
||||
&[Box::new(format!(
|
||||
"{}/revalidation?validation_token={}",
|
||||
|
|
@ -795,24 +807,23 @@ pub async fn edit_user_post(
|
|||
.await
|
||||
{
|
||||
Ok(()) => {
|
||||
message = tr.t(Sentence::ProfileEmailSent);
|
||||
message = context.tr.t(Sentence::ProfileEmailSent);
|
||||
}
|
||||
Err(_) => {
|
||||
// error!("Email validation error: {}", error); // TODO: log
|
||||
return error_response(
|
||||
ProfileUpdateError::UnableSendEmail,
|
||||
&form_data,
|
||||
user,
|
||||
tr,
|
||||
context,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(db::user::UpdateUserResult::Ok) => {
|
||||
message = tr.t(Sentence::ProfileSaved);
|
||||
message = context.tr.t(Sentence::ProfileSaved);
|
||||
}
|
||||
Err(_) => {
|
||||
return error_response(ProfileUpdateError::DatabaseError, &form_data, user, tr);
|
||||
return error_response(ProfileUpdateError::DatabaseError, &form_data, context);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -821,41 +832,42 @@ pub async fn edit_user_post(
|
|||
|
||||
Ok(Html(
|
||||
ProfileTemplate {
|
||||
user,
|
||||
username: &form_data.name,
|
||||
email: &form_data.email,
|
||||
default_servings: form_data.default_servings,
|
||||
message,
|
||||
message_email: "",
|
||||
message_password: "",
|
||||
tr,
|
||||
context: Context { user, ..context },
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
.into_response())
|
||||
} else {
|
||||
Ok(Html(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).render()?).into_response())
|
||||
Ok(
|
||||
Html(MessageTemplate::new(context.tr.t(Sentence::NotLoggedIn), context.tr).render()?)
|
||||
.into_response(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn email_revalidation(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(user): Extension<Option<model::User>>,
|
||||
Extension(tr): Extension<translation::Tr>,
|
||||
Extension(context): Extension<Context>,
|
||||
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() {
|
||||
if context.user.is_some() {
|
||||
return Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::ValidationUserAlreadyExists),
|
||||
tr,
|
||||
user,
|
||||
context.tr.t(Sentence::ValidationUserAlreadyExists),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
|
|
@ -875,15 +887,16 @@ pub async fn email_revalidation(
|
|||
.await?
|
||||
{
|
||||
db::user::ValidationResult::Ok(token, user_id) => {
|
||||
let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token);
|
||||
let cookie = Cookie::build((consts::COOKIE_AUTH_TOKEN_NAME, token))
|
||||
.same_site(cookie::SameSite::Strict);
|
||||
jar = jar.add(cookie);
|
||||
let user = connection.load_user(user_id).await?;
|
||||
Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::ValidationSuccessful),
|
||||
tr,
|
||||
context.tr.t(Sentence::ValidationSuccessful),
|
||||
context.tr,
|
||||
user,
|
||||
)
|
||||
.render()?,
|
||||
|
|
@ -893,17 +906,21 @@ pub async fn email_revalidation(
|
|||
db::user::ValidationResult::ValidationExpired => Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationExpired), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::ValidationExpired),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
)),
|
||||
db::user::ValidationResult::UnknownUser => Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(
|
||||
tr.t(Sentence::ValidationErrorTryToSignUpAgain),
|
||||
tr,
|
||||
user,
|
||||
context.tr.t(Sentence::ValidationErrorTryToSignUpAgain),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
|
|
@ -913,8 +930,12 @@ pub async fn email_revalidation(
|
|||
None => Ok((
|
||||
jar,
|
||||
Html(
|
||||
MessageTemplate::new_with_user(tr.t(Sentence::ValidationError), tr, user)
|
||||
.render()?,
|
||||
MessageTemplate::new_with_user(
|
||||
context.tr.t(Sentence::ValidationError),
|
||||
context.tr,
|
||||
context.user,
|
||||
)
|
||||
.render()?,
|
||||
),
|
||||
)),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue