Skip to content

j7nw4r/supabase-auth-redux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

supabase-auth-redux

Crates.io Documentation License CI

A Rust client library for the Supabase Auth API.

Features

  • πŸ” 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

Installation

Add this to your Cargo.toml:

[dependencies]
supabase-auth-redux = "0.1.0"

Quick Start

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(())
}

Advanced Usage

Using the Builder Pattern

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()?;

User Management

// 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

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?;

API Reference

Authentication Methods

  • signup() - Create a new user account
  • signin_with_password() - Sign in with email/phone and password
  • logout() - Sign out a user

Token Management

  • refresh_token() - Refresh access tokens
  • get_user_by_token() - Validate a token and get user info

User Management

  • get_user_by_id() - Get user by UUID (requires service role key)
  • hard_delete_user() - Permanently delete a user account
  • soft_delete_user() - Mark user as deleted but keep data

Error Handling

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);
    }
}

Requirements

  • Rust 1.70 or later
  • Tokio runtime

Development

Running Tests

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

Examples

Check out the examples directory for more detailed usage examples:

# Run the local Supabase example
cargo run --example local_supabase

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under either of

at your option.

Acknowledgments

  • Built for use with Supabase
  • Inspired by the official Supabase JavaScript client

Links

About

Use Supabase Auth in your rust backend.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published