╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Login.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
143 return; 144 }, 145 else => {}, 146 } 147 148 if (current == button_index) { 149 switch (key) { 150 .enter => { 151 try self.submit(allocator); 152 return; 153 }, 154 .mouse => |mouse| { 155 if (inp.leftClickOn(root_focus, child_id, mouse)) { 156 try self.submit(allocator); 157 return; 158 } 159 }, 160 else => {}, 161 } 162 } 163 164 const username_input = &box.children.values()[username_index].widget.text_input; 165 const password_input = &box.children.values()[password_index].widget.text_input; 166 const username_len_before = username_input.content.items.len; 167 const password_len_before = password_input.content.items.len; 168 169 if (box.children.getIndex(child_id)) |idx| { 170 try box.children.values()[idx].widget.input(allocator, key, root_focus); 171 } 172 173 if (username_input.content.items.len != username_len_before or 174 password_input.content.items.len != password_len_before) 175 { 176 // any edit clears the failure flag so the "(invalid)" label 177 // doesn't linger after the user has typed a correction. 178 self.session.data.login_failure = null; 179 } 180 } 181 182 pub fn clearGrid(self: *View) void { 183 self.center.clearGrid(); 184 } 185 186 pub fn getGrid(self: View) ?Grid { 187 return self.center.getGrid(); 188 } 189 190 pub fn getFocus(self: *View) *Focus { 191 return self.center.getFocus(); 192 } 193 194 pub fn getSelectedIndex(self: View) ?usize { 195 const child_id = self.center.child.box.focus.child_id orelse return null; 196 return self.indexOf(child_id); 197 } 198 199 fn indexOf(self: View, child_id: usize) ?usize { 200 for (self.nav_ids, 0..) |id, i| { 201 if (id == child_id) return i; 202 } 203 return null; 204 } 205 206 fn submit(self: *View, allocator: std.mem.Allocator) !void { 207 if (comptime wasm) { 208 // no DB cursor available on the wasm render path 209 self.session.data.login_failure = .unknown_user; 210 return; 211 } 212 213 const box = &self.center.child.box; 214 const username_input = &box.children.values()[username_index].widget.text_input; 215 const password_input = &box.children.values()[password_index].widget.text_input; 216 217 const username = try username_input.text(allocator); 218 defer allocator.free(username); 219 220 const password = try password_input.text(allocator); 221 defer allocator.free(password); 222 223 const haxy_moment = self.session.haxy_moment orelse { 224 // no DB context (e.g. wasm/web rendering path); treat as unknown. 225 self.session.data.login_failure = .unknown_user; 226 return; 227 }; 228 229 const result = try evt.User.verifyCredentials(evt.AdminDB, evt.admin_repo_opts.hash, haxy_moment, self.session.arena, username, password); 230 switch (result) { 231 .unknown_user => { 232 self.session.data.login_failure = .unknown_user; 233 return; 234 }, 235 .wrong_password => { 236 self.session.data.login_failure = .wrong_password; 237 return; 238 }, 239 .success => |user_id| { 240 // dupe user_id into the arena so the session can hold a stable slice 241 const user_id_stable = try self.session.arena.allocator().dupe(u8, &user_id); 242 self.session.data.user_id = user_id_stable; 243 self.session.data.login_failure = null; 244 245 // adopt the user's name and persisted prefs 246 try self.session.loadUser(); 247 248 // wipe the entered credentials so they don't linger if the 249 // user returns to this page after logging out 250 username_input.clear(allocator); 251 password_input.clear(allocator); 252 253 // leave the auth tab for the page it belongs to, where the 254 // web's /login redirect also lands 255 try self.session.navigate(self.session.data.current_page.pageRoot()); 256 }, 257 } 258 } 259 }; 260