diff --git a/assets/migrations/00001_init.sql b/assets/migrations/00001_init.sql index d040ae1..b9905e2 100644 --- a/assets/migrations/00001_init.sql +++ b/assets/migrations/00001_init.sql @@ -101,12 +101,6 @@ CREATE TABLE IF NOT EXISTS bots ( FOREIGN KEY (owner_id) REFERENCES users(id) ); -CREATE TABLE IF NOT EXISTS pending_accounts ( - user_id BIGINT PRIMARY KEY, - code TEXT NOT NULL, - FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE -); - CREATE TABLE IF NOT EXISTS account_invites ( code TEXT NOT NULL, used BOOLEAN DEFAULT FALSE, diff --git a/src/utils/email.rs b/src/utils/email.rs index 9778c4d..0c0c618 100644 --- a/src/utils/email.rs +++ b/src/utils/email.rs @@ -1,4 +1,5 @@ use crate::config::*; +use crate::database::redis::{connection, AsyncCommands}; use crate::database::DB as db; use crate::structures::{Base, User}; use nanoid::nanoid; @@ -6,17 +7,13 @@ use rbatis::crud::CRUD; use regex::Regex; use serde_json::json; +const THREE_HOURS_IN_SECONDS: usize = 10800; + lazy_static! { static ref SPLIT_REGEX: Regex = Regex::new("([^@]+)(@.+)").unwrap(); static ref SYMBOL_REGEX: Regex = Regex::new("\\+.+|\\.").unwrap(); } -#[crud_table(table_name:pending_accounts)] -struct PendingVerification { - user_id: u64, - code: String, -} - #[crud_table(table_name:account_invites)] pub struct Invite { pub code: String, @@ -74,30 +71,21 @@ pub async fn send(user: &User) -> bool { .unwrap(); if res.status().is_success() { - let p = PendingVerification { - user_id: user.id, - code, - }; - db.save(&p, &[]).await.is_ok() + let mut con = connection().await; + con.set_ex::(user.id.to_string(), code, THREE_HOURS_IN_SECONDS) + .await + .is_ok() } else { false } } pub async fn verify(user_id: u64, code: &str) -> bool { - let p: Option = db - .fetch( - "SELECT * FROM pending_accounts WHERE user_id = $1 AND code = $2", - vec![user_id.into(), code.into()], - ) - .await - .ok(); + let mut con = connection().await; - match p { - Some(_) => { - db.remove_by_column::("user_id", user_id) - .await - .ok(); + match con.get::(user_id.to_string()).await { + Ok(token) if code == token => { + con.del::(user_id.to_string()).await.ok(); true } _ => false,