@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

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 /// `POST /api/v1/auth/password/reset` — emails reset instructions. The
52 /// server answers the same way whether or not the address has an account.
53 pub async fn reset_password(&self, email: &str) -> Result<()> {
54 self.send_empty(self.reset_password_request(email)).await
55 }
56
57 fn reset_password_request(&self, email: &str) -> reqwest::RequestBuilder {
58 let body = serde_json::json!({ "email": email.trim().to_ascii_lowercase() });
59 self.request(Method::POST, "auth/password/reset")
60 .json(&body)
61 }
62
63 /// `DELETE /api/v1/me` — permanently deletes the signed-in account.
64 pub async fn delete_account(&self) -> Result<()> {
65 self.delete("me").await
66 }
67
68 /// `GET /api/v1/user` — the signed-in user's profile.
69 pub async fn user(&self) -> Result<User> {
70 self.get("user").await
71 }
72 }
73
74 /// Human-readable explanation for an `AccountStatus::Incomplete` status code,
75 /// mirroring `account::ErrorCode` in the shared model.
76 pub fn incomplete_reason(status: u32) -> &'static str {
77 match status {
78 1001 => "Your email address is not verified yet. Check your inbox, or run `si auth resend`.",
79 1002 => "Your account is locked.",
80 1003 => "Your account is not active yet.",
81 _ => "Your account is not ready to sign in yet. Finish setting it up at https://sigit.si/auth.",
82 }
83 }
84
85 #[cfg(test)]
86 mod tests {
87 use {super::*, crate::Environment};
88
89 #[test]
90 fn a_reset_posts_the_normalized_email() {
91 let client = Client::builder(Environment::Production)
92 .base_url("https://sigit.si")
93 .build()
94 .unwrap();
95 let request = client
96 .reset_password_request(" Seto@Example.com ")
97 .build()
98 .unwrap();
99
100 assert_eq!(request.method(), Method::POST);
101 assert_eq!(
102 request.url().as_str(),
103 "https://sigit.si/api/v1/auth/password/reset"
104 );
105 let body = request.body().and_then(|b| b.as_bytes()).unwrap();
106 let body: serde_json::Value = serde_json::from_slice(body).unwrap();
107 assert_eq!(body["email"], "seto@example.com");
108 }
109 }