Skip to content

Commit c5e3c26

Browse files
authored
Merge pull request #20 from Raflos10/reset-password-options
Reset password options
2 parents f54be2d + 3778f66 commit c5e3c26

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/client.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
LoginWithEmailAndPasswordPayload, LoginWithEmailOtpPayload, LoginWithOAuthOptions,
3333
LoginWithPhoneAndPasswordPayload, LoginWithSSO, LogoutScope, OAuthResponse, OTPResponse,
3434
Provider, RefreshSessionPayload, RequestMagicLinkPayload, ResendParams,
35-
ResetPasswordForEmailPayload, SendSMSOtpPayload, Session,
35+
ResetPasswordForEmailPayload, ResetPasswordOptions, SendSMSOtpPayload, Session,
3636
SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
3737
SignUpWithPhoneAndPasswordPayload, UpdatedUser, User, VerifyOtpParams, AUTH_V1,
3838
},
@@ -1021,20 +1021,32 @@ impl AuthClient {
10211021
/// Valid email addresses that are not registered as users will not return an error.
10221022
/// # Example
10231023
/// ```
1024-
/// let response = auth_client.reset_password_for_email(demo_email).await.unwrap();
1024+
/// let response = auth_client.reset_password_for_email(demo_email, None).await.unwrap();
10251025
/// ```
1026-
pub async fn reset_password_for_email(&self, email: &str) -> Result<(), Error> {
1026+
pub async fn reset_password_for_email(
1027+
&self,
1028+
email: &str,
1029+
options: Option<ResetPasswordOptions>,
1030+
) -> Result<(), Error> {
1031+
let redirect_to = options
1032+
.as_ref()
1033+
.and_then(|o| o.email_redirect_to.as_deref().map(str::to_owned));
1034+
1035+
let payload = ResetPasswordForEmailPayload {
1036+
email: String::from(email),
1037+
options,
1038+
};
1039+
10271040
let mut headers = HeaderMap::new();
10281041
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
10291042
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
10301043

1031-
let body = serde_json::to_string(&ResetPasswordForEmailPayload {
1032-
email: email.into(),
1033-
})?;
1044+
let body = serde_json::to_string(&payload)?;
10341045

10351046
let response = self
10361047
.client
10371048
.post(&format!("{}{}/recover", self.project_url, AUTH_V1))
1049+
.query(&[("redirect_to", redirect_to.as_deref())])
10381050
.headers(headers)
10391051
.body(body)
10401052
.send()

src/models.rs

+13
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ pub struct SignUpWithPasswordOptions {
210210
pub captcha_token: Option<String>,
211211
}
212212

213+
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
214+
pub struct ResetPasswordOptions {
215+
/// The redirect url embedded in the email link
216+
#[serde(skip)]
217+
pub email_redirect_to: Option<String>,
218+
219+
/// Verification token received when the user completes the captcha on the site.
220+
pub captcha_token: Option<String>,
221+
}
222+
213223
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
214224
pub struct LoginAnonymouslyOptions {
215225
/// The `data` should be a JSON object that includes user-specific info, such as their first and last name.
@@ -341,6 +351,9 @@ pub(crate) struct RefreshSessionPayload<'a> {
341351
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
342352
pub(crate) struct ResetPasswordForEmailPayload {
343353
pub email: String,
354+
#[serde(flatten)]
355+
#[serde(skip_serializing_if = "Option::is_none")]
356+
pub(crate) options: Option<ResetPasswordOptions>,
344357
}
345358

346359
#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]

tests/client_tests.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use supabase_auth::{
55
error::Error,
66
models::{
77
AuthClient, LoginEmailOtpParams, LoginWithOAuthOptions, LoginWithSSO, LogoutScope,
8-
ResendParams, SignUpWithPasswordOptions, UpdatedUser,
8+
ResendParams, ResetPasswordOptions, SignUpWithPasswordOptions, UpdatedUser,
99
},
1010
};
1111

@@ -357,7 +357,14 @@ async fn reset_password_for_email_test() {
357357

358358
let demo_email = env::var("DEMO_EMAIL").unwrap();
359359

360-
let response = auth_client.reset_password_for_email(&demo_email).await;
360+
let options = ResetPasswordOptions {
361+
email_redirect_to: Some("https://www.thisisnotarealdomain.com".to_string()),
362+
..Default::default()
363+
};
364+
365+
let response = auth_client
366+
.reset_password_for_email(&demo_email, Some(options))
367+
.await;
361368

362369
// Wait to prevent running into Supabase rate limits when running cargo test
363370
let one_minute = time::Duration::from_secs(60);

0 commit comments

Comments
 (0)