Skip to content

Commit

Permalink
Update Login With Email
Browse files Browse the repository at this point in the history
  • Loading branch information
Proziam committed Sep 29, 2024
1 parent a456a7a commit 18ff38d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
49 changes: 43 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand Down Expand Up @@ -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)?;

Expand All @@ -98,6 +97,44 @@ impl AuthClient {
Ok(serde_json::from_str(&response)?)
}

pub async fn login_with_email<S: Into<String>>(
&self,
email: S,
password: S,
) -> Result<Session, Error> {
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
/// ```
Expand Down
4 changes: 2 additions & 2 deletions tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 = "[email protected]";
Expand Down

0 comments on commit 18ff38d

Please sign in to comment.