@smbcloud / cli / commits / d24c24d

WIP

Seto Elkahfi committed Feb 21, 2023 at 00:21 UTC d24c24d99f21c515bb51a489f40d10445146a80e
3 files changed +75 -5
src/login/process.rs
+3 -3
@@ -20,9 +20,9 @@ struct LoginParams {
20 }
21
22 #[derive(Debug, Serialize)]
23 -struct User {
24 - email: String,
25 - password: String,
23 +pub struct User {
24 + pub(crate) email: String,
25 + pub(crate) password: String,
26 }
27
28 #[derive(Debug, Serialize, Deserialize)]
src/main.rs
+32 -2
@@ -7,6 +7,7 @@ use smbpndk_cli::{
7 cli::{Cli, Commands},
8 constants::ERROR_EMOJI,
9 login::{process_login, LoginArgs},
10 + signup::{process_signup, SignupArgs},
11 };
12 use spinners::Spinner;
13 use tokio::time::sleep;
@@ -53,7 +54,7 @@ async fn main() {
54 }
55 }
56
56 -async fn run() -> Result<()> {
57 +async fn run() -> Result<Spinner> {
58 let cli = Cli::parse();
59
60 let log_level_error: Result<()> = Err(anyhow!(
@@ -86,7 +87,36 @@ async fn run() -> Result<()> {
87 spinner.stop_and_persist("✅", style("You are logged in!").green().bold().to_string())
88 }
89 Commands::Signup { username, password } => {
89 - println!("Signup: {}, {}", username, password);
90 + let mut spinner = Spinner::new(
91 + spinners::Spinners::BouncingBall,
92 + style("Signing up...").green().bold().to_string(),
93 + );
94 + let join_handle = tokio::spawn(async move {
95 + let _future = process_signup(SignupArgs { username, password }).await;
96 + sleep(Duration::from_millis(5000)).await;
97 + });
98 +
99 + match join_handle.await {
100 + Ok(_) => {}
101 + Err(e) => {
102 + spinner.stop_and_persist(
103 + "❌",
104 + style(format!("Failed to sign up: {}", e))
105 + .red()
106 + .bold()
107 + .to_string(),
108 + );
109 + return Ok(());
110 + }
111 + }
112 +
113 + spinner.stop_and_persist(
114 + "✅",
115 + style("Your account is created! Check your email to confirm your account.")
116 + .green()
117 + .bold()
118 + .to_string(),
119 + )
120 }
121 }
122
src/signup/process.rs
+40
@@ -0,0 +1,40 @@
1 +use anyhow::Result;
2 +use reqwest::Client;
3 +use serde::Serialize;
4 +
5 +use crate::{constants::BASE_URL, login::User};
6 +pub struct SignupArgs {
7 + pub username: String,
8 + pub password: String,
9 +}
10 +#[derive(Debug, Serialize)]
11 +pub struct SignupParams {
12 + pub user: User,
13 +}
14 +pub async fn process_signup(args: SignupArgs) -> Result<()> {
15 + let signup_params = SignupParams {
16 + user: User {
17 + email: args.username,
18 + password: args.password,
19 + },
20 + };
21 +
22 + let response = Client::new()
23 + .post([BASE_URL, "/v1/users"].join(""))
24 + .json(&signup_params)
25 + .send()
26 + .await?;
27 +
28 + 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);
33 + }
34 + _ => {
35 + println!("Error: {}", response.status());
36 + }
37 + }
38 +
39 + Ok(())
40 +}