Skip to main content

rs_auth_core/
store.rs

1use async_trait::async_trait;
2
3use crate::error::AuthError;
4use crate::types::{Account, NewAccount, NewSession, NewVerification, Session, User, Verification};
5
6/// Storage backend for user records.
7#[async_trait]
8pub trait UserStore: Send + Sync {
9    async fn create_user(
10        &self,
11        email: &str,
12        name: Option<&str>,
13        password_hash: Option<&str>,
14    ) -> Result<User, AuthError>;
15
16    async fn find_by_email(&self, email: &str) -> Result<Option<User>, AuthError>;
17
18    async fn find_by_id(&self, id: i64) -> Result<Option<User>, AuthError>;
19
20    async fn set_email_verified(&self, user_id: i64) -> Result<(), AuthError>;
21
22    async fn update_password(&self, user_id: i64, password_hash: &str) -> Result<(), AuthError>;
23
24    async fn delete_user(&self, user_id: i64) -> Result<(), AuthError>;
25}
26
27/// Storage backend for session records.
28#[async_trait]
29pub trait SessionStore: Send + Sync {
30    async fn create_session(&self, session: NewSession) -> Result<Session, AuthError>;
31
32    async fn find_by_token_hash(&self, token_hash: &str) -> Result<Option<Session>, AuthError>;
33
34    async fn find_by_user_id(&self, user_id: i64) -> Result<Vec<Session>, AuthError>;
35
36    async fn delete_session(&self, id: i64) -> Result<(), AuthError>;
37
38    async fn delete_by_user_id(&self, user_id: i64) -> Result<(), AuthError>;
39
40    async fn delete_expired(&self) -> Result<u64, AuthError>;
41}
42
43/// Storage backend for verification token records.
44#[async_trait]
45pub trait VerificationStore: Send + Sync {
46    async fn create_verification(
47        &self,
48        verification: NewVerification,
49    ) -> Result<Verification, AuthError>;
50
51    async fn find_by_identifier(&self, identifier: &str)
52    -> Result<Option<Verification>, AuthError>;
53
54    async fn find_by_token_hash(&self, token_hash: &str)
55    -> Result<Option<Verification>, AuthError>;
56
57    async fn delete_verification(&self, id: i64) -> Result<(), AuthError>;
58
59    async fn delete_by_identifier(&self, identifier: &str) -> Result<(), AuthError>;
60
61    async fn delete_expired(&self) -> Result<u64, AuthError>;
62}
63
64/// Storage backend for OAuth account records.
65#[async_trait]
66pub trait AccountStore: Send + Sync {
67    async fn create_account(&self, account: NewAccount) -> Result<Account, AuthError>;
68    async fn find_by_provider(
69        &self,
70        provider_id: &str,
71        account_id: &str,
72    ) -> Result<Option<Account>, AuthError>;
73    async fn find_by_user_id(&self, user_id: i64) -> Result<Vec<Account>, AuthError>;
74    async fn delete_account(&self, id: i64) -> Result<(), AuthError>;
75}