All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 10m12s
60 lines
1.3 KiB
Rust
60 lines
1.3 KiB
Rust
use sqlx::{
|
|
Executor, Postgres,
|
|
types::{
|
|
Uuid,
|
|
chrono::{DateTime, Utc},
|
|
},
|
|
};
|
|
|
|
use crate::{db::model::refresh_token::RefreshToken, errors::AppError};
|
|
|
|
pub async fn create_refresh_token<'e, E>(
|
|
executor: E,
|
|
user_id: Uuid,
|
|
token_hash: String,
|
|
expires_at: DateTime<Utc>,
|
|
) -> Result<RefreshToken, AppError>
|
|
where
|
|
E: Executor<'e, Database = Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
RefreshToken,
|
|
"insert into refresh_tokens (user_id, token_hash, expires_at) values ($1, $2, $3) returning *",
|
|
user_id,
|
|
token_hash,
|
|
expires_at
|
|
).fetch_one(executor)
|
|
.await
|
|
.map_err(AppError::from)
|
|
}
|
|
|
|
pub async fn find_by_hash<'e, E>(
|
|
executor: E,
|
|
token_hash: &str,
|
|
) -> Result<Option<RefreshToken>, AppError>
|
|
where
|
|
E: Executor<'e, Database = Postgres>,
|
|
{
|
|
sqlx::query_as!(
|
|
RefreshToken,
|
|
"select * from refresh_tokens where token_hash = $1",
|
|
token_hash
|
|
)
|
|
.fetch_optional(executor)
|
|
.await
|
|
.map_err(AppError::from)
|
|
}
|
|
pub async fn revoke<'e, E>(executor: E, id: Uuid) -> Result<(), AppError>
|
|
where
|
|
E: Executor<'e, Database = Postgres>,
|
|
{
|
|
sqlx::query!(
|
|
"update refresh_tokens set revoked_at = now() where id = $1",
|
|
id
|
|
)
|
|
.execute(executor)
|
|
.await
|
|
.map_err(AppError::from)?;
|
|
Ok(())
|
|
}
|