Skip to content

Commit 26a0ed0

Browse files
authored
Merge pull request #26 from Raflos10/email-confirmations
added email sign up confirmation result to handle different possible …
2 parents 8906ecb + 65c757d commit 26a0ed0

File tree

4 files changed

+80
-40
lines changed

4 files changed

+80
-40
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ use crate::{
2727
SupabaseHTTPError,
2828
},
2929
models::{
30-
AuthClient, AuthServerHealth, AuthServerSettings, IdTokenCredentials, InviteParams,
31-
LoginAnonymouslyOptions, LoginAnonymouslyPayload, LoginEmailOtpParams,
32-
LoginWithEmailAndPasswordPayload, LoginWithEmailOtpPayload, LoginWithOAuthOptions,
33-
LoginWithPhoneAndPasswordPayload, LoginWithSSO, LogoutScope, OAuthResponse, OTPResponse,
34-
Provider, RefreshSessionPayload, RequestMagicLinkPayload, ResendParams,
35-
ResetPasswordForEmailPayload, ResetPasswordOptions, SendSMSOtpPayload, Session,
36-
SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
30+
AuthClient, AuthServerHealth, AuthServerSettings, EmailSignUpConfirmation,
31+
EmailSignUpResult, IdTokenCredentials, InviteParams, LoginAnonymouslyOptions,
32+
LoginAnonymouslyPayload, LoginEmailOtpParams, LoginWithEmailAndPasswordPayload,
33+
LoginWithEmailOtpPayload, LoginWithOAuthOptions, LoginWithPhoneAndPasswordPayload,
34+
LoginWithSSO, LogoutScope, OAuthResponse, OTPResponse, Provider, RefreshSessionPayload,
35+
RequestMagicLinkPayload, ResendParams, ResetPasswordForEmailPayload, ResetPasswordOptions,
36+
SendSMSOtpPayload, Session, SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
3737
SignUpWithPhoneAndPasswordPayload, UpdatedUser, User, VerifyOtpParams, AUTH_V1,
3838
},
3939
};
@@ -183,19 +183,19 @@ impl AuthClient {
183183
/// Sign up a new user with an email and password
184184
/// # Example
185185
/// ```
186-
/// let session = auth_client
186+
/// let result = auth_client
187187
/// .sign_up_with_email_and_password(demo_email, demo_password)
188188
/// .await
189189
/// .unwrap();
190190
///
191-
/// assert!(session.user.email == demo_email)
191+
/// assert!(result.session.user.email == demo_email)
192192
///```
193193
pub async fn sign_up_with_email_and_password(
194194
&self,
195195
email: &str,
196196
password: &str,
197197
options: Option<SignUpWithPasswordOptions>,
198-
) -> Result<Session, Error> {
198+
) -> Result<EmailSignUpResult, Error> {
199199
let redirect_to = options
200200
.as_ref()
201201
.and_then(|o| o.email_redirect_to.as_deref().map(str::to_owned));
@@ -224,8 +224,12 @@ impl AuthClient {
224224
let res_status = response.status();
225225
let res_body = response.text().await?;
226226

227-
if let Ok(session) = from_str(&res_body) {
228-
return Ok(session);
227+
if let Ok(session) = from_str::<Session>(&res_body) {
228+
return Ok(EmailSignUpResult::SessionResult(session));
229+
}
230+
231+
if let Ok(result) = from_str::<EmailSignUpConfirmation>(&res_body) {
232+
return Ok(EmailSignUpResult::ConfirmationResult(result));
229233
}
230234

231235
if let Ok(error) = from_str::<SupabaseHTTPError>(&res_body) {

src/models.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use core::fmt;
44
use reqwest::{Client, Url};
55
use serde::{Deserialize, Serialize};
66
use serde_json::Value;
7-
use uuid::Uuid;
87
use std::{collections::HashMap, fmt::Display};
8+
use uuid::Uuid;
99

1010
/// Supabase Auth Client
1111
#[derive(Clone)]
@@ -98,6 +98,34 @@ pub struct UserMetadata {
9898
pub custom: HashMap<String, Value>,
9999
}
100100

101+
#[derive(Debug)]
102+
pub enum EmailSignUpResult {
103+
SessionResult(Session),
104+
ConfirmationResult(EmailSignUpConfirmation),
105+
}
106+
107+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
108+
pub struct EmailSignUpConfirmation {
109+
#[serde(skip_serializing_if = "Option::is_none")]
110+
pub id: Option<Uuid>,
111+
#[serde(skip_serializing_if = "Option::is_none")]
112+
pub aud: Option<String>,
113+
#[serde(skip_serializing_if = "Option::is_none")]
114+
pub role: Option<String>,
115+
#[serde(skip_serializing_if = "Option::is_none")]
116+
pub email: Option<String>,
117+
#[serde(skip_serializing_if = "Option::is_none")]
118+
pub phone: Option<String>,
119+
#[serde(skip_serializing_if = "Option::is_none")]
120+
pub confirmation_sent_at: Option<String>,
121+
#[serde(skip_serializing_if = "Option::is_none")]
122+
pub created_at: Option<String>,
123+
#[serde(skip_serializing_if = "Option::is_none")]
124+
pub updated_at: Option<String>,
125+
#[serde(skip_serializing_if = "Option::is_none")]
126+
pub is_anonymous: Option<bool>,
127+
}
128+
101129
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
102130
pub struct IdTokenCredentials {
103131
/// Provider name or OIDC `iss` value identifying which provider should be used to verify the provided token.

tests/client_tests.rs

+34-26
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::{collections::HashMap, env, thread};
44
use supabase_auth::{
55
error::Error,
66
models::{
7-
AuthClient, LoginEmailOtpParams, LoginWithOAuthOptions, LoginWithSSO, LogoutScope,
8-
ResendParams, ResetPasswordOptions, SignUpWithPasswordOptions, UpdatedUser,
7+
AuthClient, EmailSignUpResult, LoginEmailOtpParams, LoginWithOAuthOptions, LoginWithSSO,
8+
LogoutScope, ResendParams, ResetPasswordOptions, SignUpWithPasswordOptions, UpdatedUser,
99
},
1010
};
1111

@@ -73,7 +73,7 @@ async fn sign_up_with_email_test_valid() {
7373
..Default::default()
7474
};
7575

76-
let session = auth_client
76+
let result = auth_client
7777
.sign_up_with_email_and_password(demo_email.as_ref(), demo_password, Some(options))
7878
.await
7979
.unwrap();
@@ -82,19 +82,21 @@ async fn sign_up_with_email_test_valid() {
8282
let one_minute = time::Duration::from_secs(60);
8383
thread::sleep(one_minute);
8484

85-
assert!(session.user.email == demo_email);
86-
assert!(session.user.user_metadata.name.unwrap() == "test");
87-
assert!(
88-
session
89-
.user
90-
.user_metadata
91-
.custom
92-
.get("test")
93-
.unwrap()
94-
.as_str()
95-
.unwrap()
96-
== "test"
97-
)
85+
if let EmailSignUpResult::SessionResult(session) = result {
86+
assert!(session.user.email == demo_email);
87+
assert!(session.user.user_metadata.name.unwrap() == "test");
88+
assert!(
89+
session
90+
.user
91+
.user_metadata
92+
.custom
93+
.get("test")
94+
.unwrap()
95+
.as_str()
96+
.unwrap()
97+
== "test"
98+
)
99+
}
98100
}
99101

100102
#[tokio::test]
@@ -164,8 +166,8 @@ fn login_with_oauth_test() {
164166
skip_brower_redirect: Some(true),
165167
};
166168

167-
let response = auth_client
168-
.login_with_oauth(supabase_auth::models::Provider::Github, Some(options));
169+
let response =
170+
auth_client.login_with_oauth(supabase_auth::models::Provider::Github, Some(options));
169171

170172
if response.is_err() {
171173
println!("SIGN IN WITH OAUTH TEST RESPONSE -- \n{:?}", response);
@@ -191,8 +193,8 @@ fn sign_up_with_oauth_test() {
191193
skip_brower_redirect: Some(true),
192194
};
193195

194-
let response = auth_client
195-
.sign_up_with_oauth(supabase_auth::models::Provider::Github, Some(options));
196+
let response =
197+
auth_client.sign_up_with_oauth(supabase_auth::models::Provider::Github, Some(options));
196198

197199
if response.is_err() {
198200
println!("SIGN IN WITH OAUTH TEST RESPONSE -- \n{:?}", response);
@@ -217,8 +219,7 @@ fn login_with_oauth_no_options_test() {
217219
// eprintln!("{:?}", session.as_ref().unwrap_err())
218220
// }
219221

220-
let response = auth_client
221-
.login_with_oauth(supabase_auth::models::Provider::Github, None);
222+
let response = auth_client.login_with_oauth(supabase_auth::models::Provider::Github, None);
222223

223224
println!(
224225
"SIGN IN WITH OAUTH \n NO OPTIONS TEST RESPONSE -- \n{:?}",
@@ -379,12 +380,12 @@ async fn resend_email_test() {
379380
let demo_email = format!("signup__{}@demo.com", uuid);
380381
let demo_password = "ciJUAojfZZYKfCxkiUWH";
381382

382-
let session = auth_client
383+
let result = auth_client
383384
.sign_up_with_email_and_password(&demo_email, demo_password, None)
384385
.await;
385386

386-
if session.is_err() {
387-
eprintln!("{:?}", session.as_ref().unwrap_err())
387+
if result.is_err() {
388+
eprintln!("{:?}", result.as_ref().unwrap_err())
388389
}
389390

390391
let credentials = ResendParams {
@@ -403,7 +404,14 @@ async fn resend_email_test() {
403404
println!("{:?}", response)
404405
}
405406

406-
assert!(response.is_ok() && session.unwrap().user.email == demo_email)
407+
match result.unwrap() {
408+
EmailSignUpResult::SessionResult(session) => {
409+
assert!(response.is_ok() && session.user.email == demo_email)
410+
}
411+
EmailSignUpResult::ConfirmationResult(email_sign_up_confirmation) => {
412+
assert!(response.is_ok() && email_sign_up_confirmation.email.unwrap() == demo_email)
413+
}
414+
}
407415
}
408416

409417
#[tokio::test]

0 commit comments

Comments
 (0)