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, ) -> Result 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, 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(()) }