rive_http/authentication/
account.rs

1use crate::prelude::*;
2use rive_models::{
3    account::{AccountInfo, EmailVerification},
4    data::{
5        ChangeEmailData, ChangePasswordData, ConfirmAccountDeletionData, CreateAccountData,
6        PasswordResetData, ResendVerificationData, SendPasswordResetData,
7    },
8};
9
10impl Client {
11    /// Create a new account.
12    pub async fn create_account(&self, data: CreateAccountData) -> Result<()> {
13        self.client
14            .post(ep!(self, "/auth/account/create"))
15            .json(&data)
16            .send()
17            .await?
18            .process_error()
19            .await?;
20        Ok(())
21    }
22
23    /// Resend account creation verification email.
24    pub async fn resend_verification(&self, data: ResendVerificationData) -> Result<()> {
25        self.client
26            .post(ep!(self, "/auth/account/reverify"))
27            .json(&data)
28            .send()
29            .await?
30            .process_error()
31            .await?;
32        Ok(())
33    }
34
35    /// Schedule an account for deletion by confirming the received token.
36    pub async fn confirm_account_deletion(&self, data: ConfirmAccountDeletionData) -> Result<()> {
37        self.client
38            .put(ep!(self, "/auth/account/delete"))
39            .json(&data)
40            .send()
41            .await?
42            .process_error()
43            .await?;
44        Ok(())
45    }
46
47    /// Request to have an account deleted.
48    pub async fn delete_account(&self) -> Result<()> {
49        self.client
50            .post(ep!(self, "/auth/account/delete"))
51            .auth(&self.authentication)
52            .send()
53            .await?
54            .process_error()
55            .await?;
56        Ok(())
57    }
58
59    /// Fetch account information from the current session.
60    pub async fn fetch_account(&self) -> Result<AccountInfo> {
61        Ok(self
62            .client
63            .get(ep!(self, "/auth/account/"))
64            .auth(&self.authentication)
65            .send()
66            .await?
67            .process_error()
68            .await?
69            .json()
70            .await?)
71    }
72
73    /// Disable an account.
74    pub async fn disable_account(&self) -> Result<()> {
75        self.client
76            .post(ep!(self, "/auth/account/disable"))
77            .auth(&self.authentication)
78            .send()
79            .await?
80            .process_error()
81            .await?;
82        Ok(())
83    }
84
85    /// Change the current account password.
86    pub async fn change_password(&self, data: ChangePasswordData) -> Result<()> {
87        self.client
88            .patch(ep!(self, "/auth/account/change/password"))
89            .json(&data)
90            .auth(&self.authentication)
91            .send()
92            .await?
93            .process_error()
94            .await?;
95        Ok(())
96    }
97
98    /// Change the associated account email.
99    pub async fn change_email(&self, data: ChangeEmailData) -> Result<()> {
100        self.client
101            .patch(ep!(self, "/auth/account/change/email"))
102            .json(&data)
103            .auth(&self.authentication)
104            .send()
105            .await?
106            .process_error()
107            .await?;
108        Ok(())
109    }
110
111    /// Change the associated account email.
112    pub async fn verify_email(&self, code: impl Into<String>) -> Result<EmailVerification> {
113        Ok(self
114            .client
115            .post(ep!(self, "/auth/account/verify/{}", code.into()))
116            .send()
117            .await?
118            .process_error()
119            .await?
120            .json()
121            .await?)
122    }
123
124    /// Send an email to reset account password.
125    pub async fn send_password_reset(&self, data: SendPasswordResetData) -> Result<()> {
126        self.client
127            .post(ep!(self, "/auth/account/reset_password"))
128            .json(&data)
129            .send()
130            .await?
131            .process_error()
132            .await?;
133        Ok(())
134    }
135
136    /// Confirm password reset and change the password.
137    pub async fn password_reset(&self, data: PasswordResetData) -> Result<()> {
138        self.client
139            .patch(ep!(self, "/auth/account/reset_password"))
140            .json(&data)
141            .send()
142            .await?
143            .process_error()
144            .await?;
145        Ok(())
146    }
147}