@smbcloud / cli / commits / 8443e19

fix logout login validation

Seto Elkahfi committed Sep 11, 2023 at 12:00 UTC 8443e197ff204aa81c532eec600b5fec472ee20b
5 files changed +60 -37
app-oten/src/lib.rs
+5 -5
@@ -5,11 +5,11 @@ use anyhow::{anyhow, Result};
5 use log::debug;
6 use reqwest::Client;
7 use smbpndk_model::{AuthApp, AuthAppCreate};
8 -use smbpndk_networking::{constants::BASE_URL, get_token};
8 +use smbpndk_networking::{constants::BASE_URL, get_smb_token};
9
10 async fn get_auth_apps() -> Result<Vec<AuthApp>> {
11 // Get current token
12 - let token = get_token().await?;
12 + let token = get_smb_token().await?;
13
14 let response = Client::new()
15 .get([BASE_URL, "v1/auth_apps"].join(""))
@@ -28,7 +28,7 @@ async fn get_auth_apps() -> Result<Vec<AuthApp>> {
28
29 async fn get_auth_app(id: &str) -> Result<AuthApp> {
30 // Get current token
31 - let token = get_token().await?;
31 + let token = get_smb_token().await?;
32
33 let response = Client::new()
34 .get([BASE_URL, "v1/auth_apps/", id].join(""))
@@ -50,7 +50,7 @@ async fn get_auth_app(id: &str) -> Result<AuthApp> {
50
51 async fn delete_auth_app(id: String) -> Result<()> {
52 // Get current token
53 - let token = get_token().await?;
53 + let token = get_smb_token().await?;
54
55 let response = Client::new()
56 .delete([BASE_URL, "v1/auth_apps/", &id].join(""))
@@ -69,7 +69,7 @@ async fn delete_auth_app(id: String) -> Result<()> {
69
70 async fn create_auth_app(auth_app: AuthAppCreate) -> Result<AuthApp> {
71 // Get current token
72 - let token = get_token().await?;
72 + let token = get_smb_token().await?;
73
74 let response = Client::new()
75 .post([BASE_URL, "v1/auth_apps"].join(""))
smbpndk-cli/src/account/login/process.rs
+27 -24
@@ -13,7 +13,7 @@ use log::debug;
13 use reqwest::{Client, StatusCode};
14 use serde::{Deserialize, Serialize};
15 use smbpndk_model::CommandResult;
16 -use smbpndk_networking::smb_base_url_builder;
16 +use smbpndk_networking::{smb_base_url_builder, smb_token_file_path};
17 use smbpndk_utils::email_validation;
18 use spinners::Spinner;
19 use std::fs::{self};
@@ -41,6 +41,18 @@ struct LoginResult {
41 }
42
43 pub async fn process_login() -> Result<CommandResult> {
44 + // Check if token file exists
45 + if smb_token_file_path().is_some() {
46 + return Ok(CommandResult {
47 + spinner: Spinner::new(
48 + spinners::Spinners::SimpleDotsScrolling,
49 + style("Loading...").green().bold().to_string(),
50 + ),
51 + symbol: "✅".to_owned(),
52 + msg: "You are already logged in.".to_owned(),
53 + });
54 + }
55 +
56 let signup_methods = vec![SignupMethod::Email, SignupMethod::GitHub];
57 let selection = Select::with_theme(&ColorfulTheme::default())
58 .items(&signup_methods)
@@ -385,29 +397,20 @@ pub async fn process_logout() -> Result<CommandResult> {
397 spinners::Spinners::SimpleDotsScrolling,
398 style("Logging you out...").green().bold().to_string(),
399 );
388 - match home::home_dir() {
389 - Some(path) => {
390 - debug!("Home directory: {}.", path.to_str().unwrap());
391 -
392 - // Check if token file exists
393 - if !path.join(".smb/token").exists() {
394 - return Ok(CommandResult {
395 - spinner,
396 - symbol: "✅".to_owned(),
397 - msg: "You are not logged in.".to_owned(),
398 - });
399 - }
400 -
401 - // Remove token file
402 - fs::remove_file(path.join(".smb/token"))?;
403 -
404 - Ok(CommandResult {
405 - spinner,
406 - symbol: "✅".to_owned(),
407 - msg: "You are now logged out!".to_owned(),
408 - })
409 - }
410 - None => Err(anyhow!("Failed to get home directory. Are you logged in?")),
400 + if let Some(token_path) = smb_token_file_path() {
401 + // Call backend
402 + fs::remove_file(token_path)?;
403 + Ok(CommandResult {
404 + spinner,
405 + symbol: "✅".to_owned(),
406 + msg: "You are now logged out!".to_owned(),
407 + })
408 + } else {
409 + Ok(CommandResult {
410 + spinner,
411 + symbol: "😏".to_owned(),
412 + msg: "You are not logged in.".to_owned(),
413 + })
414 }
415 }
416
smbpndk-networking-project/src/lib.rs
+5 -5
@@ -2,11 +2,11 @@ use anyhow::{anyhow, Result};
2 use log::debug;
3 use reqwest::Client;
4 use smbpndk_model::{Project, ProjectCreate};
5 -use smbpndk_networking::{get_token, smb_base_url_builder};
5 +use smbpndk_networking::{get_smb_token, smb_base_url_builder};
6
7 pub async fn get_all() -> Result<Vec<Project>> {
8 // Get current token
9 - let token = get_token().await?;
9 + let token = get_smb_token().await?;
10
11 debug!("Current token: {}", token);
12
@@ -28,7 +28,7 @@ pub async fn get_all() -> Result<Vec<Project>> {
28
29 pub async fn create_project(project: ProjectCreate) -> Result<Project> {
30 // Get current token
31 - let token = get_token().await?;
31 + let token = get_smb_token().await?;
32
33 let response = Client::new()
34 .post(build_project_url())
@@ -49,7 +49,7 @@ pub async fn create_project(project: ProjectCreate) -> Result<Project> {
49
50 pub async fn get_project(id: String) -> Result<Project> {
51 // Get current token
52 - let token = get_token().await?;
52 + let token = get_smb_token().await?;
53
54 let response = Client::new()
55 .get(build_project_url_with_id(id))
@@ -69,7 +69,7 @@ pub async fn get_project(id: String) -> Result<Project> {
69
70 pub async fn delete_project(id: String) -> Result<()> {
71 // Get current token
72 - let token = get_token().await?;
72 + let token = get_smb_token().await?;
73
74 let response = Client::new()
75 .delete(build_project_url_with_id(id))
smbpndk-networking/src/constants.rs
+2
@@ -3,3 +3,5 @@ pub const BASE_URL: &str = "http://localhost:3000/";
3
4 #[cfg(not(debug_assertions))]
5 pub const BASE_URL: &str = "https://api.smbpndk.com/";
6 +
7 +pub const TOKEN_PATH_STR: &str = ".smb/token";
smbpndk-networking/src/lib.rs
+21 -3
@@ -1,13 +1,14 @@
1 pub mod constants;
2
3 +use crate::constants::TOKEN_PATH_STR;
4 use anyhow::{anyhow, Result};
5 use dotenvy_macro::dotenv;
6 use log::debug;
7 +use std::path::PathBuf;
8 use url_builder::URLBuilder;
9
8 -pub async fn get_token() -> Result<String> {
9 - if let Some(mut path) = dirs::home_dir() {
10 - path.push(".smb/token");
10 +pub async fn get_smb_token() -> Result<String> {
11 + if let Some(path) = smb_token_file_path() {
12 std::fs::read_to_string(path).map_err(|e| {
13 debug!("Error while reading token: {}", &e);
14 anyhow!("Error while reading token. Are you logged in?")
@@ -17,6 +18,23 @@ pub async fn get_token() -> Result<String> {
18 }
19 }
20
21 +pub fn smb_token_file_path() -> Option<PathBuf> {
22 + match home::home_dir() {
23 + Some(path) => {
24 + debug!("Home directory: {}.", path.to_str().unwrap());
25 + let token_path = path.join(TOKEN_PATH_STR);
26 + if token_path.exists() && token_path.is_file() {
27 + return Some(token_path);
28 + }
29 + None
30 + }
31 + None => {
32 + debug!("Failed to get home directory.");
33 + None
34 + }
35 + }
36 +}
37 +
38 pub fn smb_base_url_builder() -> URLBuilder {
39 let client_id = dotenv!("SMB_CLIENT_ID", "Please set SMB_CLIENT_ID");
40 let client_secret = dotenv!("SMB_CLIENT_SECRET", "Please set SMB_CLIENT_SECRET");