|
1
|
//! Sign in, sign out, and the current-user profile. |
|
2
|
|
|
3
|
use crate::{ |
|
4
|
client::Client, |
|
5
|
error::Result, |
|
6
|
models::{AccountStatus, User}, |
|
7
|
}; |
|
8
|
use reqwest::Method; |
|
9
|
|
|
10
|
impl Client { |
|
11
|
/// `POST /api/v1/auth/sign_in` |
|
12
|
/// |
|
13
|
/// Answers `200` for all three outcomes — the caller inspects the |
|
14
|
/// [`AccountStatus`] rather than the status code. |
|
15
|
pub async fn sign_in(&self, email: &str, password: &str) -> Result<AccountStatus> { |
|
16
|
let body = serde_json::json!({ |
|
17
|
"email": email.trim().to_ascii_lowercase(), |
|
18
|
"password": password, |
|
19
|
}); |
|
20
|
self.post("auth/sign_in", &body).await |
|
21
|
} |
|
22
|
|
|
23
|
/// `DELETE /api/v1/auth/sign_out` — revokes the token server-side. |
|
24
|
pub async fn sign_out(&self) -> Result<()> { |
|
25
|
self.delete("auth/sign_out").await |
|
26
|
} |
|
27
|
|
|
28
|
/// `POST /api/v1/auth/sign_up` — creates an account and sends the |
|
29
|
/// confirmation email. The answer is the sign-up result as the server sent |
|
30
|
/// it (`code`, `message`, `data`), so it is returned untyped. |
|
31
|
pub async fn sign_up(&self, email: &str, password: &str) -> Result<serde_json::Value> { |
|
32
|
let body = serde_json::json!({ |
|
33
|
"email": email.trim().to_ascii_lowercase(), |
|
34
|
"password": password, |
|
35
|
}); |
|
36
|
self.post("auth/sign_up", &body).await |
|
37
|
} |
|
38
|
|
|
39
|
/// `POST /api/v1/auth/confirmation/resend` — sends the confirmation email |
|
40
|
/// again. The server answers the same way whether or not the address has an |
|
41
|
/// account. |
|
42
|
pub async fn resend_confirmation(&self, email: &str) -> Result<()> { |
|
43
|
let body = serde_json::json!({ "email": email.trim().to_ascii_lowercase() }); |
|
44
|
self.send_empty( |
|
45
|
self.request(Method::POST, "auth/confirmation/resend") |
|
46
|
.json(&body), |
|
47
|
) |
|
48
|
.await |
|
49
|
} |
|
50
|
|
|
51
|
/// `DELETE /api/v1/me` — permanently deletes the signed-in account. |
|
52
|
pub async fn delete_account(&self) -> Result<()> { |
|
53
|
self.delete("me").await |
|
54
|
} |
|
55
|
|
|
56
|
/// `GET /api/v1/user` — the signed-in user's profile. |
|
57
|
pub async fn user(&self) -> Result<User> { |
|
58
|
self.get("user").await |
|
59
|
} |
|
60
|
} |
|
61
|
|
|
62
|
/// Human-readable explanation for an `AccountStatus::Incomplete` status code, |
|
63
|
/// mirroring `account::ErrorCode` in the shared model. |
|
64
|
pub fn incomplete_reason(status: u32) -> &'static str { |
|
65
|
match status { |
|
66
|
1001 => "Your email address is not verified yet. Check your inbox, or run `si auth resend`.", |
|
67
|
1002 => "Your account is locked.", |
|
68
|
1003 => "Your account is not active yet.", |
|
69
|
_ => "Your account is not ready to sign in yet. Finish setting it up at https://sigit.si/auth.", |
|
70
|
} |
|
71
|
} |