Skip to content

Commit

Permalink
added options for signin with oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Biggs committed Sep 16, 2024
1 parent 5951ab4 commit ff32fea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::{
models::{
Provider, RequestMagicLinkPayload, Session, SignInEmailOtpParams,
SignInWithEmailAndPasswordPayload, SignInWithEmailOtpPayload, SignInWithIdTokenCredentials,
SignInWithPhoneAndPasswordPayload, SignUpWithEmailAndPasswordPayload,
SignUpWithPhoneAndPasswordPayload, UpdateUserPayload, User, VerifyEmailOtpParams,
VerifyMobileOtpParams, VerifyOtpParams, VerifyTokenHashParams,
SignInWithOAuthOptions, SignInWithPhoneAndPasswordPayload,
SignUpWithEmailAndPasswordPayload, SignUpWithPhoneAndPasswordPayload, UpdateUserPayload,
User, VerifyEmailOtpParams, VerifyMobileOtpParams, VerifyOtpParams, VerifyTokenHashParams,
},
};

Expand Down Expand Up @@ -259,11 +259,17 @@ impl AuthClient {
}

// TODO: Add scopes and redirects and query params
pub async fn sign_in_with_oauth(&self, provider: Provider) -> Result<Response, Error> {
pub async fn sign_in_with_oauth(
&self,
provider: Provider,
options: Option<SignInWithOAuthOptions>,
) -> Result<Response, Error> {
let mut headers = header::HeaderMap::new();
headers.insert("Content-Type", "application/json".parse().unwrap());
headers.insert("apikey", self.api_key.parse().unwrap());

let body = serde_json::to_string(&options)?;

let response = self
.client
.get(format!(
Expand All @@ -272,6 +278,7 @@ impl AuthClient {
provider.to_string()
))
.headers(headers)
.body(body)
.send()
.await?;

Expand Down
13 changes: 12 additions & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use core::fmt;
use serde::{Deserialize, Serialize};
use std::fmt::{write, Display};
use std::{
collections::HashMap,
fmt::{write, Display},
};

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Session {
Expand Down Expand Up @@ -63,6 +66,14 @@ pub struct SignInWithIdTokenCredentials {
pub gotrue_meta_security: Option<GotrueMetaSecurity>,
}

#[derive(Debug, Serialize)]
pub struct SignInWithOAuthOptions {
pub query_params: Option<HashMap<String, String>>,
pub redirect_to: Option<String>,
pub scopes: Option<String>,
pub skip_brower_redirect: Option<bool>,
}

#[derive(Debug, Serialize)]
pub struct GotrueMetaSecurity {
captcha_token: Option<String>,
Expand Down
66 changes: 59 additions & 7 deletions tests/client_tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::env;
use std::{collections::HashMap, env};

use reqwest::header;
use supabase_auth::{
client::AuthClient,
models::{VerifyEmailOtpParams, VerifyOtpParams},
};
use reqwest::{header, Body};
use supabase_auth::{client::AuthClient, models::SignInWithOAuthOptions};

#[tokio::test]
async fn create_client_test_valid() {
Expand Down Expand Up @@ -184,10 +181,65 @@ async fn sign_in_with_oauth_test() {
headers.insert("apikey", auth_client.api_key.parse().unwrap());
headers.insert(header::AUTHORIZATION, auth_client.api_key.parse().unwrap());

let mut params = HashMap::new();
params.insert("key".to_string(), "value".to_string());
params.insert("second_key".to_string(), "second_value".to_string());
params.insert("third_key".to_string(), "third_value".to_string());

let options = SignInWithOAuthOptions {
query_params: Some(params),
redirect_to: Some("localhost".to_string()),
scopes: Some("repo gist notifications".to_string()),
skip_brower_redirect: Some(true),
};

let response = auth_client
.sign_in_with_oauth(supabase_auth::models::Provider::Github, Some(options))
.await;

println!("SIGN IN WITH OAUTH TEST RESPONSE -- \n{:?}", response);

if response.is_err() {
eprintln!("{:?}", response.as_ref().unwrap_err())
}

assert!(response.is_ok())
}

#[tokio::test]
async fn sign_in_with_oauth_no_options_test() {
let test_project_url = env::var("SUPABASE_URL").unwrap();
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

// Must login to get a user bearer token
let demo_email = "[email protected]";
let demo_password = "qwerqwer";

let session = auth_client
.sign_in_with_email_and_password(demo_email, demo_password)
.await;

if session.is_err() {
eprintln!("{:?}", session.as_ref().unwrap_err())
}

let mut headers = header::HeaderMap::new();
headers.insert("Content-Type", "application/json".parse().unwrap());
headers.insert("apikey", auth_client.api_key.parse().unwrap());
headers.insert(header::AUTHORIZATION, auth_client.api_key.parse().unwrap());

let response = auth_client
.sign_in_with_oauth(supabase_auth::models::Provider::Github)
.sign_in_with_oauth(supabase_auth::models::Provider::Github, None)
.await;

println!(
"SIGN IN WITH OAUTH \n NO OPTIONS TEST RESPONSE -- \n{:?}",
response
);

if response.is_err() {
eprintln!("{:?}", response.as_ref().unwrap_err())
}
Expand Down

0 comments on commit ff32fea

Please sign in to comment.