sigil-added

This commit is contained in:
2023-10-11 20:08:25 +03:00
parent 9fd6a89f7f
commit 510ac17375
2 changed files with 109 additions and 5 deletions

View File

@@ -6,19 +6,26 @@ use std::env;
pub async fn get_auth_id(token: &str) -> Result<i32, Box<dyn Error>> {
let auth_api_base = env::var("AUTH_URL")?;
let gql = match auth_api_base.contains("v2") {
true => r#"mutation { getSession { user { id } } }"#, // v2
_ => r#"query { sessiom { user { id } } }"# // authorizer
let queryname = match auth_api_base.contains("discours.io") {
true => "getSession", // v2
_ => "session" // authorizer
};
let body = format!(r#"{{
"query": "mutation GetSessionMutation {{ {} {{ user {{ id }} }} }}",
"operationName": "GetSessionMutation",
"variables": {{}}
}}"#, queryname);
let client = HTTPClient::new();
let response = client
.post(auth_api_base)
.bearer_auth(token) // NOTE: auth token is here
.body(gql)
.body(body)
.send()
.await?;
let response_body: Value = response.json().await?;
let id = response_body["data"]["getSession"]["user"]["id"]
let id = response_body["data"][queryname]["user"]["id"]
.as_i64()
.ok_or("Failed to get user id by token")? as i32;
Ok(id)