sigil-added
This commit is contained in:
17
src/data.rs
17
src/data.rs
@@ -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)
|
||||
|
Reference in New Issue
Block a user