pub struct Auth { /* private fields */ }
Expand description
Authentication client for handling user sessions and JWT tokens
Implementations§
Source§impl Auth
impl Auth
Sourcepub fn new(
config: Arc<SupabaseConfig>,
http_client: Arc<HttpClient>,
) -> Result<Self>
pub fn new( config: Arc<SupabaseConfig>, http_client: Arc<HttpClient>, ) -> Result<Self>
Create a new Auth instance
Sourcepub async fn sign_up_with_email_and_password(
&self,
email: &str,
password: &str,
) -> Result<AuthResponse>
pub async fn sign_up_with_email_and_password( &self, email: &str, password: &str, ) -> Result<AuthResponse>
Sign up a new user with email and password
Sourcepub async fn sign_up_with_email_password_and_data(
&self,
email: &str,
password: &str,
data: Option<Value>,
redirect_to: Option<String>,
) -> Result<AuthResponse>
pub async fn sign_up_with_email_password_and_data( &self, email: &str, password: &str, data: Option<Value>, redirect_to: Option<String>, ) -> Result<AuthResponse>
Sign up a new user with email, password, and optional metadata
Sourcepub async fn sign_in_with_email_and_password(
&self,
email: &str,
password: &str,
) -> Result<AuthResponse>
pub async fn sign_in_with_email_and_password( &self, email: &str, password: &str, ) -> Result<AuthResponse>
Sign in with email and password
Sourcepub async fn reset_password_for_email(&self, email: &str) -> Result<()>
pub async fn reset_password_for_email(&self, email: &str) -> Result<()>
Reset password via email
Sourcepub async fn reset_password_for_email_with_redirect(
&self,
email: &str,
redirect_to: Option<String>,
) -> Result<()>
pub async fn reset_password_for_email_with_redirect( &self, email: &str, redirect_to: Option<String>, ) -> Result<()>
Reset password via email with optional redirect URL
Sourcepub async fn update_user(
&self,
email: Option<String>,
password: Option<String>,
data: Option<Value>,
) -> Result<AuthResponse>
pub async fn update_user( &self, email: Option<String>, password: Option<String>, data: Option<Value>, ) -> Result<AuthResponse>
Update the current user’s information
Sourcepub async fn refresh_session(&self) -> Result<AuthResponse>
pub async fn refresh_session(&self) -> Result<AuthResponse>
Refresh the current session token
Sourcepub async fn current_user(&self) -> Result<Option<User>>
pub async fn current_user(&self) -> Result<Option<User>>
Get the current user information
Sourcepub fn get_session(&self) -> Result<Session>
pub fn get_session(&self) -> Result<Session>
Get the current session
Sourcepub async fn set_session(&self, session: Session) -> Result<()>
pub async fn set_session(&self, session: Session) -> Result<()>
Set a new session
Sourcepub async fn set_session_token(&self, token: &str) -> Result<()>
pub async fn set_session_token(&self, token: &str) -> Result<()>
Set session from JWT token
Sourcepub async fn clear_session(&self) -> Result<()>
pub async fn clear_session(&self) -> Result<()>
Clear the current session
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Check if the user is authenticated
Sourcepub fn needs_refresh(&self) -> bool
pub fn needs_refresh(&self) -> bool
Check if the current token needs refresh
Sourcepub async fn sign_in_with_oauth(
&self,
provider: OAuthProvider,
options: Option<OAuthOptions>,
) -> Result<OAuthResponse>
pub async fn sign_in_with_oauth( &self, provider: OAuthProvider, options: Option<OAuthOptions>, ) -> Result<OAuthResponse>
Sign in with OAuth provider
Returns a URL that the user should be redirected to for authentication. After successful authentication, the user will be redirected back with the session.
§Example
use supabase::auth::{OAuthProvider, OAuthOptions};
let client = supabase::Client::new("url", "key")?;
let options = OAuthOptions {
redirect_to: Some("https://myapp.com/callback".to_string()),
scopes: Some(vec!["email".to_string(), "profile".to_string()]),
..Default::default()
};
let response = client.auth().sign_in_with_oauth(OAuthProvider::Google, Some(options)).await?;
println!("Redirect to: {}", response.url);
Sourcepub async fn sign_up_with_phone(
&self,
phone: &str,
password: &str,
data: Option<Value>,
) -> Result<AuthResponse>
pub async fn sign_up_with_phone( &self, phone: &str, password: &str, data: Option<Value>, ) -> Result<AuthResponse>
Sign up with phone number
§Example
let client = supabase::Client::new("url", "key")?;
let response = client.auth()
.sign_up_with_phone("+1234567890", "securepassword", None)
.await?;
if let Some(user) = response.user {
println!("User created: {:?}", user.phone);
}
Sourcepub async fn sign_in_with_phone(
&self,
phone: &str,
password: &str,
) -> Result<AuthResponse>
pub async fn sign_in_with_phone( &self, phone: &str, password: &str, ) -> Result<AuthResponse>
Sign in with phone number
§Example
let client = supabase::Client::new("url", "key")?;
let response = client.auth()
.sign_in_with_phone("+1234567890", "securepassword")
.await?;
if let Some(user) = response.user {
println!("User signed in: {:?}", user.phone);
}
Sourcepub async fn verify_otp(
&self,
phone: &str,
token: &str,
verification_type: &str,
) -> Result<AuthResponse>
pub async fn verify_otp( &self, phone: &str, token: &str, verification_type: &str, ) -> Result<AuthResponse>
Verify OTP token
§Example
let client = supabase::Client::new("url", "key")?;
let response = client.auth()
.verify_otp("+1234567890", "123456", "sms")
.await?;
if let Some(session) = response.session {
println!("OTP verified, user signed in");
}
Sourcepub async fn sign_in_with_magic_link(
&self,
email: &str,
redirect_to: Option<String>,
data: Option<Value>,
) -> Result<()>
pub async fn sign_in_with_magic_link( &self, email: &str, redirect_to: Option<String>, data: Option<Value>, ) -> Result<()>
Send magic link for passwordless authentication
§Example
let client = supabase::Client::new("url", "key")?;
client.auth()
.sign_in_with_magic_link("user@example.com", Some("https://myapp.com/callback".to_string()), None)
.await?;
println!("Magic link sent to email");
Sourcepub async fn sign_in_anonymously(
&self,
data: Option<Value>,
) -> Result<AuthResponse>
pub async fn sign_in_anonymously( &self, data: Option<Value>, ) -> Result<AuthResponse>
Sign in anonymously
Creates a temporary anonymous user session that can be converted to a permanent account later.
§Example
let client = supabase::Client::new("url", "key")?;
let response = client.auth()
.sign_in_anonymously(None)
.await?;
if let Some(user) = response.user {
println!("Anonymous user created: {}", user.id);
}
Sourcepub async fn reset_password_for_email_enhanced(
&self,
email: &str,
redirect_to: Option<String>,
) -> Result<()>
pub async fn reset_password_for_email_enhanced( &self, email: &str, redirect_to: Option<String>, ) -> Result<()>
Enhanced password recovery with custom redirect and options
§Example
let client = supabase::Client::new("url", "key")?;
client.auth()
.reset_password_for_email_enhanced("user@example.com", Some("https://myapp.com/reset".to_string()))
.await?;
println!("Password reset email sent");
Sourcepub fn on_auth_state_change<F>(&self, callback: F) -> AuthEventHandle
pub fn on_auth_state_change<F>(&self, callback: F) -> AuthEventHandle
Subscribe to authentication state changes
Returns a handle that can be used to remove the listener later.
§Example
use supabase::auth::AuthEvent;
let client = supabase::Client::new("url", "key")?;
let handle = client.auth().on_auth_state_change(|event, session| {
match event {
AuthEvent::SignedIn => {
if let Some(session) = session {
println!("User signed in: {}", session.user.email.unwrap_or_default());
}
}
AuthEvent::SignedOut => println!("User signed out"),
AuthEvent::TokenRefreshed => println!("Token refreshed"),
_ => {}
}
});
// Later remove the listener
handle.remove();
Sourcepub fn remove_auth_listener(&self, id: Uuid)
pub fn remove_auth_listener(&self, id: Uuid)
Remove an authentication state listener