Added Jellyfin Auth
This commit is contained in:
148
src/main.rs
148
src/main.rs
@@ -23,6 +23,13 @@ use ratatui::backend::CrosstermBackend;
|
||||
enum View {
|
||||
Categories,
|
||||
Placeholder(String), // temporary for testing navigation
|
||||
Login,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum LoginFocus {
|
||||
User,
|
||||
Pass,
|
||||
}
|
||||
|
||||
struct App {
|
||||
@@ -34,20 +41,41 @@ struct App {
|
||||
|
||||
items: Vec<String>,
|
||||
selected: usize,
|
||||
|
||||
config: Option<core::config::Config>,
|
||||
login_user: String,
|
||||
login_pass: String,
|
||||
login_focus: LoginFocus,
|
||||
login_error: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
fn default() -> Self {
|
||||
let config_status = match core::config::load_config() {
|
||||
Ok(cfg) => format!("config: ok ({})", cfg.base_url),
|
||||
Err(e) => format!("config: missing/invalid ({:?})", e),
|
||||
let loaded = core::config::load_config().ok();
|
||||
|
||||
let (view_stack, config_status) = match &loaded {
|
||||
Some(cfg) if cfg.access_token.is_some() => (
|
||||
vec![View::Categories],
|
||||
format!("auth: token present ({})", cfg.base_url),
|
||||
),
|
||||
Some(cfg) => (
|
||||
vec![View::Login],
|
||||
format!("auth: needs login ({})", cfg.base_url),
|
||||
),
|
||||
None => (
|
||||
vec![View::Login],
|
||||
"config: missing/invalid (need base_url)".to_string(),
|
||||
),
|
||||
};
|
||||
|
||||
Self {
|
||||
should_exit: false,
|
||||
status: "Press q to quit".to_string(),
|
||||
config_status,
|
||||
view_stack: vec![View::Categories],
|
||||
config: loaded,
|
||||
|
||||
view_stack,
|
||||
|
||||
items: vec![
|
||||
"Shows".into(),
|
||||
"Movies".into(),
|
||||
@@ -60,10 +88,16 @@ impl Default for App {
|
||||
"Settings".into(),
|
||||
],
|
||||
selected: 0,
|
||||
|
||||
login_user: String::new(),
|
||||
login_pass: String::new(),
|
||||
login_focus: LoginFocus::User,
|
||||
login_error: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl App {
|
||||
fn current_view(&self) -> &View {
|
||||
self.view_stack.last().unwrap()
|
||||
@@ -115,6 +149,75 @@ fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<
|
||||
}
|
||||
|
||||
fn handle_key(app: &mut App, code: KeyCode) {
|
||||
// Special input handling in Login view
|
||||
if matches!(app.current_view(), View::Login) {
|
||||
match code {
|
||||
KeyCode::Char('q') => { app.should_exit = true; return; }
|
||||
KeyCode::Esc => { app.should_exit = true; return; }
|
||||
|
||||
KeyCode::Tab => {
|
||||
app.login_focus = match app.login_focus {
|
||||
LoginFocus::User => LoginFocus::Pass,
|
||||
LoginFocus::Pass => LoginFocus::User,
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
KeyCode::Backspace => {
|
||||
match app.login_focus {
|
||||
LoginFocus::User => { app.login_user.pop(); }
|
||||
LoginFocus::Pass => { app.login_pass.pop(); }
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
KeyCode::Enter => {
|
||||
let cfg = match &app.config {
|
||||
Some(c) => c.clone(),
|
||||
None => {
|
||||
app.login_error = Some("No config loaded. Create config.toml with base_url.".to_string());
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let client = core::jellyfin::JellyfinClient::from_config(&cfg);
|
||||
match client.login_by_name(&app.login_user, &app.login_pass) {
|
||||
Ok(res) => {
|
||||
let mut new_cfg = cfg;
|
||||
new_cfg.access_token = Some(res.access_token);
|
||||
new_cfg.user_id = res.user_id;
|
||||
|
||||
if let Err(e) = core::config::save_config(&new_cfg) {
|
||||
app.login_error = Some(format!("login ok but failed to write config: {e:?}"));
|
||||
return;
|
||||
}
|
||||
|
||||
app.config_status = format!("auth: token present ({})", new_cfg.base_url);
|
||||
app.config = Some(new_cfg);
|
||||
app.login_error = None;
|
||||
app.login_pass.clear(); // don't keep password around
|
||||
app.view_stack = vec![View::Categories];
|
||||
app.status = "Logged in".to_string();
|
||||
}
|
||||
Err(e) => {
|
||||
app.login_error = Some(e);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
KeyCode::Char(c) => {
|
||||
match app.login_focus {
|
||||
LoginFocus::User => app.login_user.push(c),
|
||||
LoginFocus::Pass => app.login_pass.push(c),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_ => return,
|
||||
}
|
||||
}
|
||||
|
||||
match code {
|
||||
KeyCode::Char('q') => app.should_exit = true,
|
||||
|
||||
@@ -177,6 +280,9 @@ fn ui(frame: &mut Frame, app: &App) {
|
||||
View::Placeholder(name) => {
|
||||
render_placeholder(frame, name, panes.left_main, panes.right_main);
|
||||
}
|
||||
View::Login => {
|
||||
render_login(frame, app, panes.left_main, panes.right_main);
|
||||
}
|
||||
}
|
||||
|
||||
// Bottom
|
||||
@@ -267,4 +373,36 @@ fn layout(area: Rect) -> Panes {
|
||||
left_output,
|
||||
right_output,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn render_login(frame: &mut Frame, app: &App, left: Rect, right: Rect) {
|
||||
let focus = match app.login_focus {
|
||||
LoginFocus::User => "username",
|
||||
LoginFocus::Pass => "password",
|
||||
};
|
||||
|
||||
let masked = "*".repeat(app.login_pass.len());
|
||||
|
||||
let left_text = format!(
|
||||
"Login (Tab switch, Enter submit)\n\nusername: {}\npassword: {}\n\nfocus: {}\n",
|
||||
app.login_user, masked, focus
|
||||
);
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(left_text)
|
||||
.block(Block::bordered().title("Login").merge_borders(MergeStrategy::Exact)),
|
||||
left,
|
||||
);
|
||||
|
||||
let right_text = match &app.login_error {
|
||||
Some(e) => format!("Error:\n{e}"),
|
||||
None => "Enter Jellyfin credentials.\nToken will be stored in config.toml.".to_string(),
|
||||
};
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(right_text)
|
||||
.block(Block::bordered().title("Info").merge_borders(MergeStrategy::Exact)),
|
||||
right,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user