Skip to content

Commit c7e8eaf

Browse files
committed
fix auth flow bugs
1 parent 723bde8 commit c7e8eaf

3 files changed

Lines changed: 39 additions & 31 deletions

File tree

src/auth.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ pub fn login() {
8383
let api_url = profile_config.api_url.to_string();
8484
let app_url = profile_config.app_url.to_string();
8585

86+
// Check if already authenticated
87+
if let Some(api_key) = &profile_config.api_key {
88+
if api_key != "PLACEHOLDER" {
89+
let client = reqwest::blocking::Client::new();
90+
if let Ok(resp) = client
91+
.get(format!("{api_url}/workspaces"))
92+
.header("Authorization", format!("Bearer {api_key}"))
93+
.send()
94+
{
95+
if resp.status().is_success() {
96+
println!("{}", "You are already signed in.".green());
97+
print!("Do you want to log in again? [y/N] ");
98+
use std::io::Write;
99+
std::io::stdout().flush().unwrap();
100+
let mut input = String::new();
101+
std::io::stdin().read_line(&mut input).unwrap();
102+
if !input.trim().eq_ignore_ascii_case("y") {
103+
return;
104+
}
105+
}
106+
}
107+
}
108+
}
109+
86110
let code_verifier = generate_code_verifier();
87111
let code_challenge = generate_code_challenge(&code_verifier);
88112
let state = generate_random_string(32);
@@ -261,6 +285,10 @@ pub fn login() {
261285
None => print_row("Workspace", &"None".dark_grey().to_string()),
262286
}
263287
}
288+
Ok(r) if r.status() == reqwest::StatusCode::FORBIDDEN => {
289+
eprintln!("{}", "You are not authorized to create a new API token.".red());
290+
std::process::exit(1);
291+
}
264292
Ok(r) => {
265293
eprintln!("token exchange failed: HTTP {}", r.status());
266294
std::process::exit(1);

src/config.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ pub struct ConfigFile {
9999
pub profiles: HashMap<String, ProfileConfig>,
100100
}
101101

102+
fn write_config(config_path: &std::path::Path, content: &str) -> Result<(), String> {
103+
if let Some(parent) = config_path.parent() {
104+
fs::create_dir_all(parent).map_err(|e| format!("error creating config directory: {e}"))?;
105+
}
106+
fs::write(config_path, content).map_err(|e| format!("error writing config file: {e}"))
107+
}
108+
102109
pub fn save_api_key(profile: &str, api_key: &str) -> Result<(), String> {
103110
let user_dirs = UserDirs::new().ok_or("could not determine home directory")?;
104111
let config_path = user_dirs.home_dir().join(".hotdata").join("config.yml");
@@ -122,7 +129,7 @@ pub fn save_api_key(profile: &str, api_key: &str) -> Result<(), String> {
122129
let content = serde_yaml::to_string(&config_file)
123130
.map_err(|e| format!("error serializing config: {e}"))?;
124131

125-
fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
132+
write_config(&config_path, &content)
126133
}
127134

128135
pub fn remove_api_key(profile: &str) -> Result<(), String> {
@@ -145,7 +152,7 @@ pub fn remove_api_key(profile: &str) -> Result<(), String> {
145152

146153
let content = serde_yaml::to_string(&config_file)
147154
.map_err(|e| format!("error serializing config: {e}"))?;
148-
fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
155+
write_config(&config_path, &content)
149156
}
150157

151158
pub fn save_workspaces(profile: &str, workspaces: Vec<WorkspaceEntry>) -> Result<(), String> {
@@ -171,7 +178,7 @@ pub fn save_workspaces(profile: &str, workspaces: Vec<WorkspaceEntry>) -> Result
171178
let content = serde_yaml::to_string(&config_file)
172179
.map_err(|e| format!("error serializing config: {e}"))?;
173180

174-
fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
181+
write_config(&config_path, &content)
175182
}
176183

177184
pub fn save_default_workspace(profile: &str, workspace: WorkspaceEntry) -> Result<(), String> {
@@ -192,7 +199,7 @@ pub fn save_default_workspace(profile: &str, workspace: WorkspaceEntry) -> Resul
192199

193200
let content = serde_yaml::to_string(&config_file)
194201
.map_err(|e| format!("error serializing config: {e}"))?;
195-
fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
202+
write_config(&config_path, &content)
196203
}
197204

198205
pub fn resolve_workspace_id(provided: Option<String>, profile_config: &ProfileConfig) -> Result<String, String> {

src/init.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)