@smbcloud / cli / commits / 586e6c9

Resend verification from email and password

Seto Elkahfi committed Sep 3, 2023 at 18:28 UTC 586e6c919f3ded650b0225a8c2188878f3b736ca
1 file changed +33 -25
smbpndk-cli/src/account/login/process.rs
+33 -25
@@ -9,7 +9,7 @@ use anyhow::{anyhow, Result};
9 use console::{style, Term};
10 use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password, Select};
11 use log::debug;
12 -use reqwest::Client;
12 +use reqwest::{Client, StatusCode};
13 use serde::{Deserialize, Serialize};
14 use smbpndk_model::CommandResult;
15 use smbpndk_utils::email_validation;
@@ -205,27 +205,10 @@ async fn login_with_email() -> Result<CommandResult> {
205 .with_prompt("Password")
206 .interact()
207 .unwrap();
208 -
209 - let spinner = Spinner::new(
210 - spinners::Spinners::SimpleDotsScrolling,
211 - style("Logging in...").green().bold().to_string(),
212 - );
213 -
214 - match do_process_login(LoginArgs { username, password }).await {
215 - Ok(_) => Ok(CommandResult {
216 - spinner,
217 - symbol: "✅".to_owned(),
218 - msg: "You are logged in!".to_owned(),
219 - }),
220 - Err(e) => Ok(CommandResult {
221 - spinner,
222 - symbol: "😩".to_owned(),
223 - msg: format!("Failed to login: {e}"),
224 - }),
225 - }
208 + do_process_login(LoginArgs { username, password }).await
209 }
210
228 -async fn do_process_login(args: LoginArgs) -> Result<()> {
211 +async fn do_process_login(args: LoginArgs) -> Result<CommandResult> {
212 let login_params = LoginParams {
213 user: UserParam {
214 email: args.username,
@@ -240,7 +223,7 @@ async fn do_process_login(args: LoginArgs) -> Result<()> {
223 .await?;
224
225 match response.status() {
243 - reqwest::StatusCode::OK => {
226 + StatusCode::OK => {
227 let headers = response.headers();
228 match headers.get("Authorization") {
229 Some(token) => {
@@ -254,6 +237,15 @@ async fn do_process_login(args: LoginArgs) -> Result<()> {
237 .write(true)
238 .open([path.to_str().unwrap(), "/.smb/token"].join(""))?;
239 file.write_all(token.to_str()?.as_bytes())?;
240 +
241 + Ok(CommandResult {
242 + spinner: Spinner::new(
243 + spinners::Spinners::SimpleDotsScrolling,
244 + style("Logging you in...").green().bold().to_string(),
245 + ),
246 + symbol: "✅".to_owned(),
247 + msg: "You are logged in!".to_owned(),
248 + })
249 }
250 None => {
251 let error = anyhow!("Failed to get home directory.");
@@ -266,14 +258,30 @@ async fn do_process_login(args: LoginArgs) -> Result<()> {
258 return Err(error);
259 }
260 }
269 - }
261 + },
262 + StatusCode::NOT_FOUND => {
263 + // Account not found and we show signup option
264 + Ok(CommandResult {
265 + spinner: Spinner::new(
266 + spinners::Spinners::SimpleDotsScrolling,
267 + style("Account not found.").green().bold().to_string(),
268 + ),
269 + symbol: "✅".to_owned(),
270 + msg: "Please signup!".to_owned(),
271 + })
272 + },
273 + StatusCode::UNPROCESSABLE_ENTITY => {
274 + // Account found but email not verified
275 + println!("✅ Account found but email not verified.");
276 + let result: SmbAuthorization = response.json().await?;
277 + // println!("Result: {:#?}", &result);
278 + return send_email_verification(result.user).await;
279 + },
280 _ => {
271 - let error = anyhow!("Connection error. Check your username and password.");
281 + let error = anyhow!("Login failed. Check your username and password.");
282 return Err(error);
283 }
284 }
275 -
276 - Ok(())
285 }
286
287 fn build_smb_login_url() -> String {