A Rust client library for the Supabase Auth API.
- π Full authentication flow support (signup, signin, logout)
- π Token management (refresh, validation)
- π€ User management (get user, update, delete)
- π‘οΈ Admin operations with service role key
- π Async/await support with Tokio
- π Comprehensive error handling
- π― Type-safe API
Add this to your Cargo.toml
:
[dependencies]
supabase-auth-redux = "0.1.0"
use supabase_auth_redux::{AuthClient, AuthError, IdType};
#[tokio::main]
async fn main() -> Result<(), AuthError> {
// Initialize the client
let auth_client = AuthClient::new(
"https://your-project.supabase.co",
"your-anon-key"
)?;
// Sign up a new user
let (user, access_token) = auth_client
.signup(
IdType::Email("[email protected]".to_string()),
"secure_password".to_string(),
None,
)
.await?;
println!("User created: {}", user.id);
// Sign in an existing user
let token_response = auth_client
.signin_with_password(
IdType::Email("[email protected]".to_string()),
"secure_password".to_string(),
)
.await?;
println!("Access token: {}", token_response.access_token);
Ok(())
}
For more control over the client configuration, use the builder pattern:
use supabase_auth_rs::AuthClient;
let auth_client = AuthClient::builder()
.api_url("https://your-project.supabase.co")
.anon_key("your-anon-key")
.service_role_key("your-service-role-key") // Optional: for admin operations
.build()?;
// Get user by access token
let user = auth_client
.get_user_by_token(&access_token)
.await?;
// Refresh tokens
let new_tokens = auth_client
.refresh_token(&refresh_token)
.await?;
// Logout
auth_client
.logout(&access_token)
.await?;
Admin operations require a service role key:
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()?;
// Hard delete a user (requires service role key)
admin_client
.hard_delete_user(user_id)
.await?;
// Soft delete a user (marks as deleted but keeps data)
admin_client
.soft_delete_user(user_id)
.await?;
signup()
- Create a new user accountsignin_with_password()
- Sign in with email/phone and passwordlogout()
- Sign out a user
refresh_token()
- Refresh access tokensget_user_by_token()
- Validate a token and get user info
get_user_by_id()
- Get user by UUID (requires service role key)hard_delete_user()
- Permanently delete a user accountsoft_delete_user()
- Mark user as deleted but keep data
The library provides a comprehensive AuthError
enum for different error scenarios:
use supabase_auth_rs::AuthError;
match auth_client.signin_with_password(id_type, password).await {
Ok(token_response) => {
println!("Signed in successfully!");
}
Err(AuthError::NotAuthorized) => {
println!("Invalid credentials");
}
Err(AuthError::InvalidParameters) => {
println!("Invalid input parameters");
}
Err(e) => {
println!("An error occurred: {}", e);
}
}
- Rust 1.70 or later
- Tokio runtime
Tests require a local Supabase instance:
# Start local Supabase
supabase start
# Run tests
cargo test
# Run tests with service role key (for admin operations)
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key cargo test
Check out the examples directory for more detailed usage examples:
# Run the local Supabase example
cargo run --example local_supabase
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
- Built for use with Supabase
- Inspired by the official Supabase JavaScript client