pub struct AuthClient { /* private fields */ }
Expand description
Supabase Auth Client
Implementations§
Source§impl AuthClient
impl AuthClient
Sourcepub fn new(
project_url: impl Into<String>,
api_key: impl Into<String>,
jwt_secret: impl Into<String>,
) -> Self
pub fn new( project_url: impl Into<String>, api_key: impl Into<String>, jwt_secret: impl Into<String>, ) -> Self
Create a new Auth Client
You can find your project url and keys at https://supabase.com/dashboard/project/YOUR_PROJECT_ID/settings/api
§Example
let auth_client = AuthClient::new(project_url, api_key, jwt_secret).unwrap();
Sourcepub fn new_from_env() -> Result<AuthClient, Error>
pub fn new_from_env() -> Result<AuthClient, Error>
Create a new AuthClient from environment variables
Requires SUPABASE_URL
, SUPABASE_API_KEY
, and SUPABASE_JWT_SECRET
environment variables
§Example
let auth_client = AuthClient::new_from_env().unwrap();
assert!(auth_client.project_url == env::var("SUPABASE_URL").unwrap())
Sourcepub async fn login_with_email(
&self,
email: &str,
password: &str,
) -> Result<Session, Error>
pub async fn login_with_email( &self, email: &str, password: &str, ) -> Result<Session, Error>
Sign in a user with an email and password
§Example
let session = auth_client
.login_with_email(demo_email, demo_password)
.await
.unwrap();
assert!(session.user.email == demo_email)
Sourcepub async fn login_with_phone(
&self,
phone: &str,
password: &str,
) -> Result<Session, Error>
pub async fn login_with_phone( &self, phone: &str, password: &str, ) -> Result<Session, Error>
Sign in a user with phone number and password
§Example
let session = auth_client
.login_with_phone(demo_phone, demo_password)
.await
.unwrap();
assert!(session.user.phone == demo_phone)
Sourcepub async fn sign_up_with_email_and_password(
&self,
email: &str,
password: &str,
options: Option<SignUpWithPasswordOptions>,
) -> Result<EmailSignUpResult, Error>
pub async fn sign_up_with_email_and_password( &self, email: &str, password: &str, options: Option<SignUpWithPasswordOptions>, ) -> Result<EmailSignUpResult, Error>
Sign up a new user with an email and password
§Example
let result = auth_client
.sign_up_with_email_and_password(demo_email, demo_password)
.await
.unwrap();
assert!(result.session.user.email == demo_email)
Sourcepub async fn sign_up_with_phone_and_password(
&self,
phone: &str,
password: &str,
options: Option<SignUpWithPasswordOptions>,
) -> Result<Session, Error>
pub async fn sign_up_with_phone_and_password( &self, phone: &str, password: &str, options: Option<SignUpWithPasswordOptions>, ) -> Result<Session, Error>
Sign up a new user with an email and password
§Example
let session = auth_client
.sign_up_with_phone_and_password(demo_phone, demo_password)
.await
.unwrap();
assert!(session.user.phone == demo_phone)
Sourcepub async fn login_anonymously(
&self,
options: Option<LoginAnonymouslyOptions>,
) -> Result<Session, Error>
pub async fn login_anonymously( &self, options: Option<LoginAnonymouslyOptions>, ) -> Result<Session, Error>
Sign in a new user anonymously. This actually signs up a user, but it’s
called “sign in” by Supabase in their own client, so that’s why it’s
named like this here. You can also pass in the same signup options
that work for the other sign_up_*
methods, but that’s not required.
This method requires anonymous sign in to be enabled in your dashboard.
§Example
let session = auth_client
.login_anonymously(demo_options)
.await
.unwrap();
assert!(session.user.user_metadata.display_name == demo_options.data.display_name)
Sourcepub async fn send_login_email_with_magic_link(
&self,
email: &str,
) -> Result<(), Error>
pub async fn send_login_email_with_magic_link( &self, email: &str, ) -> Result<(), Error>
Sends a login email containing a magic link
§Example
let _response = auth_client
.send_login_email_with_magic_link(demo_email)
.await
.unwrap();
Sourcepub async fn send_sms_with_otp(&self, phone: &str) -> Result<OTPResponse, Error>
pub async fn send_sms_with_otp(&self, phone: &str) -> Result<OTPResponse, Error>
Sourcepub async fn send_email_with_otp(
&self,
email: &str,
options: Option<LoginEmailOtpParams>,
) -> Result<OTPResponse, Error>
pub async fn send_email_with_otp( &self, email: &str, options: Option<LoginEmailOtpParams>, ) -> Result<OTPResponse, Error>
Send a Login OTP via email
Returns an OTPResponse on success
§Example
let send = auth_client.send_sms_with_otp(demo_phone).await.unwrap();
Sourcepub fn login_with_oauth(
&self,
provider: Provider,
options: Option<LoginWithOAuthOptions>,
) -> Result<OAuthResponse, Error>
pub fn login_with_oauth( &self, provider: Provider, options: Option<LoginWithOAuthOptions>, ) -> Result<OAuthResponse, Error>
Sign in a user using an OAuth provider.
§Example
// You can add custom parameters using a HashMap<String, String>
let mut params = HashMap::new();
params.insert("key".to_string(), "value".to_string());
let options = LoginWithOAuthOptions {
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
.login_with_oauth(supabase_auth::models::Provider::Github, Some(options))
.unwrap();
Sourcepub fn sign_up_with_oauth(
&self,
provider: Provider,
options: Option<LoginWithOAuthOptions>,
) -> Result<OAuthResponse, Error>
pub fn sign_up_with_oauth( &self, provider: Provider, options: Option<LoginWithOAuthOptions>, ) -> Result<OAuthResponse, Error>
Sign up a user using an OAuth provider.
§Example
// You can add custom parameters using a HashMap<String, String>
let mut params = HashMap::new();
params.insert("key".to_string(), "value".to_string());
let options = LoginWithOAuthOptions {
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_up_with_oauth(supabase_auth::models::Provider::Github, Some(options))
.unwrap();
Sourcepub async fn get_user(&self, bearer_token: &str) -> Result<User, Error>
pub async fn get_user(&self, bearer_token: &str) -> Result<User, Error>
Return the signed in User
§Example
let user = auth_client
.get_user(session.unwrap().access_token)
.await
.unwrap();
assert!(user.email == demo_email)
Sourcepub async fn update_user(
&self,
updated_user: UpdatedUser,
bearer_token: &str,
) -> Result<User, Error>
pub async fn update_user( &self, updated_user: UpdatedUser, bearer_token: &str, ) -> Result<User, Error>
Update the user, such as changing email or password. Each field (email, password, and data) is optional
§Example
let updated_user_data = UpdateUserPayload {
email: Some("demo@demo.com".to_string()),
password: Some("demo_password".to_string()),
data: None, // This field can hold any valid JSON value
};
let user = auth_client
.update_user(updated_user_data, access_token)
.await
.unwrap();
Sourcepub async fn login_with_id_token(
&self,
credentials: IdTokenCredentials,
) -> Result<Session, Error>
pub async fn login_with_id_token( &self, credentials: IdTokenCredentials, ) -> Result<Session, Error>
Allows signing in with an OIDC ID token. The authentication provider used should be enabled and configured.
§Example
let credentials = IdTokenCredentials {
provider: Provider::Github,
token: "<id-token-from-auth-provider>",
};
let session = auth_client
.login_with_id_token(credentials)
.await
.unwrap();
Sourcepub async fn invite_user_by_email(
&self,
email: &str,
data: Option<Value>,
bearer_token: &str,
) -> Result<User, Error>
pub async fn invite_user_by_email( &self, email: &str, data: Option<Value>, bearer_token: &str, ) -> Result<User, Error>
Sends an invite link to an email address. Requires admin permissions to issue invites
The data field corresponds to the raw_user_meta_data
User field
§Example
let demo_email = env::var("DEMO_INVITE").unwrap();
let user = auth_client
.invite_user_by_email(&demo_email, None, auth_client.api_key())
.await
.unwrap();
Sourcepub async fn verify_otp(
&self,
params: VerifyOtpParams,
) -> Result<Session, Error>
pub async fn verify_otp( &self, params: VerifyOtpParams, ) -> Result<Session, Error>
Verify the OTP sent to the user
§Example
let params = VerifyEmailOtpParams {
token: "abc123",
otp_type: OtpType::EmailChange,
options: None,
};
let session = auth_client
.verify_otp(params)
.await
.unwrap();
Sourcepub async fn get_health(&self) -> Result<AuthServerHealth, Error>
pub async fn get_health(&self) -> Result<AuthServerHealth, Error>
Check the Health Status of the Auth Server
§Example
let health = auth_client
.get_health()
.await
.unwrap();
Sourcepub async fn get_settings(&self) -> Result<AuthServerSettings, Error>
pub async fn get_settings(&self) -> Result<AuthServerSettings, Error>
Retrieve the public settings of the server
§Example
let settings = auth_client
.get_settings()
.await
.unwrap();
Sourcepub async fn exchange_token_for_session(
&self,
refresh_token: &str,
) -> Result<Session, Error>
pub async fn exchange_token_for_session( &self, refresh_token: &str, ) -> Result<Session, Error>
Exchange refresh token for a new session
§Example
// When a user signs in they get a session
let original_session = auth_client
.login_with_email_and_password(demo_email.as_ref(), demo_password)
.await
.unwrap();
// Exchange the refresh token from the original session to create a new session
let new_session = auth_client
.refresh_session(original_session.refresh_token)
.await
.unwrap();
pub async fn refresh_session( &self, refresh_token: &str, ) -> Result<Session, Error>
Sourcepub async fn reset_password_for_email(
&self,
email: &str,
options: Option<ResetPasswordOptions>,
) -> Result<(), Error>
pub async fn reset_password_for_email( &self, email: &str, options: Option<ResetPasswordOptions>, ) -> Result<(), Error>
Send a password recovery email. Invalid Email addresses will return Error Code 400. Valid email addresses that are not registered as users will not return an error.
§Example
let response = auth_client.reset_password_for_email(demo_email, None).await.unwrap();
Sourcepub async fn resend(&self, credentials: ResendParams) -> Result<(), Error>
pub async fn resend(&self, credentials: ResendParams) -> Result<(), Error>
Resends emails for existing signup confirmation, email change, SMS OTP, or phone change OTP.
§Example
// Resend can also take MobileResendParams
let credentials = DesktopResendParams {
otp_type: supabase_auth::models::EmailOtpType::Email,
email: demo_email.to_owned(),
options: None,
};
let resend = auth_client.resend(ResendParams::Desktop(credentials)).await;
Sourcepub async fn logout(
&self,
scope: Option<LogoutScope>,
bearer_token: &str,
) -> Result<(), Error>
pub async fn logout( &self, scope: Option<LogoutScope>, bearer_token: &str, ) -> Result<(), Error>
Logs out a user with a given scope
§Example
auth_client.logout(Some(LogoutScope::Global), session.access_token).await.unwrap();
Sourcepub async fn sso(&self, params: LoginWithSSO) -> Result<Url, Error>
pub async fn sso(&self, params: LoginWithSSO) -> Result<Url, Error>
Initiates an SSO Login Flow Returns the URL where the user must authenticate with the SSO Provider
WARNING: Requires an SSO Provider and Supabase Pro plan
§Example
let url = auth_client.sso(params).await.unwrap();
println!("{}", url.to_string());
Sourcepub fn project_url(&self) -> &str
pub fn project_url(&self) -> &str
Get the project URL from an AuthClient
Sourcepub fn jwt_secret(&self) -> &str
pub fn jwt_secret(&self) -> &str
Get the JWT Secret from an AuthClient
Trait Implementations§
Source§impl Clone for AuthClient
impl Clone for AuthClient
Source§fn clone(&self) -> AuthClient
fn clone(&self) -> AuthClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more