diff --git a/Cargo.lock b/Cargo.lock index 9674859..dabe887 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1967,6 +1967,7 @@ dependencies = [ "async-std", "dotenv", "ldap3", + "rand 0.8.5", "serde", "sqlx", "surf", diff --git a/Cargo.toml b/Cargo.toml index 721f937..69a00fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,6 @@ sqlx = { version = "0.6.3", features = [ "runtime-async-std-native-tls", "sqlite # to make the http requests surf = "2.3.2" + +# create random strings +rand = "0.8.5" diff --git a/src/methods/account_new.rs b/src/methods/account_new.rs index 5174a9c..0d43c99 100644 --- a/src/methods/account_new.rs +++ b/src/methods/account_new.rs @@ -97,6 +97,39 @@ pub async fn post_new_account(mut req: Request) -> tide::Result { } } + // frontend now tells user to check their email + + /* + TODO: + now check with wolves to see if the email is already activated + use email as primary match + then search up to see if teh wolves ID has a match + if not generate tuhe user and send email + */ + + let auth_code = create_random_string(50); + + // 1 hour expiry + let expiry = get_now() + (60 * 60); + + sqlx::query_as::<_, AccountsPending>( + r#" + INSERT OR REPLACE INTO accounts_pending (user, mail, name_first, name_second, auth_code, discord, expiry) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) + "#, + ) + .bind(&user) + .bind(&mail) + .bind(&name_first) + .bind(&name_second) + .bind(&auth_code) + .bind(&discord) + .bind(&expiry) + .fetch_optional(pool) + .await + .ok(); + + // TODO: Send email with auth_code Ok(json!({"result": "success"}).into()) } @@ -114,4 +147,15 @@ async fn db_pending_clear_expired(pool: &Pool){ ).bind(now).fetch_all(pool).await { println!("{:?}", results) } +} + +fn create_random_string(length: usize) -> String { + use rand::{thread_rng, Rng}; + use rand::distributions::Alphanumeric; + + thread_rng() + .sample_iter(&Alphanumeric) + .take(length) + .map(char::from) + .collect() } \ No newline at end of file