Skip to content

Commit

Permalink
modified login_with_oauth to not call the /authorize endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Raflos10 committed Feb 4, 2025
1 parent c5e3c26 commit 86a7bf6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 51 deletions.
48 changes: 9 additions & 39 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginWithOAuthOptions>,
) -> Result<OAuthResponse, Error> {
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| {
Expand All @@ -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::<SupabaseHTTPError>(&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.
Expand All @@ -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<LoginWithOAuthOptions>,
) -> Result<OAuthResponse, Error> {
self.login_with_oauth(provider, options).await
self.login_with_oauth(provider, options)
}

/// Return the signed in User
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down
21 changes: 9 additions & 12 deletions tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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{:?}",
Expand Down

0 comments on commit 86a7bf6

Please sign in to comment.