axum_supabase_auth/auth/
mod.rs

1use crate::api::SignUpResponse;
2use crate::{EmailOrPhone, OAuthRequest, OAuthResponse, Session, User};
3use std::future::Future;
4use thiserror::Error;
5
6pub mod api;
7pub mod service;
8pub mod types;
9
10pub trait Auth: Clone + Send + Sync + 'static {
11    fn sign_up(
12        &self,
13        email_or_phone: EmailOrPhone,
14        password: impl AsRef<str> + Send,
15    ) -> impl Future<Output = Result<SignUpResponse, ClientError>> + Send;
16    fn sign_in(
17        &self,
18        email_or_phone: EmailOrPhone,
19        password: impl AsRef<str> + Send,
20    ) -> impl Future<Output = Result<Session, ClientError>> + Send;
21    fn exchange_code_for_session(
22        &self,
23        code: &str,
24        csrf_token_b64: &str,
25    ) -> impl Future<Output = Result<Session, ClientError>> + Send;
26
27    // TODO: move to axum?
28    fn create_oauth_url(&self, req: OAuthRequest) -> Result<OAuthResponse, ClientError>;
29
30    fn with_token(&self, access_token: String) -> impl SessionAuth;
31    fn with_refresh_token(&self, access_token: String, refresh_token: String) -> impl SessionAuth;
32}
33
34pub trait SessionAuth {
35    fn logout(&self) -> impl Future<Output = Result<(), ClientError>>;
36    fn list_users(&self) -> impl Future<Output = Result<Vec<User>, ClientError>>;
37    fn refresh(&mut self) -> impl Future<Output = Result<Session, ClientError>>;
38}
39
40#[derive(Debug, Error)]
41pub enum ClientError {
42    #[error("User already signed up")]
43    AlreadySignedUp,
44    #[error("Wrong credentials")]
45    WrongCredentials,
46    #[error("User not found")]
47    UserNotFound,
48    #[error("User not authenticated")]
49    NotAuthenticated,
50    #[error("Missing refresh token")]
51    MissingRefreshToken,
52    #[error("Wrong token")]
53    WrongToken,
54    #[error("GoTrue Internal error")]
55    InternalError,
56}