Skip to content

Commit c3a1b21

Browse files
authored
Merge pull request #7 from tubbo/anonymous-signin
Anonymous Sign-In
2 parents b25214a + 813a435 commit c3a1b21

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/client.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
AuthClient, AuthServerHealth, AuthServerSettings, IdTokenCredentials, InviteParams,
1515
LogoutScope, OAuthResponse, OTPResponse, Provider, RefreshSessionPayload,
1616
RequestMagicLinkPayload, ResendParams, ResetPasswordForEmailPayload, SendSMSOtpPayload,
17-
Session, SignInEmailOtpParams, SignInWithEmailAndPasswordPayload,
17+
Session, SignInAnonymouslyPayload, SignInEmailOtpParams, SignInWithEmailAndPasswordPayload,
1818
SignInWithEmailOtpPayload, SignInWithOAuthOptions, SignInWithPhoneAndPasswordPayload,
1919
SignInWithSSO, SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
2020
SignUpWithPhoneAndPasswordPayload, UpdatedUser, User, VerifyOtpParams, AUTH_V1,
@@ -237,6 +237,51 @@ impl AuthClient {
237237
Ok(session)
238238
}
239239

240+
/// Sign in a new user anonymously. This actually signs up a user, but it's
241+
/// called "sign in" by Supabase in their own client, so that's why it's
242+
/// named like this here. You can also pass in the same signup options
243+
/// that work for the other `sign_up_*` methods, but that's not required.
244+
///
245+
/// # Example
246+
/// ```
247+
/// let session = auth_client
248+
/// .sign_in_anonymously(demo_options)
249+
/// .await
250+
/// .unwrap();
251+
///
252+
/// assert!(session.user.user_metadata.display_name == demo_options.data.display_name)
253+
/// ```
254+
pub async fn sign_in_anonymously(
255+
&self,
256+
options: Option<SignUpWithPasswordOptions>,
257+
) -> Result<Session, Error> {
258+
let payload = SignInAnonymouslyPayload { options };
259+
260+
let mut headers = header::HeaderMap::new();
261+
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
262+
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
263+
264+
let body = serde_json::to_string(&payload)?;
265+
266+
let response = self
267+
.client
268+
.post(format!("{}{}/signup", self.project_url, AUTH_V1))
269+
.headers(headers)
270+
.body(body)
271+
.send()
272+
.await?;
273+
274+
let res_status = response.status();
275+
let res_body = response.text().await?;
276+
277+
let session: Session = from_str(&res_body).map_err(|_| AuthError {
278+
status: res_status,
279+
message: res_body,
280+
})?;
281+
282+
Ok(session)
283+
}
284+
240285
/// Sends a login email containing a magic link
241286
/// # Example
242287
/// ```

src/models.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ pub(crate) struct SignUpWithPhoneAndPasswordPayload<'a> {
175175
pub(crate) options: Option<SignUpWithPasswordOptions>,
176176
}
177177

178+
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
179+
pub(crate) struct SignInAnonymouslyPayload {
180+
pub(crate) options: Option<SignUpWithPasswordOptions>,
181+
}
182+
178183
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
179184
pub struct SignUpWithPasswordOptions {
180185
/// The redirect url embedded in the email link

0 commit comments

Comments
 (0)