diff --git a/src/client.rs b/src/client.rs index 1f4fc4e..1601652 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,18 +4,17 @@ use std::env; use reqwest::{ header::{self, HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE}, - Client, Response, + Client, Response, StatusCode, }; -use serde::Serialize; use crate::{ - error::Error, + error::{Error, SupabaseHTTPError}, models::{ AuthClient, AuthServerHealth, AuthServerSettings, LogoutScope, Provider, RefreshSessionPayload, RequestMagicLinkPayload, ResendParams, ResetPasswordForEmailPayload, - Session, SignInEmailOtpParams, SignInWithEmailAndPasswordPayload, + SSOResponse, Session, SignInEmailOtpParams, SignInWithEmailAndPasswordPayload, SignInWithEmailOtpPayload, SignInWithIdTokenCredentials, SignInWithOAuthOptions, - SignInWithPhoneAndPasswordPayload, SignUpWithEmailAndPasswordPayload, + SignInWithPhoneAndPasswordPayload, SignInWithSSO, SignUpWithEmailAndPasswordPayload, SignUpWithPhoneAndPasswordPayload, UpdateUserPayload, User, VerifyOtpParams, }, }; @@ -78,7 +77,7 @@ impl AuthClient { }; let mut headers = header::HeaderMap::new(); - headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?); + headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); headers.insert("apikey", HeaderValue::from_str(&self.api_key)?); let body = serde_json::to_string(&payload)?; @@ -98,6 +97,44 @@ impl AuthClient { Ok(serde_json::from_str(&response)?) } + pub async fn login_with_email>( + &self, + email: S, + password: S, + ) -> Result { + let payload = SignInWithEmailAndPasswordPayload { + email: email.into(), + password: password.into(), + }; + + let mut headers = header::HeaderMap::new(); + headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); + headers.insert("apikey", HeaderValue::from_str(&self.api_key)?); + let body = serde_json::to_string(&payload)?; + + let response = self + .client + .post(format!( + "{}/auth/v1/token?grant_type=password", + self.project_url + )) + .headers(headers) + .body(body) + .send() + .await?; + + match response.status() { + StatusCode::OK => { + let session: Session = serde_json::from_str(&response.text().await?)?; + Ok(session) + } + _ => { + let error: SupabaseHTTPError = serde_json::from_str(&response.text().await?)?; + Err(Error::Supabase(error)) + } + } + } + /// Sign in a user with phone number and password /// # Example /// ``` diff --git a/tests/client_tests.rs b/tests/client_tests.rs index 5d935fb..7931864 100644 --- a/tests/client_tests.rs +++ b/tests/client_tests.rs @@ -17,7 +17,7 @@ async fn create_client_test_valid() { } #[tokio::test] -async fn sign_in_with_password_test_valid() { +async fn test_login_with_email() { let auth_client = create_test_client(); let demo_email = env::var("DEMO_EMAIL").unwrap(); @@ -32,7 +32,7 @@ async fn sign_in_with_password_test_valid() { } #[tokio::test] -async fn sign_in_with_password_test_invalid() { +async fn test_login_with_email_invalid() { let auth_client = create_test_client(); let demo_email = "invalid@demo.com";