Database::get_token
This commit is contained in:
parent
3c5bff0237
commit
9cd4848ce8
1 changed files with 18 additions and 9 deletions
27
src/db.rs
27
src/db.rs
|
@ -1,4 +1,5 @@
|
|||
use sqlx::{query, Connection, SqliteConnection};
|
||||
use secrecy::SecretString;
|
||||
use sqlx::{query, query_as, Connection, SqliteConnection};
|
||||
|
||||
use crate::auth::UserToken;
|
||||
|
||||
|
@ -6,7 +7,7 @@ use crate::auth::UserToken;
|
|||
const DB_URI_DEFAULT: &str = "sqlite://sqlite.db";
|
||||
|
||||
pub trait Database {
|
||||
async fn get_token(&self, username: &str) -> UserToken;
|
||||
async fn get_token(&mut self, username: &str) -> Result<UserToken, sqlx::Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -44,12 +45,20 @@ impl SqliteDatabase {
|
|||
}
|
||||
|
||||
impl Database for SqliteDatabase {
|
||||
async fn get_token(&self, username: &str) -> UserToken {
|
||||
todo!()
|
||||
// query(
|
||||
// "
|
||||
// SELECT token, created, valid
|
||||
// ",
|
||||
// )
|
||||
async fn get_token(&mut self, username: &str) -> Result<UserToken, sqlx::Error> {
|
||||
let row: (String,) = query_as(
|
||||
"SELECT token
|
||||
FROM user_tokens
|
||||
WHERE
|
||||
username = {username}
|
||||
AND valid = TRUE
|
||||
ORDER BY
|
||||
created DESC",
|
||||
)
|
||||
.bind(username)
|
||||
.fetch_one(&mut self.conn)
|
||||
.await?;
|
||||
|
||||
Ok(UserToken(SecretString::new(row.0)))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue