@smbcloud / cli / commits / 9e3a31d

Fix signup flow

Seto Elkahfi committed Mar 4, 2023 at 15:11 UTC 9e3a31dede9c4c103c3c600c4431a7ac8975b668
5 files changed +52 -36
src/account/login/process.rs
+5 -29
@@ -7,7 +7,11 @@ use anyhow::{anyhow, Result};
7 use reqwest::Client;
8 use serde::{Deserialize, Serialize};
9
10 -use crate::{account::model::User, constants::BASE_URL};
10 +use crate::{
11 + account::model::{Data, Status, User},
12 + constants::BASE_URL,
13 + debug,
14 +};
15
16 pub struct LoginArgs {
17 pub username: String,
@@ -25,34 +29,6 @@ struct LoginResult {
29 data: Data,
30 }
31
28 -#[derive(Debug, Serialize, Deserialize)]
29 -struct Status {
30 - code: i32,
31 - message: String,
32 -}
33 -
34 -#[derive(Debug, Serialize, Deserialize)]
35 -struct Data {
36 - id: i32,
37 - email: String,
38 - created_at: String,
39 -}
40 -
41 -macro_rules! debug {
42 - ($($e:expr),+) => {
43 - {
44 - #[cfg(debug_assertions)]
45 - {
46 - dbg!($($e),+)
47 - }
48 - #[cfg(not(debug_assertions))]
49 - {
50 - ($($e),+)
51 - }
52 - }
53 - };
54 -}
55 -
32 pub async fn process_login(args: LoginArgs) -> Result<()> {
33 let login_params = LoginParams {
34 user: User {
src/account/model/mod.rs
+14 -1
@@ -1,7 +1,20 @@
1 -use serde::Serialize;
1 +use serde::{Deserialize, Serialize};
2
3 #[derive(Debug, Serialize)]
4 pub struct User {
5 pub(crate) email: String,
6 pub(crate) password: String,
7 }
8 +
9 +#[derive(Debug, Serialize, Deserialize)]
10 +pub struct Status {
11 + code: Option<i32>,
12 + pub(crate) message: String,
13 +}
14 +
15 +#[derive(Debug, Serialize, Deserialize)]
16 +pub struct Data {
17 + id: i32,
18 + email: String,
19 + created_at: String,
20 +}
src/account/signup/process.rs
+17 -6
@@ -1,8 +1,11 @@
1 use anyhow::{anyhow, Result};
2 use reqwest::Client;
3 -use serde::Serialize;
3 +use serde::{Deserialize, Serialize};
4
5 -use crate::{account::model::User, constants::BASE_URL};
5 +use crate::{
6 + account::model::{Data, Status, User},
7 + constants::BASE_URL,
8 +};
9 pub struct SignupArgs {
10 pub username: String,
11 pub password: String,
@@ -11,6 +14,13 @@ pub struct SignupArgs {
14 pub struct SignupParams {
15 pub user: User,
16 }
17 +
18 +#[derive(Debug, Serialize, Deserialize)]
19 +struct SignupResult {
20 + status: Status,
21 + data: Option<Data>,
22 +}
23 +
24 pub async fn process_signup(args: SignupArgs) -> Result<()> {
25 let signup_params = SignupParams {
26 user: User {
@@ -26,10 +36,11 @@ pub async fn process_signup(args: SignupArgs) -> Result<()> {
36 .await?;
37
38 match response.status() {
29 - reqwest::StatusCode::OK => {
30 - let headers = response.headers();
31 - let token = headers.get("Authorization").unwrap().to_str().unwrap();
32 - println!("token: {token}");
39 + reqwest::StatusCode::OK => {}
40 + reqwest::StatusCode::UNPROCESSABLE_ENTITY => {
41 + let result: SignupResult = response.json().await?;
42 + let error = anyhow!("Failed to signup: {}", result.status.message);
43 + return Err(error);
44 }
45 _ => {
46 let error = anyhow!("Failed to signup: {}", response.status());
src/lib.rs
+1
@@ -1,3 +1,4 @@
1 pub mod account;
2 pub mod cli;
3 pub mod constants;
4 +mod util;
src/util.rs new
+15
@@ -0,0 +1,15 @@
1 +#[macro_export]
2 +macro_rules! debug {
3 + ($($e:expr),+) => {
4 + {
5 + #[cfg(debug_assertions)]
6 + {
7 + dbg!($($e),+)
8 + }
9 + #[cfg(not(debug_assertions))]
10 + {
11 + ($($e),+)
12 + }
13 + }
14 + };
15 +}