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