feat: add auth
This commit is contained in:
32
src/helper.rs
Normal file
32
src/helper.rs
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user