-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
98 lines (89 loc) · 2.63 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Supabase Auth client library for Rust
//!
//! This crate provides a Rust interface to the Supabase Auth API.
//! It handles authentication operations like signup, signin, token refresh,
//! and user management.
use std::fmt::{Display, Formatter};
use postgrest::Postgrest;
use serde::{Deserialize, Serialize};
use thiserror::Error;
pub use error::AuthError;
pub use models::user::UserSchema as User;
#[allow(unused)]
pub use ErrorSchema as Error;
mod error;
mod get_user;
mod logout;
pub mod models;
mod refresh_token;
mod signin_with_password;
mod signup;
mod util;
mod delete_user;
/// Main client for interacting with Supabase Auth
#[derive(Clone, Debug)]
pub struct AuthClient {
/// HTTP client for making requests
http_client: reqwest::Client,
/// Base URL for the Supabase API
supabase_api_url: String,
/// Anonymous API key for authentication
supabase_anon_key: String,
/// Client for making PostgreSQL REST API calls
#[allow(unused)]
#[derive(Debug)]
postgrest_client: Postgrest,
}
impl AuthClient {
/// Creates a new AuthClient instance
///
/// # Arguments
/// * `api_url` - Base URL for the Supabase API
/// * `anon_key` - Anonymous API key for authentication
///
/// # Returns
/// * `Result<Self, anyhow::Error>` - New client instance or error
pub fn new(api_url: &str, anon_key: &str) -> anyhow::Result<Self> {
Ok(Self {
http_client: reqwest::Client::new(),
supabase_api_url: api_url.to_owned(),
supabase_anon_key: anon_key.to_owned(),
postgrest_client: Postgrest::new(format!("{}/rest/v1/", api_url.to_owned()))
.schema("auth")
.insert_header("apikey", anon_key.to_owned()),
})
}
}
/// Represents an error response from the Supabase Auth API
#[derive(Debug, Error, Deserialize, Serialize)]
pub struct ErrorSchema {
/// Numeric error code
pub code: Option<u8>,
/// Error type/name
pub error: Option<String>,
/// Detailed error description
pub error_description: Option<String>,
/// Error message
pub msg: Option<String>,
}
impl Display for ErrorSchema {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(ref e) = self.error {
f.write_str(e)?;
return Ok(());
}
if let Some(ref msg) = self.msg {
f.write_str(msg)?;
return Ok(());
}
Err(std::fmt::Error)
}
}
/// Types of user identifiers supported for authentication
#[derive(Debug)]
pub enum IdType {
/// Email address
Email(String),
/// Phone number
PhoneNumber(String),
}