Skip to content
Snippets Groups Projects
Verified Commit b2fa7aa2 authored by insert's avatar insert
Browse files

Finish email verification.

parent 70b41112
No related merge requests found
use reqwest::blocking::Client; use reqwest::blocking::Client;
use std::collections::HashMap; use std::collections::HashMap;
use std::env;
pub fn send_email(target: String, subject: String, body: String, html: String) -> Result<(), ()> { pub fn send_email(target: String, subject: String, body: String, html: String) -> Result<(), ()> {
let mut map = HashMap::new(); let mut map = HashMap::new();
...@@ -16,3 +17,32 @@ pub fn send_email(target: String, subject: String, body: String, html: String) - ...@@ -16,3 +17,32 @@ pub fn send_email(target: String, subject: String, body: String, html: String) -
Err(_) => Err(()) Err(_) => Err(())
} }
} }
fn public_uri() -> String {
env::var("PUBLIC_URI").expect("PUBLIC_URI not in environment variables!")
}
pub fn send_verification_email(email: String, code: String) -> bool {
let url = format!("{}/api/account/verify/{}", public_uri(), code);
match send_email(
email,
"Verify your email!".to_string(),
format!("Verify your email here: {}", url).to_string(),
format!("<a href=\"{}\">Click to verify your email!</a>", url).to_string()
) {
Ok(_) => true,
Err(_) => false,
}
}
pub fn send_welcome_email(email: String, username: String) -> bool {
match send_email(
email,
"Welcome to REVOLT!".to_string(),
format!("Welcome, {}! You can now use REVOLT.", username.clone()).to_string(),
format!("<b>Welcome, {}!</b><br/>You can now use REVOLT.<br/><a href=\"{}\">Go to REVOLT</a>", username.clone(), public_uri()).to_string()
) {
Ok(_) => true,
Err(_) => false,
}
}
...@@ -78,23 +78,13 @@ pub fn create(info: Json<Create>) -> JsonValue { ...@@ -78,23 +78,13 @@ pub fn create(info: Json<Create>) -> JsonValue {
"email_verification": { "email_verification": {
"verified": false, "verified": false,
"target": info.email.clone(), "target": info.email.clone(),
"expiry": UtcDatetime(Utc::now() + chrono::Duration::seconds(1)), "expiry": UtcDatetime(Utc::now() + chrono::Duration::days(1)),
"rate_limit": UtcDatetime(Utc::now() + chrono::Duration::minutes(1)), "rate_limit": UtcDatetime(Utc::now() + chrono::Duration::minutes(1)),
"code": code.clone(), "code": code.clone(),
} }
}, None) { }, None) {
Ok(_) => { Ok(_) => {
let url = format!("http://192.168.0.10:5500/api/account/verify/{}", code); let sent = email::send_verification_email(info.email.clone(), code);
let sent =
match email::send_email(
info.email.clone(),
"Verify your email!".to_string(),
format!("Verify your email here: {}", url).to_string(),
format!("<a href=\"{}\">Click to verify your email!</a>", url).to_string()
) {
Ok(_) => true,
Err(_) => false,
};
json!({ json!({
"success": true, "success": true,
...@@ -145,12 +135,17 @@ pub fn verify_email(code: String) -> JsonValue { ...@@ -145,12 +135,17 @@ pub fn verify_email(code: String) -> JsonValue {
}, },
"$set": { "$set": {
"email_verification.verified": true, "email_verification.verified": true,
"email": target, "email": target.clone(),
}, },
}, },
None, None,
).expect("Failed to update user!"); ).expect("Failed to update user!");
email::send_welcome_email(
target.to_string(),
u.get_str("username").expect("Failed to retrieve username.").to_string()
);
json!({ json!({
"success": true "success": true
}) })
...@@ -162,3 +157,65 @@ pub fn verify_email(code: String) -> JsonValue { ...@@ -162,3 +157,65 @@ pub fn verify_email(code: String) -> JsonValue {
}) })
} }
} }
#[derive(Serialize, Deserialize)]
pub struct Resend {
email: String,
}
/// resend a verification email
/// (1) check if verification is pending for x email
/// (2) check for rate limit
/// (3) resend the email
#[post("/resend", data = "<info>")]
pub fn resend_email(info: Json<Resend>) -> JsonValue {
let col = database::get_db().collection("users");
if let Some(u) =
col.find_one(doc! { "email_verification.target": info.email.clone() }, None).expect("Failed user lookup") {
let ev = u.get_document("email_verification").expect("Missing email_verification on user object!");
let rate_limit = ev.get_utc_datetime("rate_limit").expect("Missing rate_limit on email_verification!");
if Utc::now() < *rate_limit {
json!({
"success": false,
"error": "Hit rate limit! Please try again in a minute or so."
})
} else {
let code = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(48)
.collect::<String>();
col.update_one(
doc! { "_id": u.get_str("_id").expect("Failed to retrieve user id.") },
doc! {
"$set": {
"email_verification.code": code.clone(),
"email_verification.expiry": UtcDatetime(Utc::now() + chrono::Duration::days(1)),
"email_verification.rate_limit": UtcDatetime(Utc::now() + chrono::Duration::minutes(1)),
},
},
None,
).expect("Failed to update user!");
match email::send_verification_email(
info.email.to_string(),
code,
) {
true => json!({
"success": true,
}),
false => json!({
"success": false,
"error": "Failed to send email! Likely an issue with the backend API."
})
}
}
} else {
json!({
"success": false,
"error": "Email not pending verification!",
})
}
}
...@@ -4,5 +4,5 @@ mod account; ...@@ -4,5 +4,5 @@ mod account;
pub fn mount(rocket: Rocket) -> Rocket { pub fn mount(rocket: Rocket) -> Rocket {
rocket rocket
.mount("/api/account", routes![ account::root, account::create, account::verify_email ]) .mount("/api/account", routes![ account::root, account::create, account::verify_email, account::resend_email ])
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment