firebase-user 0.5.0

Manage Firebase users
Documentation
use serde::{Deserialize, Serialize};

use crate::{
    error::{ApiError, check_response},
    url::FirebaseUrl,
};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignInByEmailResponse {
    pub kind: String,
    pub local_id: String,
    pub email: String,
    pub display_name: String,
    pub id_token: String,
    pub registered: bool,
    pub refresh_token: Option<String>,
    pub expires_in: Option<String>,
}

pub async fn by_email(
    firebase_url: &FirebaseUrl,
    email: &str,
    password: &str,
    return_secure_token: bool,
) -> Result<SignInByEmailResponse, ApiError> {
    let client = reqwest::Client::new();

    let response = client
        .post(&firebase_url.sign_in_by_email_url)
        .header("Content-Type", "application/json")
        .json(&Payload {
            email,
            password,
            return_secure_token,
        })
        .send()
        .await?;

    check_response(response).await
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct Payload<'a> {
    email: &'a str,
    password: &'a str,
    return_secure_token: bool,
}