-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): Implement password reset DAO (#1456)
* feat(db, webserver): Implement password reset flow * [autofix.ci] apply automated fixes * Remove webserver logic from this PR * Use ID instead of email * Create delete_expired_password_reset * Rename id to user_id * Add rowid * Rename parameters * Rename db methods * Unrename one db method --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
904ebbe
commit 76f733b
Showing
7 changed files
with
86 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE password_reset; |
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,6 @@ | ||
CREATE TABLE password_reset( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
user_id INTEGER NOT NULL UNIQUE, | ||
code VARCHAR(36) NOT NULL, | ||
created_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now')) | ||
); |
Binary file not shown.
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,58 @@ | ||
use anyhow::Result; | ||
use chrono::{DateTime, Duration, Utc}; | ||
use sqlx::{prelude::FromRow, query}; | ||
use uuid::Uuid; | ||
|
||
use crate::DbConn; | ||
|
||
#[derive(FromRow)] | ||
pub struct PasswordResetDAO { | ||
pub user_id: i32, | ||
pub code: String, | ||
pub created_at: DateTime<Utc>, | ||
} | ||
|
||
impl DbConn { | ||
pub async fn create_password_reset(&self, user_id: i32) -> Result<String> { | ||
let code = Uuid::new_v4().to_string(); | ||
let time = Utc::now(); | ||
query!( | ||
"INSERT INTO password_reset (user_id, code, created_at) VALUES ($1, $2, $3) | ||
ON CONFLICT(user_id) DO UPDATE SET code= $2, created_at = $3;", | ||
user_id, | ||
code, | ||
time | ||
) | ||
.execute(&self.pool) | ||
.await?; | ||
Ok(code) | ||
} | ||
|
||
pub async fn delete_password_reset_by_user_id(&self, user_id: i32) -> Result<()> { | ||
query!("DELETE FROM password_reset WHERE user_id = ?", user_id) | ||
.execute(&self.pool) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn get_password_reset_by_user_id( | ||
&self, | ||
user_id: i32, | ||
) -> Result<Option<PasswordResetDAO>> { | ||
let password_reset = sqlx::query_as( | ||
"SELECT user_id, code, created_at FROM password_reset WHERE user_id = ?;", | ||
) | ||
.bind(user_id) | ||
.fetch_optional(&self.pool) | ||
.await?; | ||
Ok(password_reset) | ||
} | ||
|
||
pub async fn delete_expired_password_resets(&self) -> Result<()> { | ||
let time = Utc::now() - Duration::hours(1); | ||
query!("DELETE FROM password_reset WHERE created_at < ?", time) | ||
.execute(&self.pool) | ||
.await?; | ||
Ok(()) | ||
} | ||
} |
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,9 @@ | ||
Reset your Tabby password | ||
--- | ||
You recently requested a password reset for your TabbyML account {{EMAIL}}. Please click the link below to reset your password. | ||
|
||
{{EXTERNAL_URL}}/passwordReset?code={{CODE}} | ||
|
||
Best regards, | ||
|
||
The Tabby Team |