Skip to content

Commit 4287896

Browse files
committed
Handle expired session on logout and clear token on 401
- Treat 401 Unauthorized during logout as success since the session is already expired on the server - Clear the local token and show a helpful message when any command fails with "Unauthorized access." - Add clear_smb_token helper to remove the token file for an environment
1 parent 0d6860e commit 4287896

5 files changed

Lines changed: 42 additions & 10 deletions

File tree

crates/cli/src/account/logout/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ async fn do_process_logout(env: Environment) -> Result<()> {
7070
let token = get_smb_token(env)?;
7171
match logout(env, client(), token).await {
7272
Ok(_) => Ok(()),
73-
Err(e) => Err(anyhow!("{e}")),
73+
Err(e) => {
74+
// A 401 means the session is already expired on the server.
75+
// Treat this as success — the session is gone either way.
76+
if e.to_string().contains("Unauthorized") {
77+
Ok(())
78+
} else {
79+
Err(anyhow!("{e}"))
80+
}
81+
}
7482
}
7583
}

crates/cli/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub mod project;
77
mod token;
88
mod ui;
99

10+
pub use token::clear_smb_token::clear_smb_token;
11+
1012
pub(crate) fn client() -> (&'static SmbClient, &'static str) {
1113
let secret = env!("CLI_CLIENT_SECRET");
1214
(&SmbClient::Cli, secret)

crates/cli/src/main.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use {
44
console::style,
55
smbcloud_cli::{
66
account::{login::process_login, logout::process_logout, me::process_me, process_account},
7+
clear_smb_token,
78
cli::{Cli, CommandResult, Commands},
89
deploy::process_deploy::process_deploy,
910
project::{crud_create::process_project_init, process::process_project},
@@ -72,25 +73,34 @@ fn setup_logging(env: Environment, level: Option<EnvFilter>) -> Result<()> {
7273

7374
#[tokio::main]
7475
async fn main() {
75-
match run().await {
76+
let cli = Cli::parse();
77+
let environment = cli.environment;
78+
match run(cli).await {
7679
Ok(result) => {
7780
result.stop_and_persist();
7881
std::process::exit(0);
7982
}
8083
Err(e) => {
81-
println!(
82-
"\n{} {}",
83-
style("✘".to_string()).for_stderr().red(),
84-
style(e).red()
85-
);
84+
if e.to_string().contains("Unauthorized access.") {
85+
let _ = clear_smb_token(environment);
86+
println!(
87+
"\n{} {}",
88+
style("✘".to_string()).for_stderr().red(),
89+
style("Your session has expired. Please login again with `smb login`.").red()
90+
);
91+
} else {
92+
println!(
93+
"\n{} {}",
94+
style("✘".to_string()).for_stderr().red(),
95+
style(e).red()
96+
);
97+
}
8698
std::process::exit(1);
8799
}
88100
}
89101
}
90102

91-
async fn run() -> Result<CommandResult> {
92-
let cli = Cli::parse();
93-
103+
async fn run(cli: Cli) -> Result<CommandResult> {
94104
// println!("Environment: {}", cli.environment);
95105

96106
let log_level_error: Result<CommandResult> = Err(anyhow!(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use {
2+
crate::token::smb_token_file_path::smb_token_file_path, anyhow::Result,
3+
smbcloud_network::environment::Environment, std::fs,
4+
};
5+
6+
pub fn clear_smb_token(env: Environment) -> Result<()> {
7+
if let Some(path) = smb_token_file_path(env) {
8+
fs::remove_file(path)?;
9+
}
10+
Ok(())
11+
}

crates/cli/src/token/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub(crate) mod clear_smb_token;
12
pub(crate) mod get_smb_token;
23
pub(crate) mod is_logged_in;
34
pub(crate) mod smb_token_file_path;

0 commit comments

Comments
 (0)