Skip to content

Commit 3fd2da9

Browse files
committed
More ui friendly register flow
1 parent 66daac5 commit 3fd2da9

1 file changed

Lines changed: 56 additions & 25 deletions

File tree

src/cmd/auth.rs

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use anyhow::{anyhow, Result};
2+
use crossterm::style::Stylize;
23
use serde::{Deserialize, Serialize};
34
use std::fs::{File, OpenOptions};
45
use std::path::PathBuf;
56

6-
use crate::service; // Assuming service::create_client is needed
7+
use crate::service;
78

89
// Configuration structure
910
#[derive(Serialize, Deserialize, Debug, Default)]
@@ -50,32 +51,46 @@ struct AuthInitResponse {
5051

5152
// Function to handle the login logic
5253
pub async fn run_auth(reset: bool, auth_provider: &str) -> Result<()> {
53-
println!("Attempting authentication via {}...", auth_provider);
54+
println!(
55+
"{} Authenticating via {}...",
56+
"●".cyan(),
57+
auth_provider.bold()
58+
);
5459

55-
let popcorn_api_url = std::env::var("POPCORN_API_URL")
56-
.map_err(|_| anyhow!("POPCORN_API_URL environment variable not set"))?;
60+
let popcorn_api_url = std::env::var("POPCORN_API_URL").map_err(|_| {
61+
anyhow!(
62+
"{} POPCORN_API_URL environment variable not set",
63+
"error:".red().bold()
64+
)
65+
})?;
5766

5867
let client = service::create_client(None)?;
5968

6069
let init_url = format!("{}/auth/init?provider={}", popcorn_api_url, auth_provider);
61-
println!("Requesting CLI ID from {}", init_url);
6270

63-
let init_resp = client.get(&init_url).send().await?;
71+
let init_resp = client.get(&init_url).send().await.map_err(|e| {
72+
anyhow!(
73+
"{} Could not reach auth server: {}",
74+
"error:".red().bold(),
75+
e
76+
)
77+
})?;
6478

6579
let status = init_resp.status();
6680

6781
if !status.is_success() {
6882
let error_text = init_resp.text().await?;
69-
return Err(anyhow!(
70-
"Failed to initialize auth ({}): {}",
71-
status,
83+
eprintln!(
84+
"{} Failed to initialize auth ({}): {}",
85+
"error:".red().bold(),
86+
status.to_string().red(),
7287
error_text
73-
));
88+
);
89+
return Err(anyhow!("Authentication initialization failed"));
7490
}
7591

7692
let auth_init_data: AuthInitResponse = init_resp.json().await?;
7793
let cli_id = auth_init_data.state;
78-
println!("Received CLI ID: {}", cli_id);
7994

8095
let state_json = serde_json::json!({
8196
"cli_id": cli_id,
@@ -99,41 +114,57 @@ pub async fn run_auth(reset: bool, auth_provider: &str) -> Result<()> {
99114
)
100115
}
101116
_ => {
117+
eprintln!(
118+
"{} Unsupported authentication provider: {}",
119+
"error:".red().bold(),
120+
auth_provider.yellow()
121+
);
102122
return Err(anyhow!(
103123
"Unsupported authentication provider: {}",
104124
auth_provider
105-
))
125+
));
106126
}
107127
};
108128

109129
println!(
110-
"\n>>> Please open the following URL in your browser to log in via {}:",
111-
auth_provider
112-
);
113-
println!("{}", auth_url);
114-
println!("\nWaiting for you to complete the authentication in your browser...");
115-
println!(
116-
"After successful authentication with {}, the CLI ID will be saved.",
117-
auth_provider
130+
"\n {} Open this URL to log in via {}:\n",
131+
"▸".bold(),
132+
auth_provider.bold()
118133
);
134+
println!(" {}\n", auth_url.as_str().underlined().cyan());
119135

120136
if webbrowser::open(&auth_url).is_err() {
121137
println!(
122-
"Could not automatically open the browser. Please copy the URL above and paste it manually."
138+
" {} Could not open browser automatically — please copy the link above.",
139+
"!".yellow().bold()
140+
);
141+
} else {
142+
println!(
143+
" {} Browser opened. Complete the login there.",
144+
"✓".green().bold()
123145
);
124146
}
125147

148+
println!(
149+
" {} Waiting for authentication to complete...\n",
150+
"…".dark_grey()
151+
);
152+
126153
// Save the cli_id to config file optimistically
127154
let mut config = load_config().unwrap_or_default();
128155
config.cli_id = Some(cli_id.clone());
129156
save_config(&config)?;
130157

158+
let config_path = get_config_path()?.display().to_string();
159+
println!(
160+
" {} Authentication initiated! CLI ID saved to {}",
161+
"✓".green().bold(),
162+
config_path.underlined()
163+
);
131164
println!(
132-
"\nSuccessfully initiated authentication. Your CLI ID ({}) has been saved to {}. To use the CLI on different machines, you can copy the config file.",
133-
cli_id,
134-
get_config_path()?.display()
165+
" {} You can now use commands that require authentication.\n",
166+
"●".cyan()
135167
);
136-
println!("You can now use other commands that require authentication.");
137168

138169
Ok(())
139170
}

0 commit comments

Comments
 (0)