-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add email change support to the server
- Loading branch information
1 parent
8ded579
commit 85fabcc
Showing
12 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::email::Mailable; | ||
use crate::locale::Locale; | ||
use serde::Serialize; | ||
|
||
pub struct EmailChangedMail; | ||
|
||
#[derive(Serialize)] | ||
pub struct EmailChangedData { | ||
pub name: String, | ||
} | ||
|
||
impl Mailable for EmailChangedMail { | ||
type Data = EmailChangedData; | ||
|
||
fn template_name() -> &'static str { | ||
"email_changed" | ||
} | ||
|
||
fn subject(locale: &Locale) -> &'static str { | ||
match locale { | ||
Locale::En => "Your email address was changed", | ||
Locale::Nl => "Je email adres is gewijzigd", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<html lang="en"> | ||
{{> header }} | ||
<body> | ||
<div class="container"> | ||
{{> banner }} | ||
|
||
<p>Hi {{ name }}, </p> | ||
<p> | ||
Your email address was changed | ||
</p> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<html lang="nl"> | ||
{{> header }} | ||
<body> | ||
<div class="container"> | ||
{{> banner }} | ||
|
||
<p>Hoi {{ name }}, </p> | ||
<p> | ||
Je email address is gewijzigd. | ||
</p> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::authorization::combined::CombinedAuthorizationProvider; | ||
use crate::authorization::AuthorizationProvider; | ||
use crate::mail::WilfordMailer; | ||
use crate::response_types::Empty; | ||
use crate::routes::auth::Auth; | ||
use crate::routes::error::{WebErrorKind, WebResult}; | ||
use crate::routes::{auth_error_to_web_error, WConfig, WDatabase}; | ||
use actix_web::web; | ||
use mailer::{EmailChangedData, EmailChangedMail, Locale}; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Request { | ||
new_email: String, | ||
password: String, | ||
} | ||
|
||
/// Change email address of the user | ||
/// | ||
/// # Errors | ||
/// - The operation is not supported | ||
/// - The provided password is invalid | ||
/// - The operation fails | ||
pub async fn change_email( | ||
auth: Auth, | ||
payload: web::Json<Request>, | ||
config: WConfig, | ||
database: WDatabase, | ||
) -> WebResult<Empty> { | ||
// Check for support | ||
let provider = CombinedAuthorizationProvider::new(&config, &database); | ||
if !provider.supports_email_change() { | ||
return Err(WebErrorKind::Unsupported.into()); | ||
} | ||
|
||
// Validate password | ||
auth_error_to_web_error( | ||
provider | ||
.validate_credentials(&auth.user.email, &payload.password, None) | ||
.await, | ||
)?; | ||
|
||
// Change email | ||
auth_error_to_web_error(provider.set_email(&auth.user_id, &payload.new_email).await)?; | ||
|
||
// Send email | ||
if let Some(email_cfg) = &config.email { | ||
// Old email | ||
WilfordMailer::new(email_cfg) | ||
.send_email( | ||
&auth.user.email, | ||
EmailChangedMail, | ||
&EmailChangedData { | ||
name: auth.user.name, | ||
}, | ||
Locale::En, | ||
) | ||
.await?; | ||
|
||
// TODO: Verifaction email to new email | ||
} | ||
|
||
Ok(Empty) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters