Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 1x 35x 35x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 68x 68x 68x 68x 1x 1x 248x 248x 248x 248x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 1x 1x 1x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 16x 16x 35x 35x 35x 35x 35x 35x 35x 35x 49x 49x 49x 49x 49x 35x 35x 35x 35x 51x 51x 35x 35x 35x 25x 25x 25x 35x 35x 35x 1x 35x 35x 33x 33x 33x 33x 33x 33x 33x 25x 33x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 416x 416x 416x 35x 35x 632x 632x 632x 632x 632x 632x 632x 35x 35x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 11x 24x 24x 24x 24x 35x 35x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 35x 35x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 1x 1x 1x 1x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 7x 7x 35x 35x 35x 35x 35x 7x 7x 7x 1x 1x 7x 35x 35x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 7x 1x 1x 1x 7x 7x 7x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 316x 316x 35x 35x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 316x 124x 316x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 1x | <template>
<div class="admin-users">
<div class="flex justify-between items-center mb-6">
<h2 class="text-xl font-semibold text-fg">User Login Activity</h2>
<button
@click="activeTab === 'users' ? fetchUsers() : fetchLoginEvents()"
:disabled="loading"
class="px-3 py-1.5 text-sm bg-surface-alt text-fg-muted rounded hover:bg-line disabled:opacity-50"
>
{{ loading ? 'Loading...' : 'Refresh' }}
</button>
</div>
<!-- Error -->
<div
v-if="error"
class="mb-4 p-3 bg-red-50 border border-red-200 rounded text-red-700 text-sm"
>
{{ error }}
</div>
<!-- Tabs -->
<div class="mb-4 border-b border-line">
<nav class="flex space-x-4">
<button
@click="
activeTab = 'users';
fetchUsers();
"
:class="[
activeTab === 'users'
? 'border-b-2 border-brand-600 text-brand-600 dark:border-brand-300 dark:text-brand-300'
: 'text-fg-muted hover:text-fg',
'pb-2 text-sm font-medium',
]"
>
Users ({{ userTotal }})
</button>
<button
@click="
activeTab = 'events';
fetchLoginEvents();
"
:class="[
activeTab === 'events'
? 'border-b-2 border-brand-600 text-brand-600 dark:border-brand-300 dark:text-brand-300'
: 'text-fg-muted hover:text-fg',
'pb-2 text-sm font-medium',
]"
>
Login History ({{ eventTotal }})
</button>
</nav>
</div>
<!-- Users Tab -->
<div v-if="activeTab === 'users'">
<div v-if="loading" class="text-center py-8 text-fg-muted">
Loading users...
</div>
<div
v-else-if="users.length === 0"
class="text-center py-8 text-fg-muted"
>
No users found.
</div>
<div v-else class="overflow-x-auto">
<table class="min-w-full text-sm">
<thead>
<tr class="text-left text-fg-muted border-b border-line">
<th class="pb-2 pr-4 font-medium">Username</th>
<th class="pb-2 pr-4 font-medium">Display Name</th>
<th class="pb-2 pr-4 font-medium">Email</th>
<th class="pb-2 pr-4 font-medium">Role</th>
<th class="pb-2 pr-4 font-medium">Team</th>
<th class="pb-2 pr-4 font-medium">Club</th>
<th class="pb-2 pr-4 font-medium">Last Login</th>
<th class="pb-2 pr-4 font-medium">Joined</th>
<th class="pb-2 font-medium">
<span class="sr-only">Edit</span>
</th>
</tr>
</thead>
<tbody class="divide-y divide-line">
<tr
v-for="user in users"
:key="user.id"
class="hover:bg-surface-alt"
>
<td class="py-2 pr-4 font-mono text-fg">
{{ user.username }}
</td>
<td class="py-2 pr-4 text-fg-muted">
{{ user.display_name || '—' }}
</td>
<!-- SB-1125: the address was in the payload all along. Most
accounts have none, and a blank cell reads as "nothing to
see" when it actually means a password reset cannot reach
this person.
Not amber: on this page amber already means "missing
affiliation" (SB-803), and a second meaning for the same
colour makes both harder to read. The words carry it. -->
<td class="py-2 pr-4">
<span
v-if="contactEmail(user)"
class="text-fg-muted"
data-testid="user-email"
>{{ contactEmail(user) }}</span
>
<span
v-else
class="text-fg-muted text-xs italic"
title="No address on file — a password reset or invite has nowhere to go."
data-testid="user-email-missing"
>No email</span
>
</td>
<td class="py-2 pr-4">
<span :class="roleBadgeClass(user.role)">{{
user.role || 'user'
}}</span>
<!-- SB-1460: gets the Android app link on the web. -->
<span
v-if="user.android_user"
class="ml-1"
title="Android user: sees the Android app download link"
data-testid="android-user-badge"
>📱</span
>
</td>
<!-- SB-803: affiliation was in the payload all along but never
shown, so an account named for a club and attached to
nothing looked identical to a correct one. -->
<td class="py-2 pr-4" :class="affiliationClass(user, 'team')">
{{
user.team_name || (user.team_id ? `#${user.team_id}` : '—')
}}
</td>
<td class="py-2 pr-4" :class="affiliationClass(user, 'club')">
{{
user.club_name || (user.club_id ? `#${user.club_id}` : '—')
}}
</td>
<td class="py-2 pr-4 text-fg-muted">
<span v-if="user.last_login_at">
<span
:class="
user.last_login_success
? 'text-green-600'
: 'text-red-500'
"
class="mr-1"
>{{ user.last_login_success ? '✓' : '✗' }}</span
>
{{ formatDate(user.last_login_at) }}
</span>
<span v-else class="text-fg-muted italic">Never</span>
</td>
<td class="py-2 pr-4 text-fg-muted">
{{ formatDate(user.created_at) }}
</td>
<td class="py-2">
<button
type="button"
class="px-2.5 py-1 text-xs font-medium rounded border border-line hover:bg-surface-alt"
:data-testid="`edit-user-${user.username}`"
@click="openEditor(user)"
>
Edit
</button>
</td>
</tr>
</tbody>
</table>
<!-- SB-803: edit sheet. Full-screen on small viewports on purpose —
this gets used one-handed at a match, where an inline row edit is
unusable. -->
<div
v-if="editing"
class="fixed inset-0 z-50 flex sm:items-center sm:justify-center bg-black/50"
data-testid="user-editor"
@click.self="closeEditor"
>
<div
class="bg-card w-full h-full sm:h-auto sm:max-w-md sm:rounded-xl p-5 overflow-y-auto flex flex-col gap-4"
>
<div class="flex items-start justify-between gap-3">
<div>
<h3 class="text-lg font-semibold text-fg">
{{ editing.username }}
</h3>
<p class="text-xs text-fg-muted">
{{ editing.display_name || 'No display name' }}
</p>
</div>
<button
type="button"
class="text-fg-muted hover:text-fg text-xl leading-none px-2"
data-testid="user-editor-close"
aria-label="Close"
@click="closeEditor"
>
×
</button>
</div>
<!-- SB-1128: most accounts have no address, and until now only
the user could add one (forgot-password asks for it). An
admin with the right address to hand could do nothing. -->
<label class="flex flex-col gap-1">
<span
class="text-xs font-medium text-fg-muted uppercase tracking-wide"
>Email</span
>
<input
v-model="form.email"
type="email"
placeholder="Not set"
data-testid="edit-email"
class="px-3 py-2.5 rounded-lg border border-line bg-surface text-fg"
/>
<span class="text-[11px] text-fg-muted">
Where password resets and invites go. Clearing it removes the
address.
</span>
</label>
<label class="flex flex-col gap-1">
<span
class="text-xs font-medium text-fg-muted uppercase tracking-wide"
>Role</span
>
<select
v-model="form.role"
data-testid="edit-role"
class="px-3 py-2.5 rounded-lg border border-line bg-surface text-fg"
>
<option v-for="r in ROLE_OPTIONS" :key="r" :value="r">
{{ r }}
</option>
</select>
</label>
<label class="flex flex-col gap-1">
<span
class="text-xs font-medium text-fg-muted uppercase tracking-wide"
>Team</span
>
<input
v-model="teamSearch"
type="search"
placeholder="Search teams…"
data-testid="team-search"
class="px-3 py-2.5 rounded-lg border border-line bg-surface text-fg"
/>
<select
v-model="form.team_id"
size="5"
data-testid="edit-team"
class="px-3 py-2 rounded-lg border border-line bg-surface text-fg"
@change="onTeamChange"
>
<option :value="null">— No team —</option>
<option v-for="t in filteredTeams" :key="t.id" :value="t.id">
{{ t.name }}
</option>
</select>
</label>
<!-- SB-1543: a fan's "my team" is a team at one age group. -->
<label class="flex flex-col gap-1">
<span
class="text-xs font-medium text-fg-muted uppercase tracking-wide"
>Age group</span
>
<select
v-model="form.assigned_age_group_id"
data-testid="edit-age-group"
:disabled="!form.team_id"
class="px-3 py-2.5 rounded-lg border border-line bg-surface text-fg disabled:opacity-50"
>
<option :value="null">— none —</option>
<option v-for="ag in teamAgeGroups" :key="ag.id" :value="ag.id">
{{ ag.name }}
</option>
</select>
</label>
<label class="flex flex-col gap-1">
<span
class="text-xs font-medium text-fg-muted uppercase tracking-wide"
>Club</span
>
<select
v-model="form.club_id"
data-testid="edit-club"
class="px-3 py-2.5 rounded-lg border border-line bg-surface text-fg"
>
<option :value="null">— No club —</option>
<option v-for="c in clubs" :key="c.id" :value="c.id">
{{ c.name }}
</option>
</select>
</label>
<!-- SB-1460: who gets the Android app is an admin's call, made
one family at a time, not the browser's. -->
<label class="flex items-start gap-3">
<input
v-model="form.android_user"
type="checkbox"
data-testid="edit-android-user"
class="mt-1 h-4 w-4"
/>
<span>
<span class="text-fg font-medium">Android user</span>
<span class="block text-xs text-fg-muted"
>Shows the "Install the Android app" link when they sign in on
the web.</span
>
</span>
</label>
<p
v-if="saveError"
class="text-sm text-red-500"
data-testid="save-error"
>
{{ saveError }}
</p>
<div class="flex gap-2 mt-auto sm:mt-2">
<button
type="button"
class="flex-1 px-4 py-2.5 rounded-lg border border-line text-fg"
@click="closeEditor"
>
Cancel
</button>
<button
type="button"
class="flex-1 px-4 py-2.5 rounded-lg bg-brand-600 text-white font-medium disabled:opacity-50"
data-testid="save-user"
:disabled="saving || !isDirty"
@click="saveUser"
>
{{ saving ? 'Saving…' : 'Save' }}
</button>
</div>
<!-- Kept away from Save, behind its own confirmation: this is
irreversible, and some of what it removes (roster history)
cannot be reconstructed (SB-1132). -->
<div class="mt-5 pt-4 border-t border-line">
<button
type="button"
class="text-sm text-red-600 dark:text-red-400 hover:underline disabled:opacity-40"
data-testid="delete-user"
:disabled="saving"
@click="startDelete"
>
Delete this account
</button>
</div>
</div>
</div>
</div>
</div>
<!-- Delete confirmation -->
<div
v-if="deleting"
class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4"
data-testid="delete-dialog"
>
<div class="bg-card border border-line rounded-xl w-full max-w-md p-5">
<h3 class="text-lg font-semibold text-fg">
Delete {{ deleting.username }}?
</h3>
<p
v-if="deletePreflight?.refusal"
class="mt-3 text-sm text-red-600 dark:text-red-400"
data-testid="delete-refusal"
>
{{ deletePreflight.refusal }}
</p>
<template v-else>
<p class="mt-3 text-sm text-fg-muted">This cannot be undone.</p>
<!-- The counts are the point. Roster history is user-generated
data nobody can reconstruct, and it goes silently otherwise. -->
<div
v-if="deletePreflight?.destroys?.length"
class="mt-3 p-3 rounded-lg bg-red-50 dark:bg-red-500/10 border border-red-200 dark:border-red-900"
data-testid="delete-destroys"
>
<p
class="text-xs font-semibold uppercase tracking-wide text-red-700 dark:text-red-300"
>
Also deleted
</p>
<ul class="mt-1 text-sm text-fg">
<li v-for="item in deletePreflight.destroys" :key="item.what">
{{ item.count }} {{ item.what }}
</li>
</ul>
</div>
<div
v-if="deletePreflight?.orphans?.length"
class="mt-3 text-xs text-fg-muted"
data-testid="delete-orphans"
>
Kept, but no longer attributed to anyone:
<span v-for="(item, i) in deletePreflight.orphans" :key="item.what">
{{ i ? ', ' : '' }}{{ item.count }} {{ item.what }}
</span>
</div>
<label class="block mt-4 text-sm text-fg-muted">
Type
<strong class="text-fg font-mono">{{ deleting.username }}</strong>
to confirm
<input
v-model="deleteConfirmText"
type="text"
autocomplete="off"
data-testid="delete-confirm-input"
class="mt-1 w-full px-3 py-2 rounded-lg border border-line bg-surface text-fg"
/>
</label>
</template>
<p
v-if="deleteError"
class="mt-3 text-sm text-red-600 dark:text-red-400"
data-testid="delete-error"
>
{{ deleteError }}
</p>
<div class="flex gap-3 mt-5">
<button
type="button"
class="flex-1 px-4 py-2.5 rounded-lg border border-line text-fg"
data-testid="delete-cancel"
@click="cancelDelete"
>
Cancel
</button>
<button
type="button"
class="flex-1 px-4 py-2.5 rounded-lg bg-red-600 text-white font-medium disabled:opacity-40"
data-testid="delete-confirm"
:disabled="!canConfirmDelete"
@click="confirmDelete"
>
{{ deletePending ? 'Deleting…' : 'Delete' }}
</button>
</div>
</div>
</div>
<!-- Login Events Tab -->
<div v-if="activeTab === 'events'">
<!-- Filters -->
<div class="flex flex-wrap gap-3 mb-4">
<input
v-model="filterUsername"
@input="debouncedFetch"
type="text"
placeholder="Filter by username..."
class="px-3 py-1.5 text-sm bg-card text-fg border border-line rounded w-48 focus:outline-none focus:ring-1 focus:ring-brand-500"
/>
<select
v-model="filterSuccess"
@change="fetchLoginEvents"
class="px-3 py-1.5 text-sm bg-card text-fg border border-line rounded focus:outline-none focus:ring-1 focus:ring-brand-500"
>
<option value="">All results</option>
<option value="true">Success only</option>
<option value="false">Failures only</option>
</select>
</div>
<div v-if="loading" class="text-center py-8 text-fg-muted">
Loading events...
</div>
<div
v-else-if="events.length === 0"
class="text-center py-8 text-fg-muted"
>
No login events found.
</div>
<div v-else>
<div class="overflow-x-auto">
<table class="min-w-full text-sm">
<thead>
<tr class="text-left text-fg-muted border-b border-line">
<th class="pb-2 pr-4 font-medium">Time</th>
<th class="pb-2 pr-4 font-medium">Username</th>
<th class="pb-2 pr-4 font-medium">Result</th>
<th class="pb-2 pr-4 font-medium">IP Address</th>
<th class="pb-2 font-medium">Role</th>
</tr>
</thead>
<tbody class="divide-y divide-line">
<tr
v-for="ev in events"
:key="ev.id"
class="hover:bg-surface-alt"
>
<td class="py-2 pr-4 text-fg-muted whitespace-nowrap">
{{ formatDate(ev.created_at) }}
</td>
<td class="py-2 pr-4 font-mono text-fg">
{{ ev.username }}
</td>
<td class="py-2 pr-4">
<span
v-if="ev.success"
class="text-green-700 bg-green-50 px-2 py-0.5 rounded text-xs font-medium"
>Success</span
>
<span
v-else
class="text-red-700 bg-red-50 px-2 py-0.5 rounded text-xs font-medium"
>
Failed{{
ev.failure_reason ? ` (${ev.failure_reason})` : ''
}}
</span>
</td>
<td class="py-2 pr-4 font-mono text-fg-muted text-xs">
{{ ev.client_ip || '—' }}
</td>
<td class="py-2 text-fg-muted text-xs">{{ ev.role || '—' }}</td>
</tr>
</tbody>
</table>
</div>
<!-- Pagination -->
<div
class="flex items-center justify-between mt-4 text-sm text-fg-muted"
>
<span>Showing {{ events.length }} of {{ eventTotal }} events</span>
<div class="flex gap-2">
<button
@click="prevPage"
:disabled="currentPage === 0"
class="px-3 py-1 border border-line rounded disabled:opacity-40 hover:bg-surface-alt"
>
Previous
</button>
<button
@click="nextPage"
:disabled="(currentPage + 1) * pageSize >= eventTotal"
class="px-3 py-1 border border-line rounded disabled:opacity-40 hover:bg-surface-alt"
>
Next
</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import { ref, computed, onMounted } from 'vue';
import { useAuthStore } from '@/stores/auth';
import { getApiBaseUrl } from '@/config/api';
export default {
name: 'AdminUsers',
setup() {
const authStore = useAuthStore();
const loading = ref(false);
const error = ref(null);
const activeTab = ref('users');
const users = ref([]);
const userTotal = ref(0);
const events = ref([]);
const eventTotal = ref(0);
const currentPage = ref(0);
const pageSize = 100;
const filterUsername = ref('');
const filterSuccess = ref('');
let debounceTimer = null;
const fetchUsers = async () => {
loading.value = true;
error.value = null;
try {
const data = await authStore.apiRequest(
`${getApiBaseUrl()}/api/admin/users`,
{ method: 'GET' }
);
users.value = data.users || [];
userTotal.value = data.total || 0;
} catch (err) {
error.value = err.message || 'Failed to fetch users';
} finally {
loading.value = false;
}
};
// --- SB-803: editing ---
// Written in the hyphen convention; the backend accepts both spellings and
// roles.js normalizes. Do not introduce a third form here.
const ROLE_OPTIONS = [
'admin',
'club_manager',
'club-fan',
'team-manager',
'team-player',
'team-fan',
];
const editing = ref(null);
const form = ref({
role: null,
team_id: null,
club_id: null,
assigned_age_group_id: null,
email: '',
android_user: false,
});
const saving = ref(false);
const saveError = ref(null);
// Deletion state. Separate from the editor's, because the dialog has to
// survive the editor closing and carries its own failure mode.
const deleting = ref(null);
const deletePreflight = ref(null);
const deleteConfirmText = ref('');
const deleteError = ref(null);
const deletePending = ref(false);
// Typing the username, not an "are you sure": this is irreversible and
// some of what it removes cannot be reconstructed (SB-1132).
const canConfirmDelete = computed(
() =>
!deletePending.value &&
!deletePreflight.value?.refusal &&
deleteConfirmText.value.trim() === deleting.value?.username
);
const teams = ref([]);
const clubs = ref([]);
const teamSearch = ref('');
// 183 teams in a phone-sized list is unusable, so the list is filtered
// rather than scrolled.
const filteredTeams = computed(() => {
const q = teamSearch.value.trim().toLowerCase();
const list = q
? teams.value.filter(t => (t.name || '').toLowerCase().includes(q))
: teams.value;
return list.slice(0, 50);
});
// The selected team's age groups come from its team_mappings, which
// /api/teams already flattens into age_groups.
const teamAgeGroups = computed(() => {
const team = teams.value.find(t => t.id === form.value.team_id);
return team?.age_groups || [];
});
const ageGroupChanged = computed(
() =>
!!editing.value &&
form.value.assigned_age_group_id !==
(editing.value.assigned_age_group_id ?? null)
);
// An age group belongs to a team; a new team starts with none picked.
const onTeamChange = () => {
form.value.assigned_age_group_id = null;
};
const isDirty = computed(() => {
if (!editing.value) return false;
return (
form.value.role !== editing.value.role ||
form.value.team_id !== (editing.value.team_id ?? null) ||
form.value.club_id !== (editing.value.club_id ?? null) ||
ageGroupChanged.value ||
form.value.email.trim() !== (editing.value.email ?? '') ||
form.value.android_user !== Boolean(editing.value.android_user)
);
});
// Flag an affiliation the role implies but the account lacks. This is the
// state four real accounts are in, and it was invisible before.
/**
* The address this account can actually be reached at, or null (SB-1125).
*
* MT keeps username as the primary identifier and synthesises
* `username@missingtable.local` so Supabase Auth has something to key on.
* That is a login identity, not a mailbox — nothing can be delivered to
* it, `.local` being reserved for mDNS. Showing it in a column headed
* Email would tell an admin the account is reachable when it is not,
* which is worse than showing nothing.
*/
const contactEmail = user => {
const email = user?.email;
if (!email) return null;
return email.endsWith('@missingtable.local') ? null : email;
};
const affiliationClass = (user, kind) => {
const role = (user.role || '').replace(/_/g, '-');
const expectsTeam = role.startsWith('team-') && role !== 'team-fan';
const expectsClub = role.startsWith('club-');
const missing =
(kind === 'team' && expectsTeam && !user.team_id) ||
(kind === 'club' && expectsClub && !user.club_id);
return missing ? 'text-amber-600 font-medium' : 'text-fg-muted';
};
const loadPickerData = async () => {
if (teams.value.length && clubs.value.length) return;
try {
const [teamData, clubData] = await Promise.all([
authStore.apiRequest(`${getApiBaseUrl()}/api/teams`, {
method: 'GET',
}),
authStore.apiRequest(`${getApiBaseUrl()}/api/clubs`, {
method: 'GET',
}),
]);
const t = Array.isArray(teamData) ? teamData : teamData?.teams || [];
const c = Array.isArray(clubData) ? clubData : clubData?.clubs || [];
teams.value = [...t].sort((a, b) =>
(a.name || '').localeCompare(b.name || '')
);
clubs.value = [...c].sort((a, b) =>
(a.name || '').localeCompare(b.name || '')
);
} catch (err) {
saveError.value = err.message || 'Failed to load teams and clubs';
}
};
const openEditor = async user => {
saveError.value = null;
teamSearch.value = '';
editing.value = user;
form.value = {
role: user.role,
team_id: user.team_id ?? null,
club_id: user.club_id ?? null,
assigned_age_group_id: user.assigned_age_group_id ?? null,
// The synthetic @missingtable.local identity is not a contact
// address, so the field opens empty rather than pre-filled with
// something nothing can be delivered to (SB-1125).
email: contactEmail(user) ?? '',
android_user: Boolean(user.android_user),
};
await loadPickerData();
};
const startDelete = async () => {
deleting.value = editing.value;
deletePreflight.value = null;
deleteConfirmText.value = '';
deleteError.value = null;
try {
deletePreflight.value = await authStore.apiRequest(
`${getApiBaseUrl()}/api/auth/users/${deleting.value.id}/delete-preflight`
);
} catch (err) {
// Without the preflight we cannot say what would be destroyed, and
// agreeing to an unknown is worse than not offering the button.
deleteError.value =
err.message || 'Could not check what this would delete.';
deletePreflight.value = {
refusal: 'Could not check what this would delete.',
};
}
};
const cancelDelete = () => {
deleting.value = null;
deletePreflight.value = null;
deleteConfirmText.value = '';
deleteError.value = null;
};
const confirmDelete = async () => {
if (!canConfirmDelete.value) return;
deletePending.value = true;
deleteError.value = null;
try {
await authStore.apiRequest(
`${getApiBaseUrl()}/api/auth/users/${deleting.value.id}`,
{ method: 'DELETE' }
);
cancelDelete();
closeEditor();
await fetchUsers();
} catch (err) {
// Guard rejections (own account, last admin, sent email) arrive here
// as the sentence the admin needs to read.
deleteError.value = err.message || 'Failed to delete';
} finally {
deletePending.value = false;
}
};
const closeEditor = () => {
editing.value = null;
saveError.value = null;
};
// assigned_age_group_id goes only when changed: the API clears it on a
// team change it was not sent with, and re-sending the old value would
// be rejected by the new team.
const patchBody = () => {
const { assigned_age_group_id, ...rest } = form.value;
const body = { ...rest, email: form.value.email.trim() };
if (ageGroupChanged.value) {
body.assigned_age_group_id = assigned_age_group_id;
}
return body;
};
const saveUser = async () => {
if (!editing.value || !isDirty.value) return;
saving.value = true;
saveError.value = null;
try {
await authStore.apiRequest(
`${getApiBaseUrl()}/api/admin/users/${editing.value.id}`,
{
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
// An empty string is how the address is cleared, and the API
// distinguishes that from "not sent".
body: JSON.stringify(patchBody()),
}
);
closeEditor();
await fetchUsers();
} catch (err) {
// Guardrail rejections (last admin, own role) arrive here as the
// message the user needs to read, so surface it rather than a generic.
saveError.value = err.message || 'Failed to save';
} finally {
saving.value = false;
}
};
const fetchLoginEvents = async () => {
loading.value = true;
error.value = null;
try {
const params = new URLSearchParams({
limit: pageSize,
offset: currentPage.value * pageSize,
});
if (filterUsername.value) params.set('username', filterUsername.value);
if (filterSuccess.value !== '')
params.set('success', filterSuccess.value);
const data = await authStore.apiRequest(
`${getApiBaseUrl()}/api/admin/users/login-events?${params}`,
{ method: 'GET' }
);
events.value = data.events || [];
eventTotal.value = data.total || 0;
} catch (err) {
error.value = err.message || 'Failed to fetch login events';
} finally {
loading.value = false;
}
};
const debouncedFetch = () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
currentPage.value = 0;
fetchLoginEvents();
}, 400);
};
const prevPage = () => {
if (currentPage.value > 0) {
currentPage.value--;
fetchLoginEvents();
}
};
const nextPage = () => {
currentPage.value++;
fetchLoginEvents();
};
const formatDate = iso => {
if (!iso) return '—';
return new Date(iso).toLocaleString();
};
const roleBadgeClass = role => {
const map = {
admin:
'bg-purple-100 text-purple-800 px-2 py-0.5 rounded text-xs font-medium',
club_manager:
'bg-brand-100 text-brand-800 px-2 py-0.5 rounded text-xs font-medium',
team_manager:
'bg-indigo-100 text-indigo-800 px-2 py-0.5 rounded text-xs font-medium',
'team-manager':
'bg-indigo-100 text-indigo-800 px-2 py-0.5 rounded text-xs font-medium',
team_player:
'bg-green-100 text-green-800 px-2 py-0.5 rounded text-xs font-medium',
'team-player':
'bg-green-100 text-green-800 px-2 py-0.5 rounded text-xs font-medium',
};
return (
map[role] ||
'bg-gray-100 text-gray-700 px-2 py-0.5 rounded text-xs font-medium'
);
};
onMounted(fetchUsers);
return {
// SB-803 editing
ROLE_OPTIONS,
editing,
form,
saving,
saveError,
teams,
clubs,
teamSearch,
filteredTeams,
teamAgeGroups,
onTeamChange,
isDirty,
affiliationClass,
contactEmail,
deleting,
deletePreflight,
deleteConfirmText,
deleteError,
deletePending,
canConfirmDelete,
startDelete,
cancelDelete,
confirmDelete,
openEditor,
closeEditor,
saveUser,
loading,
error,
activeTab,
users,
userTotal,
events,
eventTotal,
currentPage,
pageSize,
filterUsername,
filterSuccess,
fetchUsers,
fetchLoginEvents,
debouncedFetch,
prevPage,
nextPage,
formatDate,
roleBadgeClass,
};
},
};
</script>
|