authstate splitting into login state and auth state, quite before maddness

This commit is contained in:
filipriec
2025-04-13 17:06:59 +02:00
parent c1c4394f94
commit 1dd5f685a6

View File

@@ -11,22 +11,28 @@ lazy_static! {
]; ];
} }
/// Represents the authenticated session state
#[derive(Default)] #[derive(Default)]
pub struct AuthState { pub struct AuthState {
pub return_selected: bool,
pub username: String,
pub password: String,
pub error_message: Option<String>,
pub current_field: usize,
pub current_cursor_pos: usize,
pub auth_token: Option<String>, pub auth_token: Option<String>,
pub user_id: Option<String>, pub user_id: Option<String>,
pub role: Option<String>, pub role: Option<String>,
pub decoded_username: Option<String>, pub decoded_username: Option<String>,
}
/// Represents the state of the Login form UI
#[derive(Default)]
pub struct LoginState {
pub username: String, // Input field for username/email
pub password: String, // Input field for password
pub error_message: Option<String>, // Error message specific to login attempt
pub current_field: usize, // 0 for username, 1 for password
pub current_cursor_pos: usize, // Cursor position within current field
pub has_unsaved_changes: bool, pub has_unsaved_changes: bool,
} }
#[derive(Default, Clone)] // Add Clone derive /// Represents the state of the Registration form UI
#[derive(Default, Clone)]
pub struct RegisterState { pub struct RegisterState {
pub username: String, pub username: String,
pub email: String, pub email: String,
@@ -44,24 +50,33 @@ pub struct RegisterState {
} }
impl AuthState { impl AuthState {
/// Creates a new empty AuthState (unauthenticated)
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
return_selected: false,
username: String::new(),
password: String::new(),
error_message: None,
current_field: 0,
current_cursor_pos: 0,
auth_token: None, auth_token: None,
user_id: None, user_id: None,
role: None, role: None,
has_unsaved_changes: false,
decoded_username: None, decoded_username: None,
} }
} }
} }
impl LoginState {
/// Creates a new empty LoginState
pub fn new() -> Self {
Self {
username: String::new(),
password: String::new(),
error_message: None,
current_field: 0,
current_cursor_pos: 0,
has_unsaved_changes: false,
}
}
}
impl RegisterState { impl RegisterState {
/// Creates a new empty RegisterState
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
username: String::new(), username: String::new(),
@@ -80,7 +95,7 @@ impl RegisterState {
} }
} }
/// Updates role suggestions based on current input. /// Updates role suggestions based on current input
pub fn update_role_suggestions(&mut self) { pub fn update_role_suggestions(&mut self) {
let current_input = self.role.to_lowercase(); let current_input = self.role.to_lowercase();
self.role_suggestions = AVAILABLE_ROLES self.role_suggestions = AVAILABLE_ROLES
@@ -92,7 +107,7 @@ impl RegisterState {
} }
} }
impl CanvasState for AuthState { impl CanvasState for LoginState {
fn current_field(&self) -> usize { fn current_field(&self) -> usize {
self.current_field self.current_field
} }
@@ -126,7 +141,7 @@ impl CanvasState for AuthState {
match self.current_field { match self.current_field {
0 => &mut self.username, 0 => &mut self.username,
1 => &mut self.password, 1 => &mut self.password,
_ => panic!("Invalid current_field index in AuthState"), _ => panic!("Invalid current_field index in LoginState"),
} }
} }
@@ -135,13 +150,12 @@ impl CanvasState for AuthState {
} }
fn set_current_field(&mut self, index: usize) { fn set_current_field(&mut self, index: usize) {
if index < 2 { // AuthState only has 2 fields if index < 2 {
self.current_field = index; self.current_field = index;
// IMPORTANT: Clamp cursor position to the length of the NEW field
let len = match self.current_field { let len = match self.current_field {
0 => self.username.len(), 0 => self.username.len(),
1 => self.password.len(), 1 => self.password.len(),
_ => 0, _ => 0,
}; };
self.current_cursor_pos = self.current_cursor_pos.min(len); self.current_cursor_pos = self.current_cursor_pos.min(len);
} }
@@ -149,26 +163,23 @@ impl CanvasState for AuthState {
fn set_current_cursor_pos(&mut self, pos: usize) { fn set_current_cursor_pos(&mut self, pos: usize) {
let len = match self.current_field { let len = match self.current_field {
0 => self.username.len(), 0 => self.username.len(),
1 => self.password.len(), 1 => self.password.len(),
_ => 0, _ => 0,
}; };
// Ensure stored position is always valid
self.current_cursor_pos = pos.min(len); self.current_cursor_pos = pos.min(len);
} }
fn set_has_unsaved_changes(&mut self, changed: bool) { fn set_has_unsaved_changes(&mut self, changed: bool) {
// Allow the generic handler to signal changes
self.has_unsaved_changes = changed; self.has_unsaved_changes = changed;
} }
// --- Autocomplete Support (Not Used for AuthState) ---
fn get_suggestions(&self) -> Option<&[String]> { fn get_suggestions(&self) -> Option<&[String]> {
None // AuthState doesn't provide suggestions None
} }
fn get_selected_suggestion_index(&self) -> Option<usize> { fn get_selected_suggestion_index(&self) -> Option<usize> {
None // AuthState doesn't have selected suggestions None
} }
} }
@@ -231,12 +242,12 @@ impl CanvasState for RegisterState {
"Email (Optional)", "Email (Optional)",
"Password (Optional)", "Password (Optional)",
"Confirm Password", "Confirm Password",
"Role (Oprional)" "Role (Optional)"
] ]
} }
fn set_current_field(&mut self, index: usize) { fn set_current_field(&mut self, index: usize) {
if index < 5 { // RegisterState has 5 fields if index < 5 {
self.current_field = index; self.current_field = index;
let len = match self.current_field { let len = match self.current_field {
0 => self.username.len(), 0 => self.username.len(),
@@ -267,7 +278,6 @@ impl CanvasState for RegisterState {
} }
fn get_suggestions(&self) -> Option<&[String]> { fn get_suggestions(&self) -> Option<&[String]> {
// Only show suggestions for the role field (index 4) when requested
if self.current_field == 4 && self.in_suggestion_mode && self.show_role_suggestions { if self.current_field == 4 && self.in_suggestion_mode && self.show_role_suggestions {
Some(&self.role_suggestions) Some(&self.role_suggestions)
} else { } else {