From 86a7bf6653c5a8f3f0419edaf3179390bf4888b4 Mon Sep 17 00:00:00 2001 From: Raflos Date: Tue, 4 Feb 2025 09:47:29 +0000 Subject: [PATCH] modified login_with_oauth to not call the /authorize endpoint --- src/client.rs | 48 ++++++++----------------------------------- src/error.rs | 2 ++ tests/client_tests.rs | 21 ++++++++----------- 3 files changed, 20 insertions(+), 51 deletions(-) diff --git a/src/client.rs b/src/client.rs index d22ab78..66da12e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -518,18 +518,13 @@ impl AuthClient { /// /// let response = auth_client /// .login_with_oauth(supabase_auth::models::Provider::Github, Some(options)) - /// .await /// .unwrap(); /// ``` - pub async fn login_with_oauth( + pub fn login_with_oauth( &self, provider: Provider, options: Option, ) -> Result { - let mut headers = header::HeaderMap::new(); - headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?); - headers.insert("apikey", HeaderValue::from_str(&self.api_key)?); - let query_params = options.as_ref().map_or_else( || vec![("provider", provider.to_string())], |o| { @@ -547,37 +542,13 @@ impl AuthClient { }, ); - let body = serde_json::to_string(&options)?; - - let response = self - .client - .get(format!("{}{}/authorize", self.project_url, AUTH_V1)) - .query(&query_params) - .headers(headers) - .body(body) - .send() - .await?; - - let res_status = response.status(); - let url = response.url().to_owned(); - let res_body = response.text().await?; + let url = Url::parse_with_params( + format!("{}{}/authorize", self.project_url, AUTH_V1).as_str(), + query_params, + ) + .map_err(|_| Error::ParseUrlError)?; - if res_status.is_success() { - Ok(OAuthResponse { url, provider }) - } else { - if let Ok(error) = from_str::(&res_body) { - return Err(AuthError { - status: res_status, - message: error.message, - }); - } - - // Fallback: return raw error - return Err(AuthError { - status: res_status, - message: res_body, - }); - } + Ok(OAuthResponse { url, provider }) } /// Sign up a user using an OAuth provider. @@ -596,15 +567,14 @@ impl AuthClient { /// /// let response = auth_client /// .sign_up_with_oauth(supabase_auth::models::Provider::Github, Some(options)) - /// .await /// .unwrap(); /// ``` - pub async fn sign_up_with_oauth( + pub fn sign_up_with_oauth( &self, provider: Provider, options: Option, ) -> Result { - self.login_with_oauth(provider, options).await + self.login_with_oauth(provider, options) } /// Return the signed in User diff --git a/src/error.rs b/src/error.rs index 4df3dfc..17d95f9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -40,6 +40,8 @@ pub enum Error { InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue), #[error("Environment Variable Unreadable")] InvalidEnvironmentVariable(#[from] env::VarError), + #[error("Failed to parse URL")] + ParseUrlError, #[error("{0}")] Supabase(SupabaseHTTPError), #[error("Error: {status}: {message}")] diff --git a/tests/client_tests.rs b/tests/client_tests.rs index f4b154a..fcc5320 100644 --- a/tests/client_tests.rs +++ b/tests/client_tests.rs @@ -148,8 +148,8 @@ async fn send_email_with_otp() { assert!(response.is_ok()) } -#[tokio::test] -async fn login_with_oauth_test() { +#[test] +fn login_with_oauth_test() { let auth_client = create_test_client(); let mut params = HashMap::new(); @@ -165,8 +165,7 @@ async fn login_with_oauth_test() { }; let response = auth_client - .login_with_oauth(supabase_auth::models::Provider::Github, Some(options)) - .await; + .login_with_oauth(supabase_auth::models::Provider::Github, Some(options)); if response.is_err() { println!("SIGN IN WITH OAUTH TEST RESPONSE -- \n{:?}", response); @@ -176,8 +175,8 @@ async fn login_with_oauth_test() { } #[ignore] -#[tokio::test] -async fn sign_up_with_oauth_test() { +#[test] +fn sign_up_with_oauth_test() { let auth_client = create_test_client(); let mut params = HashMap::new(); @@ -193,8 +192,7 @@ async fn sign_up_with_oauth_test() { }; let response = auth_client - .sign_up_with_oauth(supabase_auth::models::Provider::Github, Some(options)) - .await; + .sign_up_with_oauth(supabase_auth::models::Provider::Github, Some(options)); if response.is_err() { println!("SIGN IN WITH OAUTH TEST RESPONSE -- \n{:?}", response); @@ -203,8 +201,8 @@ async fn sign_up_with_oauth_test() { assert!(response.unwrap().url.to_string().len() > 1); } -#[tokio::test] -async fn login_with_oauth_no_options_test() { +#[test] +fn login_with_oauth_no_options_test() { let auth_client = create_test_client(); // // Must login to get a user bearer token @@ -220,8 +218,7 @@ async fn login_with_oauth_no_options_test() { // } let response = auth_client - .login_with_oauth(supabase_auth::models::Provider::Github, None) - .await; + .login_with_oauth(supabase_auth::models::Provider::Github, None); println!( "SIGN IN WITH OAUTH \n NO OPTIONS TEST RESPONSE -- \n{:?}",