pub struct AuthClient { /* private fields */ }
Expand description
The main authentication client for interacting with Supabase Auth API
This client handles all authentication operations including user signup, signin, token management, and user administration.
Implementations§
Source§impl AuthClient
impl AuthClient
Sourcepub async fn soft_delete_user(&self, user_id: Uuid) -> Result<(), AuthError>
pub async fn soft_delete_user(&self, user_id: Uuid) -> Result<(), AuthError>
Soft deletes a user, marking them as deleted but preserving their data
This operation requires a service role key to be configured on the AuthClient. The user will be marked as deleted but their data will be retained in the database.
§Arguments
user_id
- The UUID of the user to soft delete
§Errors
Returns AuthError::ServiceRoleKeyRequired
if no service role key is configured.
Returns AuthError::Http
if the API request fails.
§Example
let admin_client = AuthClient::builder()
.api_url("https://your-project.supabase.co")
.anon_key("your-anon-key")
.service_role_key("your-service-role-key")
.build()?;
let user_id = Uuid::parse_str("123e4567-e89b-12d3-a456-426614174000").unwrap();
admin_client.soft_delete_user(user_id).await?;
Sourcepub async fn hard_delete_user(&self, user_id: Uuid) -> Result<(), AuthError>
pub async fn hard_delete_user(&self, user_id: Uuid) -> Result<(), AuthError>
Permanently deletes a user and all their associated data
This operation requires a service role key to be configured on the AuthClient. The user and all their data will be permanently removed from the database. This action cannot be undone.
§Arguments
user_id
- The UUID of the user to permanently delete
§Errors
Returns AuthError::ServiceRoleKeyRequired
if no service role key is configured.
Returns AuthError::Http
if the API request fails.
§Example
let admin_client = AuthClient::builder()
.api_url("https://your-project.supabase.co")
.anon_key("your-anon-key")
.service_role_key("your-service-role-key")
.build()?;
let user_id = Uuid::parse_str("123e4567-e89b-12d3-a456-426614174000").unwrap();
admin_client.hard_delete_user(user_id).await?;
Source§impl AuthClient
impl AuthClient
Sourcepub async fn get_user_by_token(
&self,
auth_token: &str,
) -> Result<UserSchema, AuthError>
pub async fn get_user_by_token( &self, auth_token: &str, ) -> Result<UserSchema, AuthError>
Retrieves user information using an authentication token
This method validates the provided access token and returns the associated user’s information. It’s commonly used to verify that a token is valid and to get the current user’s details.
§Arguments
auth_token
- A valid JWT access token
§Errors
Returns AuthError::InvalidParameters
if the token is empty.
Returns AuthError::NotAuthorized
if the token is invalid or expired.
Returns AuthError::Http
if the API request fails.
§Example
let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")?;
// After user signs in and you have their access token
let user = client.get_user_by_token("user-access-token").await?;
println!("User email: {:?}", user.email);
Sourcepub async fn get_user_by_id(
&self,
user_id: Uuid,
) -> Result<Option<UserSchema>, AuthError>
pub async fn get_user_by_id( &self, user_id: Uuid, ) -> Result<Option<UserSchema>, AuthError>
Retrieves user information by user ID
This method fetches a user’s information directly from the database using their UUID. Note: This requires appropriate permissions and may need a service role key depending on your Row Level Security policies.
§Arguments
user_id
- The UUID of the user to retrieve
§Returns
Returns Ok(Some(user))
if the user exists, Ok(None)
if not found.
§Errors
Returns AuthError::Http
if the database query fails.
§Example
let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")?;
let user_id = Uuid::parse_str("123e4567-e89b-12d3-a456-426614174000").unwrap();
if let Some(user) = client.get_user_by_id(user_id).await? {
println!("Found user: {:?}", user.email);
}
Source§impl AuthClient
impl AuthClient
Sourcepub async fn logout(&self, token: &str) -> Result<(), AuthError>
pub async fn logout(&self, token: &str) -> Result<(), AuthError>
Logs out a user by invalidating their authentication token
This method revokes the provided access token, effectively logging the user out. After calling this method, the token will no longer be valid for authentication.
§Arguments
token
- The access token to invalidate
§Errors
Returns AuthError::Http
if the API request fails.
§Example
let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")?;
// After user signs in and you have their access token
let access_token = "user-access-token";
client.logout(access_token).await?;
// The token is now invalid and cannot be used for authentication
Source§impl AuthClient
impl AuthClient
Sourcepub async fn refresh_token(
&self,
token: &str,
) -> Result<TokenResponse, AuthError>
pub async fn refresh_token( &self, token: &str, ) -> Result<TokenResponse, AuthError>
Refreshes an authentication token to obtain new access and refresh tokens
This method exchanges a valid refresh token for a new set of tokens, extending the user’s session without requiring them to sign in again. The new access token can be used for API authentication.
§Arguments
token
- A valid refresh token obtained from signin or a previous refresh
§Returns
Returns a TokenResponse
containing new access and refresh tokens.
§Errors
Returns AuthError::InvalidParameters
if the token is empty.
Returns AuthError::NotAuthorized
if the refresh token is invalid or expired.
Returns AuthError::Http
if the API request fails.
§Example
let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")?;
// After user signs in, you have their refresh token
let refresh_token = "user-refresh-token";
let new_tokens = client.refresh_token(refresh_token).await?;
println!("New access token: {}", new_tokens.access_token);
println!("New refresh token: {}", new_tokens.refresh_token);
Source§impl AuthClient
impl AuthClient
Sourcepub async fn signin_with_password(
&self,
id: IdType,
password: String,
) -> Result<TokenResponse, AuthError>
pub async fn signin_with_password( &self, id: IdType, password: String, ) -> Result<TokenResponse, AuthError>
Signs in a user with their email/phone and password
This method authenticates a user using their credentials and returns authentication tokens that can be used for subsequent API requests.
§Arguments
id
- The user’s identifier (email or phone number)password
- The user’s password
§Returns
Returns a TokenResponse
containing access and refresh tokens on successful authentication.
§Errors
Returns AuthError::InvalidParameters
if email/phone or password is empty.
Returns AuthError::NotAuthorized
if credentials are invalid.
Returns AuthError::Http
if the API request fails.
§Example
let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")?;
let tokens = client
.signin_with_password(
IdType::Email("user@example.com".to_string()),
"secure_password".to_string(),
)
.await?;
println!("Access token: {}", tokens.access_token);
Source§impl AuthClient
impl AuthClient
Sourcepub async fn signup(
&self,
signup_id_type: IdType,
password: String,
_metadata: Option<HashMap<String, String>>,
) -> Result<(UserSchema, String), AuthError>
pub async fn signup( &self, signup_id_type: IdType, password: String, _metadata: Option<HashMap<String, String>>, ) -> Result<(UserSchema, String), AuthError>
Creates a new user account
This method registers a new user with the provided credentials and optional metadata. Upon successful registration, the user is automatically signed in and authentication tokens are returned.
§Arguments
signup_id_type
- The user’s identifier (email or phone number)password
- The desired password for the accountmetadata
- Optional user metadata to store with the account
§Returns
Returns a tuple containing:
- The newly created
UserSchema
with user information - An access token string for immediate authentication
§Errors
Returns AuthError::InvalidParameters
if required fields are missing.
Returns AuthError::Http
if the API request fails or user already exists.
§Example
let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")?;
let mut metadata = HashMap::new();
metadata.insert("first_name".to_string(), "John".to_string());
metadata.insert("last_name".to_string(), "Doe".to_string());
let (user, access_token) = client
.signup(
IdType::Email("newuser@example.com".to_string()),
"secure_password".to_string(),
Some(metadata),
)
.await?;
println!("User created with ID: {}", user.id);
Source§impl AuthClient
impl AuthClient
Sourcepub fn new(api_url: &str, anon_key: &str) -> Result<Self, AuthError>
pub fn new(api_url: &str, anon_key: &str) -> Result<Self, AuthError>
Creates a new authentication client with the given API URL and anonymous key
§Arguments
api_url
- The base URL of your Supabase instanceanon_key
- The anonymous key for your Supabase project
§Example
use supabase_auth_redux::AuthClient;
let client = AuthClient::new("https://your-project.supabase.co", "your-anon-key")
.expect("Failed to create auth client");
Sourcepub fn builder() -> AuthClientBuilder
pub fn builder() -> AuthClientBuilder
Creates a new builder for constructing an AuthClient with additional options
§Example
use supabase_auth_redux::AuthClient;
let client = AuthClient::builder()
.api_url("https://your-project.supabase.co")
.anon_key("your-anon-key")
.service_role_key("your-service-role-key")
.build()
.expect("Failed to create auth client");
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