1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::prelude::*;
use rive_models::{
    account::{AccountInfo, EmailVerification},
    payload::{
        ChangeEmailPayload, ChangePasswordPayload, ConfirmAccountDeletionPayload,
        CreateAccountPayload, PasswordResetPayload, ResendVerificationPayload,
        SendPasswordResetPayload,
    },
};

impl Client {
    /// Create a new account.
    pub async fn create_account(&self, payload: CreateAccountPayload) -> Result<()> {
        self.client
            .post(ep!(self, "/auth/account/create"))
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Resend account creation verification email.
    pub async fn resend_verification(&self, payload: ResendVerificationPayload) -> Result<()> {
        self.client
            .post(ep!(self, "/auth/account/reverify"))
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Schedule an account for deletion by confirming the received token.
    pub async fn confirm_account_deletion(
        &self,
        payload: ConfirmAccountDeletionPayload,
    ) -> Result<()> {
        self.client
            .put(ep!(self, "/auth/account/delete"))
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Request to have an account deleted.
    pub async fn delete_account(&self) -> Result<()> {
        self.client
            .post(ep!(self, "/auth/account/delete"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Fetch account information from the current session.
    pub async fn fetch_account(&self) -> Result<AccountInfo> {
        Ok(self
            .client
            .get(ep!(self, "/auth/account/"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Disable an account.
    pub async fn disable_account(&self) -> Result<()> {
        self.client
            .post(ep!(self, "/auth/account/disable"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Change the current account password.
    pub async fn change_password(&self, payload: ChangePasswordPayload) -> Result<()> {
        self.client
            .patch(ep!(self, "/auth/account/change/password"))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Change the associated account email.
    pub async fn change_email(&self, payload: ChangeEmailPayload) -> Result<()> {
        self.client
            .patch(ep!(self, "/auth/account/change/email"))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Change the associated account email.
    pub async fn verify_email(&self, code: impl Into<String>) -> Result<EmailVerification> {
        Ok(self
            .client
            .post(ep!(self, "/auth/account/verify/{}", code.into()))
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Send an email to reset account password.
    pub async fn send_password_reset(&self, payload: SendPasswordResetPayload) -> Result<()> {
        self.client
            .post(ep!(self, "/auth/account/reset_password"))
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Confirm password reset and change the password.
    pub async fn password_reset(&self, payload: PasswordResetPayload) -> Result<()> {
        self.client
            .patch(ep!(self, "/auth/account/reset_password"))
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }
}