Skip to main content

farcaster_rs/account/auth/
get_session_token.rs

1use serde_json::{json, Value};
2
3use crate::{
4    constants::merkle::API_ROOT,
5    types::account::auth::bearer::Bearer,
6    types::account::auth::secret::{Secret, SecretToken},
7    Farcaster,
8};
9
10impl Farcaster {
11    /// # Best not to use this function directly
12    /// Instead, use the [Account struct](../types/account/struct.Account.html) methods, such as from_mnemonic, and from_private_key, as that generates these automatically.
13    ///
14    /// However, if you'd really like to use these, go ahead.
15    pub async fn get_session_token(
16        bearer: &Bearer,
17    ) -> Result<SecretToken, Box<dyn std::error::Error>> {
18        let payload: Value = json!(bearer.payload);
19
20        let session_reqwest: String = reqwest::Client::new()
21            .put(format!("{}/v2/auth", API_ROOT))
22            .header("Content-Type", "application/json")
23            .header("Authorization", &bearer.bearer)
24            .json(&payload)
25            .send()
26            .await?
27            .text()
28            .await?;
29
30        let mut secret: Secret = serde_json::from_str(&session_reqwest)?;
31        secret.result.token.secret = format!("Bearer {}", secret.result.token.secret);
32
33        Ok(secret.result.token)
34    }
35}