╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Login.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
123 " password "; 124 125 // refresh our entries in the session's focus-id -> *TextInput map with 126 // this frame's current input addresses, so the web/wasm form handling 127 // can find them by focus id 128 const inputs_arena = self.session.arena.allocator(); 129 try self.session.text_inputs.put(inputs_arena, username_input.getFocus().id, username_input); 130 try self.session.text_inputs.put(inputs_arena, password_input.getFocus().id, password_input); 131 132 try self.center.build(allocator, constraint, root_focus); 133 } 134 135 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 136 const box = &self.center.child.box; 137 const child_id = box.focus.child_id orelse return; 138 const current = self.indexOf(child_id) orelse return; 139 140 switch (key) { 141 // Shift+Tab walks backward through the form, matching the 142 // typical UX; arrow_up does the same. 143 .arrow_up, .back_tab => if (current > 0) { 144 root_focus.setFocus(self.nav_ids[current - 1]); 145 return; 146 }, 147 // Tab moves to the next field, matching the form-style UX users 148 // expect; arrow_down does the same. 149 .arrow_down, .tab => if (current + 1 < self.nav_ids.len) { 150 root_focus.setFocus(self.nav_ids[current + 1]); 151 return; 152 }, 153 // submit on Enter from any nav field — typical "press Enter in 154 // a form to submit" UX 155 .enter => { 156 try self.submit(allocator, root_focus); 157 return; 158 }, 159 else => {}, 160 } 161 162 if (current == button_index) { 163 switch (key) { 164 .mouse => |mouse| { 165 if (inp.leftClickOn(root_focus, child_id, mouse)) { 166 try self.submit(allocator, root_focus); 167 return; 168 } 169 }, 170 else => {}, 171 } 172 } 173 174 const username_input = &box.children.values()[username_index].widget.text_input; 175 const password_input = &box.children.values()[password_index].widget.text_input; 176 const username_len_before = username_input.content.items.len; 177 const password_len_before = password_input.content.items.len; 178 179 if (box.children.getIndex(child_id)) |idx| { 180 try box.children.values()[idx].widget.input(allocator, key, root_focus); 181 } 182 183 if (username_input.content.items.len != username_len_before or 184 password_input.content.items.len != password_len_before) 185 { 186 // any edit clears the failure flag so the "(invalid)" label 187 // doesn't linger after the user has typed a correction. 188 self.session.data.login_failure = null; 189 } 190 } 191 192 pub fn clearGrid(self: *View) void { 193 self.center.clearGrid(); 194 } 195 196 pub fn getGrid(self: View) ?Grid { 197 return self.center.getGrid(); 198 } 199 200 pub fn getFocus(self: *View) *Focus { 201 return self.center.getFocus(); 202 } 203 204 pub fn getSelectedIndex(self: View) ?usize { 205 const child_id = self.center.child.box.focus.child_id orelse return null; 206 return self.indexOf(child_id); 207 } 208 209 fn indexOf(self: View, child_id: usize) ?usize { 210 for (self.nav_ids, 0..) |id, i| { 211 if (id == child_id) return i; 212 } 213 return null; 214 } 215 216 fn submit(self: *View, allocator: std.mem.Allocator, root_focus: *Focus) !void { 217 if (comptime wasm) { 218 // no DB cursor available on the wasm render path 219 self.session.data.login_failure = .unknown_user; 220 return; 221 } 222 223 const box = &self.center.child.box; 224 const username_input = &box.children.values()[username_index].widget.text_input; 225 const password_input = &box.children.values()[password_index].widget.text_input; 226 227 const username = try username_input.text(allocator); 228 defer allocator.free(username); 229 230 const password = try password_input.text(allocator); 231 defer allocator.free(password); 232 233 const haxy_moment = self.session.haxy_moment orelse { 234 // no DB context (e.g. wasm/web rendering path); treat as unknown. 235 self.session.data.login_failure = .unknown_user; 236 return; 237 }; 238 239 const result = try evt.User.verifyCredentials(evt.AdminDB, evt.admin_repo_opts.hash, haxy_moment, self.session.arena, username, password); 240 switch (result) { 241 .unknown_user => { 242 self.session.data.login_failure = .unknown_user; 243 return; 244 }, 245 .wrong_password => { 246 self.session.data.login_failure = .wrong_password; 247 return; 248 }, 249 .success => |user_id| { 250 // dupe user_id into the arena so the session can hold a stable slice 251 const user_id_stable = try self.session.arena.allocator().dupe(u8, &user_id); 252 self.session.data.user_id = user_id_stable; 253 self.session.data.login_failure = null; 254 255 // adopt the user's persisted prefs 256 try self.session.loadUserPrefs(); 257 258 // wipe the entered credentials so they don't linger if the 259 // user returns to this page after logging out 260 username_input.clear(allocator); 261 password_input.clear(allocator); 262 263 // jump focus back to the users tab — the login button we 264 // just pressed is about to be hidden by the tab-label swap 265 root_focus.setFocus(self.success_redirect_tab_id); 266 }, 267 } 268 } 269 }; 270