firebase_auth_sdk/api/
sign_in.rs

1use super::FailResponse;
2use crate::error::Error;
3use serde::{Deserialize, Serialize};
4
5impl crate::FireAuth {
6    pub async fn sign_in_email(
7        &self,
8        email: &str,
9        password: &str,
10        return_secure_token: bool,
11    ) -> Result<Response, Error> {
12        let url = format!(
13            "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={}",
14            self.api_key,
15        );
16
17        let client = reqwest::Client::new();
18        let resp = client
19            .post(&url)
20            .header("Content-Type", "application/json")
21            .json(&SignInPayload {
22                email,
23                password,
24                return_secure_token,
25            })
26            .send()
27            .await?;
28
29        if resp.status() != 200 {
30            let error = resp.json::<FailResponse>().await?.error;
31            return Err(Error::SignIn(error.message));
32        }
33
34        let body = resp.json::<Response>().await?;
35        Ok(body)
36    }
37}
38
39#[derive(Debug, Serialize)]
40#[serde(rename_all = "camelCase")]
41struct SignInPayload<'a> {
42    email: &'a str,
43    password: &'a str,
44    return_secure_token: bool,
45}
46
47#[derive(Debug, Serialize, Deserialize)]
48#[serde(rename_all = "camelCase")]
49pub struct Response {
50    pub kind: String,
51    pub local_id: String,
52    pub email: String,
53    pub display_name: String,
54    pub id_token: String,
55    pub registered: bool,
56    pub refresh_token: Option<String>,
57    pub expires_in: Option<String>,
58}