docs: design activation auth flow

This commit is contained in:
TTlaji committed 2026-06-05 16:36:09 +08:00
1 parent 3a51c389be
commit 8f3fd04618
1 file changed
+241
@@ -0,0 +1,241 @@
# Speaking Lab Activation Auth Design
## Goal
Turn Speaking Lab from a local-only prototype into a shareable private product prototype with gated registration.
Users should be able to register only after entering a valid activation code. Registered users can use the home and study pages. Admins can upload and manage learning materials, generate activation codes, and manage user accounts.
## Scope
Included:
- New centered login/register page inspired by the provided TEco Lab reference.
- Login with server-stored username and password.
- Registration with one-time activation code.
- Server-side SQLite storage for users and activation codes.
- Session-style browser login state using a token returned by the backend.
- Admin-only backend APIs for user listing, user enable/disable, activation code generation, activation code listing, and unused activation code deletion.
- Admin page sections for account management and activation code management.
- Home and study pages redirect unauthenticated users to the login page.
- Home and study pages show the admin entry only for admin users.
- Remove duplicate "enter study page" entry from the study page/right-side learning area.
Excluded for this pass:
- Email verification.
- Paid checkout.
- Password reset.
- Multi-device session management UI.
- Per-user cloud sync for study progress.
- Advanced user roles beyond `user` and `admin`.
- Expiring activation codes.
- Bulk CSV import/export of activation codes.
## Roles
User:
- Logs in with a registered server account.
- Can access home and study pages.
- Can study, favorite, and keep existing browser-local learning progress.
- Cannot see admin buttons.
- Cannot access admin APIs.
Admin:
- Logs in with `.env` `ADMIN_USERNAME` and `ADMIN_PASSWORD`.
- Can access home, study, and admin pages.
- Can use existing material upload/edit/delete tools.
- Can view and disable users.
- Can generate and manage activation codes.
## User Flow
Login:
1. Visitor opens `/`, `/study.html`, or `/admin.html`.
2. If no valid auth token is stored, the page redirects to `/login.html`.
3. Visitor enters username and password.
4. Backend validates credentials.
5. Frontend stores returned auth token and role in `sessionStorage`.
6. Admin users go to the admin page if they came from admin, otherwise home.
7. Normal users go to home or the originally requested study URL.
Registration:
1. Visitor clicks the register link on the login page.
2. Visitor enters activation code.
3. Backend checks the code exists and is unused.
4. Visitor enters username and password.
5. Backend creates a user, marks the activation code as used, and returns a login token.
6. Frontend stores the token and sends the user to home.
Admin activation code management:
1. Admin opens the admin page.
2. Admin enters a quantity and clicks generate.
3. Backend creates random one-time codes.
4. Admin can copy codes and share them manually.
5. Used codes show the bound username and used time.
6. Unused codes can be deleted.
Admin account management:
1. Admin opens the admin page.
2. Admin sees registered users with status, role, created time, and last login time.
3. Admin can disable or enable normal users.
4. Disabled users cannot log in.
## Data Model
Extend the existing SQLite database.
Existing `users` table should be reused. It already has `email`, `password_hash`, `display_name`, and `role`; for this pass:
- Treat `email` as the unique login username field to avoid a risky migration.
- Store display name as the same value for this version.
- Add `is_active INTEGER NOT NULL DEFAULT 1`.
- Add `last_login_at TEXT`.
New `activation_codes` table:
- `id INTEGER PRIMARY KEY AUTOINCREMENT`
- `code TEXT NOT NULL UNIQUE`
- `created_by TEXT NOT NULL`
- `used_by_user_id INTEGER`
- `created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP`
- `used_at TEXT`
- `status TEXT NOT NULL DEFAULT 'unused'`
Valid statuses:
- `unused`
- `used`
## Backend API
Public auth APIs:
- `POST /api/auth/login`
- Body: `{ "username": "...", "password": "..." }`
- Returns: `{ "token": "...", "user": { "username": "...", "role": "user|admin" } }`
- `POST /api/auth/register`
- Body: `{ "activationCode": "...", "username": "...", "password": "..." }`
- Returns the same shape as login.
- `GET /api/auth/session`
- Requires `Authorization: Bearer <token>`.
- Returns the current user and role.
Admin APIs:
- `GET /api/admin/users`
- `PATCH /api/admin/users/{user_id}`
- Body: `{ "isActive": true|false }`
- `GET /api/admin/activation-codes`
- `POST /api/admin/activation-codes`
- Body: `{ "quantity": 1-100 }`
- `DELETE /api/admin/activation-codes/{code_id}`
- Only allowed for unused codes.
Existing material and AI admin APIs remain protected by the current admin credential mechanism, but frontend requests should use the authenticated admin session. Internally, the backend can support both the existing Basic admin credentials and the new Bearer admin token during this transition.
## Frontend
`login.html`:
- Full viewport centered layout.
- White translucent card with subtle blur and shadow.
- Brand title `Speaking Lab`.
- Login mode: username, password, login button.
- Register mode: activation code, username, password, confirm password, register button.
- Small links for switching between login and register.
`index.html` and `home.js`:
- Remove the inline email login/register form.
- Require authenticated session before rendering user-specific controls.
- Show username in the top bar.
- Show admin entry only when role is `admin`.
`study.html` and `app.js`:
- Remove the inline email login/register form.
- Require authenticated session.
- Keep learning progress in browser storage for now.
- Remove duplicate "enter study page" control from the right-side learning area if present.
- Show admin entry only for admin users if such an entry exists.
`admin.html` and `admin.js`:
- Admin page should verify admin session before rendering admin content.
- Keep the existing material management form.
- Add account management panel.
- Add activation code management panel.
## Security Notes
This is a product prototype, not a full commercial auth system.
Required safeguards for this pass:
- Passwords must be hashed server-side before storage.
- Activation codes must be random and hard to guess.
- Admin-only APIs must reject normal user tokens.
- Disabled users must be blocked at login and session check.
- Tokens should be signed using an `.env` secret if configured, with a development fallback warning.
Deferred safeguards:
- HttpOnly cookies.
- CSRF protection.
- Rate limiting.
- Password reset.
- Audit logs.
## Error Handling
Login:
- Wrong credentials: show a clear Chinese error meaning "account or password is incorrect."
- Disabled account: show a clear Chinese error meaning "account is disabled, contact admin."
Registration:
- Missing activation code: show a clear Chinese error asking for an activation code.
- Invalid or used activation code: show a clear Chinese error that the code is invalid or already used.
- Duplicate username: show a clear Chinese error that the account already exists.
- Password mismatch handled in frontend before request.
Admin:
- Non-admin access: redirect to login or show a clear Chinese error that admin access is required.
- Deleting used activation code: show a clear Chinese error that used activation codes cannot be deleted.
## Testing
Automated checks:
```powershell
python -m py_compile backend\main.py backend\db.py
node --check login.js
node --check home.js
node --check app.js
node --check admin.js
node tests\progress-helpers.test.mjs
node tests\admin-file-input.test.mjs
```
Manual browser checks:
1. Open `/` while logged out and confirm redirect to `/login.html`.
2. Try a bad login and confirm a clear error.
3. Log in as admin and confirm the admin button appears.
4. Generate an activation code in admin.
5. Log out and register a normal user with that code.
6. Confirm the normal user can access home/study and cannot see admin.
7. Confirm the used activation code cannot be reused.
8. Disable the user in admin.
9. Confirm the disabled user can no longer log in.