Skip to main content

better_auth_core/wire/
account.rs

1use super::*;
2/// Public account response shape.
3#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
4pub struct AccountView {
5    pub id: String,
6    #[serde(rename = "accountId")]
7    pub account_id: String,
8    #[serde(rename = "providerId")]
9    pub provider_id: String,
10    #[serde(rename = "userId")]
11    pub user_id: String,
12    #[serde(rename = "accessToken")]
13    pub access_token: Option<String>,
14    #[serde(rename = "refreshToken")]
15    pub refresh_token: Option<String>,
16    #[serde(rename = "idToken")]
17    pub id_token: Option<String>,
18    #[serde(rename = "accessTokenExpiresAt")]
19    #[serde(serialize_with = "crate::utils::datetime::serialize_optional")]
20    pub access_token_expires_at: Option<DateTime<Utc>>,
21    #[serde(rename = "refreshTokenExpiresAt")]
22    #[serde(serialize_with = "crate::utils::datetime::serialize_optional")]
23    pub refresh_token_expires_at: Option<DateTime<Utc>>,
24    pub scope: Option<String>,
25    #[serde(skip_serializing)]
26    pub password: Option<String>,
27    #[serde(rename = "createdAt")]
28    #[serde(serialize_with = "crate::utils::datetime::serialize")]
29    pub created_at: DateTime<Utc>,
30    #[serde(rename = "updatedAt")]
31    #[serde(serialize_with = "crate::utils::datetime::serialize")]
32    pub updated_at: DateTime<Utc>,
33}
34
35impl<T: AuthAccount> From<&T> for AccountView {
36    fn from(account: &T) -> Self {
37        Self {
38            id: account.id().into_owned(),
39            account_id: account.account_id().to_owned(),
40            provider_id: account.provider_id().to_owned(),
41            user_id: account.user_id().into_owned(),
42            access_token: account.access_token().map(str::to_owned),
43            refresh_token: account.refresh_token().map(str::to_owned),
44            id_token: account.id_token().map(str::to_owned),
45            access_token_expires_at: account.access_token_expires_at(),
46            refresh_token_expires_at: account.refresh_token_expires_at(),
47            scope: account.scope().map(str::to_owned),
48            password: account.password().map(str::to_owned),
49            created_at: account.created_at(),
50            updated_at: account.updated_at(),
51        }
52    }
53}
54
55impl AuthAccount for AccountView {
56    fn id(&self) -> Cow<'_, str> {
57        Cow::Borrowed(&self.id)
58    }
59    fn account_id(&self) -> &str {
60        &self.account_id
61    }
62    fn provider_id(&self) -> &str {
63        &self.provider_id
64    }
65    fn user_id(&self) -> Cow<'_, str> {
66        Cow::Borrowed(&self.user_id)
67    }
68    fn access_token(&self) -> Option<&str> {
69        self.access_token.as_deref()
70    }
71    fn refresh_token(&self) -> Option<&str> {
72        self.refresh_token.as_deref()
73    }
74    fn id_token(&self) -> Option<&str> {
75        self.id_token.as_deref()
76    }
77    fn access_token_expires_at(&self) -> Option<DateTime<Utc>> {
78        self.access_token_expires_at
79    }
80    fn refresh_token_expires_at(&self) -> Option<DateTime<Utc>> {
81        self.refresh_token_expires_at
82    }
83    fn scope(&self) -> Option<&str> {
84        self.scope.as_deref()
85    }
86    fn password(&self) -> Option<&str> {
87        self.password.as_deref()
88    }
89    fn created_at(&self) -> DateTime<Utc> {
90        self.created_at
91    }
92    fn updated_at(&self) -> DateTime<Utc> {
93        self.updated_at
94    }
95}