feat: add auth

This commit is contained in:
2025-12-25 22:32:04 +01:00
parent 71d915ff0f
commit e7fe00c48d
17 changed files with 1654 additions and 35 deletions

32
src/helper.rs Normal file
View File

@@ -0,0 +1,32 @@
use std::error::Error;
use scp_core::apis::configuration;
use sqlx::query;
use crate::db;
pub async fn get_refresh_token() -> Result<String, Box<dyn Error>> {
let pool = db::get_pool().await?;
let tok = query!("SELECT * FROM tokens WHERE is_refresh = 1 ORDER BY expires_at DESC LIMIT 1")
.fetch_one(&pool)
.await?
.token;
Ok(tok)
}
pub async fn get_access_token() -> Result<String, Box<dyn Error>> {
let pool = db::get_pool().await?;
let tok = query!("SELECT * FROM tokens WHERE is_refresh = 0 ORDER BY expires_at DESC LIMIT 1")
.fetch_one(&pool)
.await?
.token;
Ok(tok)
}
pub async fn get_authed_api_config() -> Result<configuration::Configuration, Box<dyn Error>> {
let mut conf = configuration::Configuration::default();
conf.bearer_access_token = Some(get_access_token().await?);
Ok(conf)
}