| 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 | |
| 9 | impl Client { |
| 10 | /// `POST /api/v1/auth/sign_in` |
| 11 | /// |
| 12 | /// Answers `200` for all three outcomes — the caller inspects the |
| 13 | /// [`AccountStatus`] rather than the status code. |
| 14 | pub async fn sign_in(&self, email: &str, password: &str) -> Result<AccountStatus> { |
| 15 | let body = serde_json::json!({ |
| 16 | "email": email.trim().to_ascii_lowercase(), |
| 17 | "password": password, |
| 18 | }); |
| 19 | self.post("auth/sign_in", &body).await |
| 20 | } |
| 21 | |
| 22 | /// `DELETE /api/v1/auth/sign_out` — revokes the token server-side. |
| 23 | pub async fn sign_out(&self) -> Result<()> { |
| 24 | self.delete("auth/sign_out").await |
| 25 | } |
| 26 | |
| 27 | /// `GET /api/v1/user` — the signed-in user's profile. |
| 28 | pub async fn user(&self) -> Result<User> { |
| 29 | self.get("user").await |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// Human-readable explanation for an `AccountStatus::Incomplete` status code, |
| 34 | /// mirroring `account::ErrorCode` in the shared model. |
| 35 | pub fn incomplete_reason(status: u32) -> &'static str { |
| 36 | match status { |
| 37 | 1001 => "Your email address is not verified yet. Check your inbox, or run `si auth resend`.", |
| 38 | 1002 => "Your account is locked.", |
| 39 | 1003 => "Your account is not active yet.", |
| 40 | _ => "Your account is not ready to sign in yet. Finish setting it up at https://sigit.si/auth.", |
| 41 | } |
| 42 | } |