Skip to main content

better_auth_core/adapters/
traits.rs

1use async_trait::async_trait;
2use chrono::{DateTime, Utc};
3
4use crate::entity::{
5    AuthAccount, AuthInvitation, AuthMember, AuthOrganization, AuthSession, AuthUser,
6    AuthVerification,
7};
8use crate::error::AuthResult;
9use crate::types::{
10    CreateAccount, CreateInvitation, CreateMember, CreateOrganization, CreateSession, CreateUser,
11    CreateVerification, InvitationStatus, UpdateOrganization, UpdateUser,
12};
13
14/// User persistence operations.
15#[async_trait]
16pub trait UserOps: Send + Sync + 'static {
17    type User: AuthUser;
18
19    async fn create_user(&self, user: CreateUser) -> AuthResult<Self::User>;
20    async fn get_user_by_id(&self, id: &str) -> AuthResult<Option<Self::User>>;
21    async fn get_user_by_email(&self, email: &str) -> AuthResult<Option<Self::User>>;
22    async fn get_user_by_username(&self, username: &str) -> AuthResult<Option<Self::User>>;
23    async fn update_user(&self, id: &str, update: UpdateUser) -> AuthResult<Self::User>;
24    async fn delete_user(&self, id: &str) -> AuthResult<()>;
25}
26
27/// Session persistence operations.
28#[async_trait]
29pub trait SessionOps: Send + Sync + 'static {
30    type Session: AuthSession;
31
32    async fn create_session(&self, session: CreateSession) -> AuthResult<Self::Session>;
33    async fn get_session(&self, token: &str) -> AuthResult<Option<Self::Session>>;
34    async fn get_user_sessions(&self, user_id: &str) -> AuthResult<Vec<Self::Session>>;
35    async fn update_session_expiry(&self, token: &str, expires_at: DateTime<Utc>)
36    -> AuthResult<()>;
37    async fn delete_session(&self, token: &str) -> AuthResult<()>;
38    async fn delete_user_sessions(&self, user_id: &str) -> AuthResult<()>;
39    async fn delete_expired_sessions(&self) -> AuthResult<usize>;
40    async fn update_session_active_organization(
41        &self,
42        token: &str,
43        organization_id: Option<&str>,
44    ) -> AuthResult<Self::Session>;
45}
46
47/// Account (OAuth provider linking) persistence operations.
48#[async_trait]
49pub trait AccountOps: Send + Sync + 'static {
50    type Account: AuthAccount;
51
52    async fn create_account(&self, account: CreateAccount) -> AuthResult<Self::Account>;
53    async fn get_account(
54        &self,
55        provider: &str,
56        provider_account_id: &str,
57    ) -> AuthResult<Option<Self::Account>>;
58    async fn get_user_accounts(&self, user_id: &str) -> AuthResult<Vec<Self::Account>>;
59    async fn delete_account(&self, id: &str) -> AuthResult<()>;
60}
61
62/// Verification token persistence operations.
63#[async_trait]
64pub trait VerificationOps: Send + Sync + 'static {
65    type Verification: AuthVerification;
66
67    async fn create_verification(
68        &self,
69        verification: CreateVerification,
70    ) -> AuthResult<Self::Verification>;
71    async fn get_verification(
72        &self,
73        identifier: &str,
74        value: &str,
75    ) -> AuthResult<Option<Self::Verification>>;
76    async fn get_verification_by_value(
77        &self,
78        value: &str,
79    ) -> AuthResult<Option<Self::Verification>>;
80    async fn delete_verification(&self, id: &str) -> AuthResult<()>;
81    async fn delete_expired_verifications(&self) -> AuthResult<usize>;
82}
83
84/// Organization persistence operations.
85#[async_trait]
86pub trait OrganizationOps: Send + Sync + 'static {
87    type Organization: AuthOrganization;
88
89    async fn create_organization(&self, org: CreateOrganization) -> AuthResult<Self::Organization>;
90    async fn get_organization_by_id(&self, id: &str) -> AuthResult<Option<Self::Organization>>;
91    async fn get_organization_by_slug(&self, slug: &str) -> AuthResult<Option<Self::Organization>>;
92    async fn update_organization(
93        &self,
94        id: &str,
95        update: UpdateOrganization,
96    ) -> AuthResult<Self::Organization>;
97    async fn delete_organization(&self, id: &str) -> AuthResult<()>;
98    async fn list_user_organizations(&self, user_id: &str) -> AuthResult<Vec<Self::Organization>>;
99}
100
101/// Organization member persistence operations.
102#[async_trait]
103pub trait MemberOps: Send + Sync + 'static {
104    type Member: AuthMember;
105
106    async fn create_member(&self, member: CreateMember) -> AuthResult<Self::Member>;
107    async fn get_member(
108        &self,
109        organization_id: &str,
110        user_id: &str,
111    ) -> AuthResult<Option<Self::Member>>;
112    async fn get_member_by_id(&self, id: &str) -> AuthResult<Option<Self::Member>>;
113    async fn update_member_role(&self, member_id: &str, role: &str) -> AuthResult<Self::Member>;
114    async fn delete_member(&self, member_id: &str) -> AuthResult<()>;
115    async fn list_organization_members(
116        &self,
117        organization_id: &str,
118    ) -> AuthResult<Vec<Self::Member>>;
119    async fn count_organization_members(&self, organization_id: &str) -> AuthResult<usize>;
120    async fn count_organization_owners(&self, organization_id: &str) -> AuthResult<usize>;
121}
122
123/// Invitation persistence operations.
124#[async_trait]
125pub trait InvitationOps: Send + Sync + 'static {
126    type Invitation: AuthInvitation;
127
128    async fn create_invitation(&self, invitation: CreateInvitation)
129    -> AuthResult<Self::Invitation>;
130    async fn get_invitation_by_id(&self, id: &str) -> AuthResult<Option<Self::Invitation>>;
131    async fn get_pending_invitation(
132        &self,
133        organization_id: &str,
134        email: &str,
135    ) -> AuthResult<Option<Self::Invitation>>;
136    async fn update_invitation_status(
137        &self,
138        id: &str,
139        status: InvitationStatus,
140    ) -> AuthResult<Self::Invitation>;
141    async fn list_organization_invitations(
142        &self,
143        organization_id: &str,
144    ) -> AuthResult<Vec<Self::Invitation>>;
145    async fn list_user_invitations(&self, email: &str) -> AuthResult<Vec<Self::Invitation>>;
146}