Add username-based login + mandatory first-time admin setup; push-ready
- User id/login = username (was email). Email is a separate settable field. - Default admin: username admin / password 1234, must_setup=True. - Login forces /setup on first login: set email + change password, then clears must_setup. - New /api/auth/setup endpoint; JWT sub = username; admin routes use username. - Frontend: Login uses username, router guard forces /setup, new Setup.vue (email + new password + confirm), i18n EN/TH. - Tests: test_setup.py added; all suites adapted (m0/m1/routes/security/setup/e2e) PASS.
This commit is contained in:
@@ -23,13 +23,13 @@ def create_user():
|
|||||||
"""Create a user + provision a password (invite). Admin or super-admin only."""
|
"""Create a user + provision a password (invite). Admin or super-admin only."""
|
||||||
data = request.get_json(silent=True) or {}
|
data = request.get_json(silent=True) or {}
|
||||||
name = (data.get("name") or "").strip()
|
name = (data.get("name") or "").strip()
|
||||||
email = (data.get("email") or "").strip().lower()
|
username = (data.get("username") or data.get("email") or "").strip().lower()
|
||||||
password = data.get("password") or ""
|
password = data.get("password") or ""
|
||||||
role = (data.get("role") or "user").strip()
|
role = (data.get("role") or "user").strip()
|
||||||
org_id = (data.get("org_id") or current_user().get("org_id") or "org-default").strip()
|
org_id = (data.get("org_id") or current_user().get("org_id") or "org-default").strip()
|
||||||
|
|
||||||
if not email or not password:
|
if not username or not password:
|
||||||
raise ApiError("email and password are required")
|
raise ApiError("username and password are required")
|
||||||
if role not in Config.ROLES:
|
if role not in Config.ROLES:
|
||||||
raise ApiError(f"invalid role: {role}")
|
raise ApiError(f"invalid role: {role}")
|
||||||
# Only super_admin can create another admin/super_admin
|
# Only super_admin can create another admin/super_admin
|
||||||
@@ -38,7 +38,7 @@ def create_user():
|
|||||||
raise ApiError("only super_admin can grant admin roles", 403)
|
raise ApiError("only super_admin can grant admin roles", 403)
|
||||||
try:
|
try:
|
||||||
user = _store().create_user(
|
user = _store().create_user(
|
||||||
org_id=org_id, email=email, password=password, name=name, role=role
|
org_id=org_id, username=username, password=password, name=name, role=role
|
||||||
)
|
)
|
||||||
except AuthError as exc:
|
except AuthError as exc:
|
||||||
raise ApiError(str(exc))
|
raise ApiError(str(exc))
|
||||||
@@ -57,14 +57,14 @@ def list_users():
|
|||||||
return jsonify({"users": users})
|
return jsonify({"users": users})
|
||||||
|
|
||||||
|
|
||||||
@admin_bp.put("/users/<email>")
|
@admin_bp.put("/users/<username>")
|
||||||
@require_auth
|
@require_auth
|
||||||
@require_roles("admin")
|
@require_roles("admin")
|
||||||
def update_user(email: str):
|
def update_user(username: str):
|
||||||
data = request.get_json(silent=True) or {}
|
data = request.get_json(silent=True) or {}
|
||||||
email = email.strip().lower()
|
username = username.strip().lower()
|
||||||
actor = current_user()
|
actor = current_user()
|
||||||
target = _store().get_user_or_none(email)
|
target = _store().get_user_or_none(username)
|
||||||
if not target:
|
if not target:
|
||||||
raise ApiError("user not found", 404)
|
raise ApiError("user not found", 404)
|
||||||
|
|
||||||
@@ -75,14 +75,20 @@ def update_user(email: str):
|
|||||||
raise ApiError(f"invalid role: {role}")
|
raise ApiError(f"invalid role: {role}")
|
||||||
if actor.get("role") != "super_admin":
|
if actor.get("role") != "super_admin":
|
||||||
raise ApiError("only super_admin can change roles")
|
raise ApiError("only super_admin can change roles")
|
||||||
_store().set_role(email, role)
|
_store().set_role(username, role)
|
||||||
|
|
||||||
if "active" in data:
|
if "active" in data:
|
||||||
if actor.get("role") != "super_admin":
|
if actor.get("role") != "super_admin":
|
||||||
raise ApiError("only super_admin can activate/deactivate users")
|
raise ApiError("only super_admin can activate/deactivate users")
|
||||||
_store().set_active(email, bool(data.get("active")))
|
_store().set_active(username, bool(data.get("active")))
|
||||||
|
|
||||||
if "password" in data and data.get("password"):
|
if "password" in data and data.get("password"):
|
||||||
_store().set_password(email, data.get("password"))
|
_store().set_password(username, data.get("password"))
|
||||||
|
|
||||||
return jsonify({"user": _store().public_user(_store().get_user(email))})
|
if "email" in data:
|
||||||
|
try:
|
||||||
|
_store().set_email(username, data.get("email"))
|
||||||
|
except AuthError as exc:
|
||||||
|
raise ApiError(str(exc))
|
||||||
|
|
||||||
|
return jsonify({"user": _store().public_user(_store().get_user(username))})
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Auth routes: login + current user. No self-registration."""
|
"""Auth routes: login, current user, first-time admin setup. No self-registration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from flask import Blueprint, jsonify, request
|
from flask import Blueprint, jsonify, request
|
||||||
@@ -15,22 +15,53 @@ def _store():
|
|||||||
return current_app.extensions["user_store"]
|
return current_app.extensions["user_store"]
|
||||||
|
|
||||||
|
|
||||||
|
def _login_body(data: dict) -> str:
|
||||||
|
# Accept `username` (primary) or `email` (fallback), lower-cased.
|
||||||
|
return (data.get("username") or data.get("email") or "").strip().lower()
|
||||||
|
|
||||||
|
|
||||||
@auth_bp.post("/login")
|
@auth_bp.post("/login")
|
||||||
def login():
|
def login():
|
||||||
data = request.get_json(silent=True) or {}
|
data = request.get_json(silent=True) or {}
|
||||||
email = (data.get("email") or "").strip().lower()
|
username = _login_body(data)
|
||||||
password = data.get("password") or ""
|
password = data.get("password") or ""
|
||||||
if not email or not password:
|
if not username or not password:
|
||||||
raise ApiError("email and password are required")
|
raise ApiError("username and password are required")
|
||||||
try:
|
try:
|
||||||
user = _store().verify(email, password)
|
user = _store().verify(username, password)
|
||||||
token = _store().issue_token(user)
|
token = _store().issue_token(user)
|
||||||
except AuthError as exc:
|
except AuthError as exc:
|
||||||
raise ApiError(str(exc), 401)
|
raise ApiError(str(exc), 401)
|
||||||
return jsonify({"token": token, "user": _store().public_user(user)})
|
return jsonify({
|
||||||
|
"token": token,
|
||||||
|
"user": _store().public_user(user),
|
||||||
|
"must_setup": bool(user.get("must_setup")),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@auth_bp.get("/me")
|
@auth_bp.get("/me")
|
||||||
@require_auth
|
@require_auth
|
||||||
def me():
|
def me():
|
||||||
return jsonify({"user": _store().public_user(current_user())})
|
return jsonify({"user": _store().public_user(current_user())})
|
||||||
|
|
||||||
|
|
||||||
|
@auth_bp.post("/setup")
|
||||||
|
@require_auth
|
||||||
|
def setup():
|
||||||
|
"""First-time admin setup: set email + change password, then clear must_setup."""
|
||||||
|
user = current_user()
|
||||||
|
data = request.get_json(silent=True) or {}
|
||||||
|
username = (data.get("username") or user.get("username") or user.get("id") or "").strip().lower()
|
||||||
|
email = (data.get("email") or "").strip()
|
||||||
|
new_password = data.get("password") or ""
|
||||||
|
if not email or not new_password:
|
||||||
|
raise ApiError("email and new password are required")
|
||||||
|
try:
|
||||||
|
updated = _store().complete_setup(username, email, new_password)
|
||||||
|
except AuthError as exc:
|
||||||
|
raise ApiError(str(exc), 400)
|
||||||
|
return jsonify({
|
||||||
|
"ok": True,
|
||||||
|
"user": _store().public_user(updated),
|
||||||
|
"must_setup": False,
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
"""User + organization store and auth logic (JWT, password hashing, roles)."""
|
"""User + organization store and auth logic (JWT, password hashing, roles).
|
||||||
|
|
||||||
|
Login identity is the user's `username` (stable id). `email` is an optional
|
||||||
|
separate field that admins/users can set; the default admin must set an email
|
||||||
|
+before first real use (enforced via `must_setup`).
|
||||||
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -11,6 +17,8 @@ from werkzeug.security import check_password_hash, generate_password_hash
|
|||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..storage.store import JsonStore, StoreError, new_id
|
from ..storage.store import JsonStore, StoreError, new_id
|
||||||
|
|
||||||
|
EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
|
||||||
|
|
||||||
|
|
||||||
class AuthError(Exception):
|
class AuthError(Exception):
|
||||||
pass
|
pass
|
||||||
@@ -32,71 +40,114 @@ class UserStore:
|
|||||||
return self.orgs.get(org_id)
|
return self.orgs.get(org_id)
|
||||||
|
|
||||||
# ── users ──────────────────────────────────────────────────────────
|
# ── users ──────────────────────────────────────────────────────────
|
||||||
|
@staticmethod
|
||||||
|
def _norm(username: str) -> str:
|
||||||
|
return username.strip().lower()
|
||||||
|
|
||||||
def create_user(
|
def create_user(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
org_id: str,
|
org_id: str,
|
||||||
email: str,
|
username: str,
|
||||||
password: str,
|
password: str,
|
||||||
name: str,
|
name: str,
|
||||||
role: str = "user",
|
role: str = "user",
|
||||||
|
email: str | None = None,
|
||||||
|
must_setup: bool = False,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
if role not in Config.ROLES:
|
if role not in Config.ROLES:
|
||||||
raise AuthError(f"invalid role: {role}")
|
raise AuthError(f"invalid role: {role}")
|
||||||
org = self.orgs.get(org_id)
|
self.orgs.get(org_id)
|
||||||
email = email.strip().lower()
|
username = self._norm(username)
|
||||||
if not email or not password:
|
if not username or not password:
|
||||||
raise AuthError("email and password are required")
|
raise AuthError("username and password are required")
|
||||||
if self.users.get_or_none(email) is not None:
|
if not re.fullmatch(r"[a-zA-Z0-9_.-]{2,64}", username):
|
||||||
raise AuthError("a user with this email already exists")
|
raise AuthError("invalid username (letters/numbers/._- only, 2-64 chars)")
|
||||||
|
if self.users.get_or_none(username) is not None:
|
||||||
|
raise AuthError("a user with this username already exists")
|
||||||
|
email = (email or "").strip().lower() or None
|
||||||
|
if email:
|
||||||
|
if not EMAIL_RE.fullmatch(email):
|
||||||
|
raise AuthError("invalid email")
|
||||||
|
if self.email_exists(email):
|
||||||
|
raise AuthError("a user with this email already exists")
|
||||||
user = {
|
user = {
|
||||||
"id": email, # email = unique id/username
|
"id": username,
|
||||||
|
"username": username,
|
||||||
"email": email,
|
"email": email,
|
||||||
"org_id": org_id,
|
"org_id": org_id,
|
||||||
"org_name": org.get("name", ""),
|
"org_name": self.orgs.get(org_id).get("name", ""),
|
||||||
"name": name.strip() or email,
|
"name": name.strip() or username,
|
||||||
"password_hash": generate_password_hash(password),
|
"password_hash": generate_password_hash(password),
|
||||||
"role": role,
|
"role": role,
|
||||||
|
"must_setup": must_setup,
|
||||||
"created_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
"created_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||||
"active": True,
|
"active": True,
|
||||||
}
|
}
|
||||||
return self.users.create(user, key=email)
|
return self.users.create(user, key=username)
|
||||||
|
|
||||||
def get_user(self, email: str) -> dict[str, Any]:
|
def get_user(self, username: str) -> dict[str, Any]:
|
||||||
email = email.strip().lower()
|
return self.users.get(self._norm(username))
|
||||||
return self.users.get(email)
|
|
||||||
|
|
||||||
def get_user_or_none(self, email: str) -> dict[str, Any] | None:
|
def get_user_or_none(self, username: str) -> dict[str, Any] | None:
|
||||||
return self.users.get_or_none(email.strip().lower())
|
return self.users.get_or_none(self._norm(username))
|
||||||
|
|
||||||
|
def by_email(self, email: str) -> dict[str, Any] | None:
|
||||||
|
email = (email or "").strip().lower()
|
||||||
|
if not email:
|
||||||
|
return None
|
||||||
|
for u in self.users.all():
|
||||||
|
if u.get("email") and u["email"] == email:
|
||||||
|
return u
|
||||||
|
return None
|
||||||
|
|
||||||
|
def email_exists(self, email: str) -> bool:
|
||||||
|
return self.by_email(email) is not None
|
||||||
|
|
||||||
def list_users(self, *, org_id: str | None = None) -> list[dict[str, Any]]:
|
def list_users(self, *, org_id: str | None = None) -> list[dict[str, Any]]:
|
||||||
users = self.users.all()
|
users = self.users.all()
|
||||||
if org_id:
|
if org_id:
|
||||||
users = [u for u in users if u.get("org_id") == org_id]
|
users = [u for u in users if u.get("org_id") == org_id]
|
||||||
# Redact password hash
|
|
||||||
for u in users:
|
for u in users:
|
||||||
u.pop("password_hash", None)
|
u.pop("password_hash", None)
|
||||||
return users
|
return users
|
||||||
|
|
||||||
def set_active(self, email: str, active: bool) -> dict[str, Any]:
|
def set_active(self, username: str, active: bool) -> dict[str, Any]:
|
||||||
return self.users.update(email.strip().lower(), active=active)
|
return self.users.update(self._norm(username), active=active)
|
||||||
|
|
||||||
def set_role(self, email: str, role: str) -> dict[str, Any]:
|
def set_role(self, username: str, role: str) -> dict[str, Any]:
|
||||||
if role not in Config.ROLES:
|
if role not in Config.ROLES:
|
||||||
raise AuthError(f"invalid role: {role}")
|
raise AuthError(f"invalid role: {role}")
|
||||||
return self.users.update(email.strip().lower(), role=role)
|
return self.users.update(self._norm(username), role=role)
|
||||||
|
|
||||||
def set_password(self, email: str, new_password: str) -> dict[str, Any]:
|
def set_password(self, username: str, new_password: str) -> dict[str, Any]:
|
||||||
if not new_password:
|
if not new_password:
|
||||||
raise AuthError("password is required")
|
raise AuthError("password is required")
|
||||||
return self.users.update(
|
return self.users.update(
|
||||||
email.strip().lower(),
|
self._norm(username),
|
||||||
password_hash=generate_password_hash(new_password),
|
password_hash=generate_password_hash(new_password),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def set_email(self, username: str, email: str) -> dict[str, Any]:
|
||||||
|
email = (email or "").strip().lower()
|
||||||
|
if not EMAIL_RE.fullmatch(email):
|
||||||
|
raise AuthError("invalid email")
|
||||||
|
existing = self.by_email(email)
|
||||||
|
if existing and existing["id"] != self._norm(username):
|
||||||
|
raise AuthError("a user with this email already exists")
|
||||||
|
return self.users.update(self._norm(username), email=email)
|
||||||
|
|
||||||
|
def complete_setup(self, username: str, email: str, new_password: str) -> dict[str, Any]:
|
||||||
|
"""First-time admin setup: set email + password, clear must_setup."""
|
||||||
|
if not new_password or len(new_password) < 4:
|
||||||
|
raise AuthError("password must be at least 4 characters")
|
||||||
|
self.set_email(username, email)
|
||||||
|
self.set_password(username, new_password)
|
||||||
|
return self.users.update(self._norm(username), must_setup=False)
|
||||||
|
|
||||||
# ── auth ───────────────────────────────────────────────────────────
|
# ── auth ───────────────────────────────────────────────────────────
|
||||||
def verify(self, email: str, password: str) -> dict[str, Any]:
|
def verify(self, username: str, password: str) -> dict[str, Any]:
|
||||||
user = self.get_user_or_none(email)
|
user = self.get_user_or_none(username)
|
||||||
if not user or not user.get("active", True):
|
if not user or not user.get("active", True):
|
||||||
raise AuthError("invalid credentials")
|
raise AuthError("invalid credentials")
|
||||||
if not check_password_hash(user["password_hash"], password):
|
if not check_password_hash(user["password_hash"], password):
|
||||||
@@ -106,7 +157,7 @@ class UserStore:
|
|||||||
def issue_token(self, user: dict[str, Any]) -> str:
|
def issue_token(self, user: dict[str, Any]) -> str:
|
||||||
now = datetime.datetime.now(datetime.timezone.utc)
|
now = datetime.datetime.now(datetime.timezone.utc)
|
||||||
payload = {
|
payload = {
|
||||||
"sub": user["email"],
|
"sub": user.get("username") or user.get("id"),
|
||||||
"org_id": user["org_id"],
|
"org_id": user["org_id"],
|
||||||
"role": user["role"],
|
"role": user["role"],
|
||||||
"iat": now,
|
"iat": now,
|
||||||
@@ -116,9 +167,7 @@ class UserStore:
|
|||||||
|
|
||||||
def decode_token(self, token: str) -> dict[str, Any]:
|
def decode_token(self, token: str) -> dict[str, Any]:
|
||||||
try:
|
try:
|
||||||
return jwt.decode(
|
return jwt.decode(token, Config.SECRET_KEY, algorithms=[Config.JWT_ALGO])
|
||||||
token, Config.SECRET_KEY, algorithms=[Config.JWT_ALGO]
|
|
||||||
)
|
|
||||||
except jwt.PyJWTError as exc:
|
except jwt.PyJWTError as exc:
|
||||||
raise AuthError("invalid or expired token") from exc
|
raise AuthError("invalid or expired token") from exc
|
||||||
|
|
||||||
|
|||||||
@@ -11,20 +11,24 @@ from .config import Config
|
|||||||
|
|
||||||
|
|
||||||
def bootstrap_admin(users: UserStore) -> None:
|
def bootstrap_admin(users: UserStore) -> None:
|
||||||
"""Ensure a default org + super-admin exists on first run (no self-registration)."""
|
"""Ensure a default org + super-admin exists on first run (no self-registration).
|
||||||
email = "admin@salestrainer.local"
|
|
||||||
|
Default admin logs in with username `admin` / `1234`, then MUST set an email
|
||||||
|
and change the password on first login (`must_setup=True`).
|
||||||
|
"""
|
||||||
org = users.orgs.get_or_none("org-default")
|
org = users.orgs.get_or_none("org-default")
|
||||||
if org is None:
|
if org is None:
|
||||||
org = users.create_org("Default Organization", org_id="org-default")
|
org = users.create_org("Default Organization", org_id="org-default")
|
||||||
if users.get_user_or_none(email) is None:
|
if users.get_user_or_none("admin") is None:
|
||||||
users.create_user(
|
users.create_user(
|
||||||
org_id=org["id"],
|
org_id=org["id"],
|
||||||
email=email,
|
username="admin",
|
||||||
password="admin123",
|
password="1234",
|
||||||
name="Super Admin",
|
name="Super Admin",
|
||||||
role="super_admin",
|
role="super_admin",
|
||||||
|
must_setup=True,
|
||||||
)
|
)
|
||||||
print("[bootstrap] created default super-admin:", email, "/ admin123")
|
print("[bootstrap] created default super-admin: admin / 1234 (must set email + password)")
|
||||||
|
|
||||||
|
|
||||||
def create_app() -> Flask:
|
def create_app() -> Flask:
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ def main():
|
|||||||
client = app.test_client()
|
client = app.test_client()
|
||||||
|
|
||||||
# admin login
|
# admin login
|
||||||
r = client.post("/api/auth/login", json={"email": "admin@salestrainer.local", "password": "admin123"})
|
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||||
AT = r.get_json()["token"]
|
AT = r.get_json()["token"]
|
||||||
AH = {"Authorization": f"Bearer {AT}"}
|
AH = {"Authorization": f"Bearer {AT}"}
|
||||||
|
|
||||||
@@ -57,8 +57,8 @@ def main():
|
|||||||
|
|
||||||
# create a trainee
|
# create a trainee
|
||||||
client.post("/api/admin/users", json={
|
client.post("/api/admin/users", json={
|
||||||
"name": "Trainee", "email": "t@x.com", "password": "pass123", "role": "user"}, headers=AH)
|
"name": "Trainee", "username": "trainee9", "password": "pass123", "role": "user"}, headers=AH)
|
||||||
r = client.post("/api/auth/login", json={"email": "t@x.com", "password": "pass123"})
|
r = client.post("/api/auth/login", json={"username": "trainee9", "password": "pass123"})
|
||||||
UT = r.get_json()["token"]
|
UT = r.get_json()["token"]
|
||||||
UH = {"Authorization": f"Bearer {UT}"}
|
UH = {"Authorization": f"Bearer {UT}"}
|
||||||
|
|
||||||
|
|||||||
@@ -36,10 +36,10 @@ def main() -> None:
|
|||||||
assert r.status_code == 404, f"register should not exist, got {r.status_code}"
|
assert r.status_code == 404, f"register should not exist, got {r.status_code}"
|
||||||
print("[ok] no self-registration (register -> 404)")
|
print("[ok] no self-registration (register -> 404)")
|
||||||
|
|
||||||
# 3. Bootstrap super-admin login
|
# 3. Bootstrap super-admin login (username admin / 1234)
|
||||||
r = client.post(
|
r = client.post(
|
||||||
"/api/auth/login",
|
"/api/auth/login",
|
||||||
json={"email": "admin@salestrainer.local", "password": "admin123"},
|
json={"username": "admin", "password": "1234"},
|
||||||
)
|
)
|
||||||
assert r.status_code == 200, r.get_json()
|
assert r.status_code == 200, r.get_json()
|
||||||
admin_token = r.get_json()["token"]
|
admin_token = r.get_json()["token"]
|
||||||
@@ -54,14 +54,14 @@ def main() -> None:
|
|||||||
# 5. Create a regular user (admin)
|
# 5. Create a regular user (admin)
|
||||||
r = client.post(
|
r = client.post(
|
||||||
"/api/admin/users",
|
"/api/admin/users",
|
||||||
json={"name": "Trainee One", "email": "t1@x.com", "password": "pass123", "role": "user"},
|
json={"name": "Trainee One", "username": "trainee1", "password": "pass123", "role": "user"},
|
||||||
headers={"Authorization": f"Bearer {admin_token}"},
|
headers={"Authorization": f"Bearer {admin_token}"},
|
||||||
)
|
)
|
||||||
assert r.status_code == 201, r.get_json()
|
assert r.status_code == 201, r.get_json()
|
||||||
print("[ok] admin creates user")
|
print("[ok] admin creates user")
|
||||||
|
|
||||||
# 6. Trainee login + cannot access admin users list (403)
|
# 6. Trainee login + cannot access admin users list (403)
|
||||||
r = client.post("/api/auth/login", json={"email": "t1@x.com", "password": "pass123"})
|
r = client.post("/api/auth/login", json={"username": "trainee1", "password": "pass123"})
|
||||||
user_token = r.get_json()["token"]
|
user_token = r.get_json()["token"]
|
||||||
r = client.get("/api/admin/users", headers={"Authorization": f"Bearer {user_token}"})
|
r = client.get("/api/admin/users", headers={"Authorization": f"Bearer {user_token}"})
|
||||||
assert r.status_code == 403, f"trainee should be denied, got {r.status_code}"
|
assert r.status_code == 403, f"trainee should be denied, got {r.status_code}"
|
||||||
@@ -76,29 +76,29 @@ def main() -> None:
|
|||||||
# create an 'admin' actor first
|
# create an 'admin' actor first
|
||||||
client.post(
|
client.post(
|
||||||
"/api/admin/users",
|
"/api/admin/users",
|
||||||
json={"name": "Admin Two", "email": "a2@x.com", "password": "pass123", "role": "admin"},
|
json={"name": "Admin Two", "username": "admin2", "password": "pass123", "role": "admin"},
|
||||||
headers={"Authorization": f"Bearer {admin_token}"},
|
headers={"Authorization": f"Bearer {admin_token}"},
|
||||||
)
|
)
|
||||||
r = client.post(
|
r = client.post(
|
||||||
"/api/auth/login", json={"email": "a2@x.com", "password": "pass123"}
|
"/api/auth/login", json={"username": "admin2", "password": "pass123"}
|
||||||
)
|
)
|
||||||
admin2_token = r.get_json()["token"]
|
admin2_token = r.get_json()["token"]
|
||||||
r = client.post(
|
r = client.post(
|
||||||
"/api/admin/users",
|
"/api/admin/users",
|
||||||
json={"name": "Bogus Admin", "email": "ba@x.com", "password": "pass123", "role": "super_admin"},
|
json={"name": "Bogus Admin", "username": "bogus", "password": "pass123", "role": "super_admin"},
|
||||||
headers={"Authorization": f"Bearer {admin2_token}"},
|
headers={"Authorization": f"Bearer {admin2_token}"},
|
||||||
)
|
)
|
||||||
assert r.status_code == 403, f"admin should not promote, got {r.status_code}"
|
assert r.status_code == 403, f"admin should not promote, got {r.status_code}"
|
||||||
print("[ok] admin cannot grant super_admin (403)")
|
print("[ok] admin cannot grant super_admin (403)")
|
||||||
|
|
||||||
# 9. Duplicate email rejected
|
# 9. Duplicate username rejected
|
||||||
r = client.post(
|
r = client.post(
|
||||||
"/api/admin/users",
|
"/api/admin/users",
|
||||||
json={"name": "Dup", "email": "t1@x.com", "password": "pass123", "role": "user"},
|
json={"name": "Dup", "username": "trainee1", "password": "pass123", "role": "user"},
|
||||||
headers={"Authorization": f"Bearer {admin_token}"},
|
headers={"Authorization": f"Bearer {admin_token}"},
|
||||||
)
|
)
|
||||||
assert r.status_code == 400
|
assert r.status_code == 400
|
||||||
print("[ok] duplicate email rejected (400)")
|
print("[ok] duplicate username rejected (400)")
|
||||||
|
|
||||||
print("\nALL M0 TESTS PASSED")
|
print("\nALL M0 TESTS PASSED")
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ def main():
|
|||||||
|
|
||||||
# login as super-admin
|
# login as super-admin
|
||||||
r = client.post("/api/auth/login", json={
|
r = client.post("/api/auth/login", json={
|
||||||
"email": "admin@salestrainer.local", "password": "admin123"})
|
"username": "admin", "password": "1234"})
|
||||||
assert r.status_code == 200, r.get_json()
|
assert r.status_code == 200, r.get_json()
|
||||||
token = r.get_json()["token"]
|
token = r.get_json()["token"]
|
||||||
H = {"Authorization": f"Bearer {token}"}
|
H = {"Authorization": f"Bearer {token}"}
|
||||||
@@ -64,8 +64,8 @@ def main():
|
|||||||
|
|
||||||
# trainee created; can list groups but only 'ready' ones (this one is 'failed' -> hidden)
|
# trainee created; can list groups but only 'ready' ones (this one is 'failed' -> hidden)
|
||||||
client.post("/api/admin/users", json={
|
client.post("/api/admin/users", json={
|
||||||
"name": "Trainee", "email": "t@x.com", "password": "pass123", "role": "user"}, headers=H)
|
"name": "Trainee", "username": "trainee1", "password": "pass123", "role": "user"}, headers=H)
|
||||||
r = client.post("/api/auth/login", json={"email": "t@x.com", "password": "pass123"})
|
r = client.post("/api/auth/login", json={"username": "trainee1", "password": "pass123"})
|
||||||
ut = r.get_json()["token"]
|
ut = r.get_json()["token"]
|
||||||
UH = {"Authorization": f"Bearer {ut}"}
|
UH = {"Authorization": f"Bearer {ut}"}
|
||||||
r = client.get("/api/groups", headers=UH)
|
r = client.get("/api/groups", headers=UH)
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ def main():
|
|||||||
app = create_app()
|
app = create_app()
|
||||||
rules = sorted({str(rule) for rule in app.url_map.iter_rules() if str(rule).startswith("/api")})
|
rules = sorted({str(rule) for rule in app.url_map.iter_rules() if str(rule).startswith("/api")})
|
||||||
expected = [
|
expected = [
|
||||||
"/api/auth/login", "/api/auth/me",
|
"/api/auth/login", "/api/auth/me", "/api/auth/setup",
|
||||||
"/api/admin/users", "/api/admin/users/<email>",
|
"/api/admin/users", "/api/admin/users/<username>",
|
||||||
"/api/groups", "/api/groups/<gid>",
|
"/api/groups", "/api/groups/<gid>",
|
||||||
"/api/groups/<gid>/analyze", "/api/groups/<gid>/personas",
|
"/api/groups/<gid>/analyze", "/api/groups/<gid>/personas",
|
||||||
"/api/groups/<gid>/personas/<pid>", "/api/groups/<gid>/personas/<pid>",
|
"/api/groups/<gid>/personas/<pid>", "/api/groups/<gid>/personas/<pid>",
|
||||||
|
|||||||
@@ -26,8 +26,7 @@ def main():
|
|||||||
client = app.test_client()
|
client = app.test_client()
|
||||||
|
|
||||||
# admin login (org-default)
|
# admin login (org-default)
|
||||||
client.post("/api/auth/login", json={"email": "admin@salestrainer.local", "password": "admin123"})
|
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||||
r = client.post("/api/auth/login", json={"email": "admin@salestrainer.local", "password": "admin123"})
|
|
||||||
AT = r.get_json()["token"]
|
AT = r.get_json()["token"]
|
||||||
AH = {"Authorization": f"Bearer {AT}"}
|
AH = {"Authorization": f"Bearer {AT}"}
|
||||||
|
|
||||||
@@ -56,7 +55,7 @@ def main():
|
|||||||
|
|
||||||
# Cross-org IDOR: create org B + a group in org-default; org B user must be denied.
|
# Cross-org IDOR: create org B + a group in org-default; org B user must be denied.
|
||||||
client.post("/api/admin/users", json={
|
client.post("/api/admin/users", json={
|
||||||
"name": "Other Admin", "email": "b-admin@x.com", "password": "pass123", "role": "admin"},
|
"name": "Other Admin", "username": "badmin", "password": "pass123", "role": "admin"},
|
||||||
headers=AH)
|
headers=AH)
|
||||||
# Create a group as A (current default org)
|
# Create a group as A (current default org)
|
||||||
r = client.post("/api/groups", json={"product": "A product"}, headers=AH)
|
r = client.post("/api/groups", json={"product": "A product"}, headers=AH)
|
||||||
@@ -68,8 +67,8 @@ def main():
|
|||||||
# and admin cannot read a PERSONAL group belonging to a different user.
|
# and admin cannot read a PERSONAL group belonging to a different user.
|
||||||
# Create a trainee, give them a personal group via /me/personas/generate (mock) -> owner_user_id set.
|
# Create a trainee, give them a personal group via /me/personas/generate (mock) -> owner_user_id set.
|
||||||
client.post("/api/admin/users", json={
|
client.post("/api/admin/users", json={
|
||||||
"name": "Trainee T", "email": "t2@x.com", "password": "pass123", "role": "user"}, headers=AH)
|
"name": "Trainee T", "username": "trainee2", "password": "pass123", "role": "user"}, headers=AH)
|
||||||
r = client.post("/api/auth/login", json={"email": "t2@x.com", "password": "pass123"})
|
r = client.post("/api/auth/login", json={"username": "trainee2", "password": "pass123"})
|
||||||
TT = r.get_json()["token"]
|
TT = r.get_json()["token"]
|
||||||
TH = {"Authorization": f"Bearer {TT}"}
|
TH = {"Authorization": f"Bearer {TT}"}
|
||||||
# trainee creates own persona -> personal group owned by t2
|
# trainee creates own persona -> personal group owned by t2
|
||||||
@@ -81,8 +80,8 @@ def main():
|
|||||||
# The admin (different actor) must be able to access it (super_admin not needed; admin same org).
|
# The admin (different actor) must be able to access it (super_admin not needed; admin same org).
|
||||||
# For a strict IDOR test, a DIFFERENT trainee must be denied. Create t3.
|
# For a strict IDOR test, a DIFFERENT trainee must be denied. Create t3.
|
||||||
client.post("/api/admin/users", json={
|
client.post("/api/admin/users", json={
|
||||||
"name": "Trainee T3", "email": "t3@x.com", "password": "pass123", "role": "user"}, headers=AH)
|
"name": "Trainee T3", "username": "trainee3", "password": "pass123", "role": "user"}, headers=AH)
|
||||||
r = client.post("/api/auth/login", json={"email": "t3@x.com", "password": "pass123"})
|
r = client.post("/api/auth/login", json={"username": "trainee3", "password": "pass123"})
|
||||||
T3T = r.get_json()["token"]
|
T3T = r.get_json()["token"]
|
||||||
T3H = {"Authorization": f"Bearer {T3T}"}
|
T3H = {"Authorization": f"Bearer {T3T}"}
|
||||||
# t3 tries to read t2's personal group personas -> must be denied (owner check)
|
# t3 tries to read t2's personal group personas -> must be denied (owner check)
|
||||||
|
|||||||
67
backend/scripts/test_setup.py
Normal file
67
backend/scripts/test_setup.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
"""Test the first-time admin setup flow: admin/1234 -> must_setup -> set email+password."""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import warnings
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
warnings.filterwarnings("ignore", message="The HMAC key is")
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
|
||||||
|
|
||||||
|
tempdir = tempfile.mkdtemp(prefix="st_setup_")
|
||||||
|
os.environ["DATA_DIR"] = tempdir
|
||||||
|
os.environ["JWT_SECRET"] = "test-secret-key-0123456789abcdef"
|
||||||
|
|
||||||
|
from app.factory import create_app # noqa: E402
|
||||||
|
from app.config import Config # noqa: E402
|
||||||
|
|
||||||
|
Config.DATA_DIR = Path(tempdir)
|
||||||
|
Config.LLM_API_KEY = ""
|
||||||
|
Config.LLM_BASE_URL = ""
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = create_app()
|
||||||
|
client = app.test_client()
|
||||||
|
|
||||||
|
# 1. Default admin logs in with admin / 1234 and must_setup is true
|
||||||
|
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||||
|
assert r.status_code == 200, r.get_json()
|
||||||
|
assert r.get_json()["must_setup"] is True, "default admin should require setup"
|
||||||
|
token = r.get_json()["token"]
|
||||||
|
H = {"Authorization": f"Bearer {token}"}
|
||||||
|
me = client.get("/api/auth/me", headers=H).get_json()["user"]
|
||||||
|
assert me.get("must_setup") is True
|
||||||
|
print("[ok] default admin login admin/1234 -> must_setup=true")
|
||||||
|
|
||||||
|
# 2. Cannot set up with short password / bad email
|
||||||
|
r = client.post("/api/auth/setup", json={"username": "admin", "email": "bad", "password": "12"}, headers=H)
|
||||||
|
assert r.status_code == 400, r.get_json()
|
||||||
|
print("[ok] setup rejects bad email/short password")
|
||||||
|
|
||||||
|
# 3. Successful setup: email + new password, clears must_setup
|
||||||
|
r = client.post("/api/auth/setup", json={"username": "admin", "email": "admin@corp.com", "password": "NewPass!42"}, headers=H)
|
||||||
|
assert r.status_code == 200, r.get_json()
|
||||||
|
assert r.get_json()["must_setup"] is False
|
||||||
|
print("[ok] setup completes -> must_setup=false")
|
||||||
|
|
||||||
|
# 4. Old password no longer works; new one does
|
||||||
|
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||||
|
assert r.status_code == 401, "old default password should be invalid"
|
||||||
|
r = client.post("/api/auth/login", json={"username": "admin", "password": "NewPass!42"})
|
||||||
|
assert r.status_code == 200
|
||||||
|
new_token = r.get_json()["token"]
|
||||||
|
assert r.get_json()["must_setup"] is False
|
||||||
|
print("[ok] old password rejected; new password logs in")
|
||||||
|
|
||||||
|
# 5. Admin can now use the app (create user, etc.)
|
||||||
|
NH = {"Authorization": f"Bearer {new_token}"}
|
||||||
|
r = client.post("/api/admin/users", json={"name": "T1", "username": "t1", "password": "pass123", "role": "user"}, headers=NH)
|
||||||
|
assert r.status_code == 201, r.get_json()
|
||||||
|
print("[ok] admin can use the app after setup")
|
||||||
|
|
||||||
|
print("\nALL SETUP TESTS PASSED")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -33,11 +33,12 @@ async function request(method, url, body, isForm = false) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const api = {
|
export const api = {
|
||||||
login: (email, password) => request('POST', '/api/auth/login', { email, password }),
|
login: (username, password) => request('POST', '/api/auth/login', { username, password }),
|
||||||
me: () => request('GET', '/api/auth/me'),
|
me: () => request('GET', '/api/auth/me'),
|
||||||
|
setup: (b) => request('POST', '/api/auth/setup', b),
|
||||||
adminCreateUser: (b) => request('POST', '/api/admin/users', b),
|
adminCreateUser: (b) => request('POST', '/api/admin/users', b),
|
||||||
adminListUsers: () => request('GET', '/api/admin/users'),
|
adminListUsers: () => request('GET', '/api/admin/users'),
|
||||||
adminUpdateUser: (email, b) => request('PUT', `/api/admin/users/${email}`, b),
|
adminUpdateUser: (username, b) => request('PUT', `/api/admin/users/${username}`, b),
|
||||||
createGroup: (formData) => request('POST', '/api/groups', formData, true),
|
createGroup: (formData) => request('POST', '/api/groups', formData, true),
|
||||||
listGroups: () => request('GET', '/api/groups'),
|
listGroups: () => request('GET', '/api/groups'),
|
||||||
getGroup: (id) => request('GET', `/api/groups/${id}`),
|
getGroup: (id) => request('GET', `/api/groups/${id}`),
|
||||||
|
|||||||
@@ -6,8 +6,16 @@ const messages = {
|
|||||||
app: 'Sales Trainer',
|
app: 'Sales Trainer',
|
||||||
login: 'Login',
|
login: 'Login',
|
||||||
logout: 'Logout',
|
logout: 'Logout',
|
||||||
|
username: 'Username',
|
||||||
email: 'Email',
|
email: 'Email',
|
||||||
password: 'Password',
|
password: 'Password',
|
||||||
|
newPassword: 'New password',
|
||||||
|
confirmPassword: 'Confirm password',
|
||||||
|
save: 'Save',
|
||||||
|
passwordTooShort: 'Password must be at least 4 characters',
|
||||||
|
passwordMismatch: 'Passwords do not match',
|
||||||
|
setupTitle: 'Set up your account',
|
||||||
|
setupSubtitle: 'First login for ',
|
||||||
loginError: 'Invalid credentials',
|
loginError: 'Invalid credentials',
|
||||||
dashboard: 'Dashboard',
|
dashboard: 'Dashboard',
|
||||||
groups: 'Persona Groups',
|
groups: 'Persona Groups',
|
||||||
@@ -57,9 +65,17 @@ const messages = {
|
|||||||
app: 'ตัวฝึกขาย',
|
app: 'ตัวฝึกขาย',
|
||||||
login: 'เข้าสู่ระบบ',
|
login: 'เข้าสู่ระบบ',
|
||||||
logout: 'ออกจากระบบ',
|
logout: 'ออกจากระบบ',
|
||||||
|
username: 'ชื่อผู้ใช้',
|
||||||
email: 'อีเมล',
|
email: 'อีเมล',
|
||||||
password: 'รหัสผ่าน',
|
password: 'รหัสผ่าน',
|
||||||
loginError: 'อีเมลหรือรหัสผ่านไม่ถูกต้อง',
|
newPassword: 'รหัสผ่านใหม่',
|
||||||
|
confirmPassword: 'ยืนยันรหัสผ่าน',
|
||||||
|
save: 'บันทึก',
|
||||||
|
passwordTooShort: 'รหัสผ่านต้องอย่างน้อย 4 ตัวอักษร',
|
||||||
|
passwordMismatch: 'รหัสผ่านไม่ตรงกัน',
|
||||||
|
setupTitle: 'ตั้งค่าบัญชีของคุณ',
|
||||||
|
setupSubtitle: 'เข้าสู่ระบบครั้งแรกสำหรับ ',
|
||||||
|
loginError: 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง',
|
||||||
dashboard: 'หน้าหลัก',
|
dashboard: 'หน้าหลัก',
|
||||||
groups: 'กลุ่มลูกค้า (Persona)',
|
groups: 'กลุ่มลูกค้า (Persona)',
|
||||||
myTraining: 'การฝึกของฉัน',
|
myTraining: 'การฝึกของฉัน',
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { auth } from '../store/auth'
|
|||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{ path: '/login', component: () => import('../views/Login.vue'), meta: { public: true } },
|
{ path: '/login', component: () => import('../views/Login.vue'), meta: { public: true } },
|
||||||
|
{ path: '/setup', component: () => import('../views/Setup.vue') },
|
||||||
{ path: '/', component: () => import('../views/Dashboard.vue') },
|
{ path: '/', component: () => import('../views/Dashboard.vue') },
|
||||||
{ path: '/groups/:gid/personas', component: () => import('../views/Personas.vue') },
|
{ path: '/groups/:gid/personas', component: () => import('../views/Personas.vue') },
|
||||||
{ path: '/groups/:gid/chat/:pid', component: () => import('../views/Chat.vue') },
|
{ path: '/groups/:gid/chat/:pid', component: () => import('../views/Chat.vue') },
|
||||||
@@ -28,6 +29,10 @@ router.beforeEach(async (to) => {
|
|||||||
if (!auth.user) {
|
if (!auth.user) {
|
||||||
return { path: '/login', query: { redirect: to.fullPath } }
|
return { path: '/login', query: { redirect: to.fullPath } }
|
||||||
}
|
}
|
||||||
|
// Force the mandatory first-time setup (set email + change password) before use.
|
||||||
|
if (auth.mustSetup && to.path !== '/setup') {
|
||||||
|
return { path: '/setup' }
|
||||||
|
}
|
||||||
if (to.meta.admin && !auth.isAdmin) {
|
if (to.meta.admin && !auth.isAdmin) {
|
||||||
return { path: '/' }
|
return { path: '/' }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { getToken, setToken, api } from '../api'
|
|||||||
export const auth = reactive({
|
export const auth = reactive({
|
||||||
user: null,
|
user: null,
|
||||||
token: getToken(),
|
token: getToken(),
|
||||||
|
mustSetup: false,
|
||||||
get role() {
|
get role() {
|
||||||
return this.user ? this.user.role : null
|
return this.user ? this.user.role : null
|
||||||
},
|
},
|
||||||
@@ -16,23 +17,33 @@ export const auth = reactive({
|
|||||||
try {
|
try {
|
||||||
const data = await api.me()
|
const data = await api.me()
|
||||||
this.user = data.user
|
this.user = data.user
|
||||||
|
this.mustSetup = !!data.user?.must_setup
|
||||||
return this.user
|
return this.user
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.user = null
|
this.user = null
|
||||||
|
this.mustSetup = false
|
||||||
setToken(null)
|
setToken(null)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async login(email, password) {
|
async login(username, password) {
|
||||||
const data = await api.login(email, password)
|
const data = await api.login(username, password)
|
||||||
this.token = data.token
|
this.token = data.token
|
||||||
setToken(data.token)
|
setToken(data.token)
|
||||||
this.user = data.user
|
this.user = data.user
|
||||||
|
this.mustSetup = !!data.must_setup
|
||||||
|
return data.user
|
||||||
|
},
|
||||||
|
async finishSetup(email, password) {
|
||||||
|
const data = await api.setup({ username: this.user.username || this.user.id, email, password })
|
||||||
|
this.user = data.user
|
||||||
|
this.mustSetup = false
|
||||||
return data.user
|
return data.user
|
||||||
},
|
},
|
||||||
logout() {
|
logout() {
|
||||||
this.user = null
|
this.user = null
|
||||||
this.token = null
|
this.token = null
|
||||||
|
this.mustSetup = false
|
||||||
setToken(null)
|
setToken(null)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
<div class="card login-card">
|
<div class="card login-card">
|
||||||
<h1>🎯 {{ i18n.t('app') }}</h1>
|
<h1>🎯 {{ i18n.t('app') }}</h1>
|
||||||
<p class="muted" style="margin-top:-8px">Sales training simulator</p>
|
<p class="muted" style="margin-top:-8px">Sales training simulator</p>
|
||||||
<label>{{ i18n.t('email') }}</label>
|
<label>{{ i18n.t('username') }}</label>
|
||||||
<input v-model="email" type="email" autocomplete="username" @keyup.enter="submit" />
|
<input v-model="username" type="text" autocomplete="username" @keyup.enter="submit" />
|
||||||
<label>{{ i18n.t('password') }}</label>
|
<label>{{ i18n.t('password') }}</label>
|
||||||
<div class="pw-wrap">
|
<div class="pw-wrap">
|
||||||
<input v-model="password" :type="showPw ? 'text' : 'password'" autocomplete="current-password" @keyup.enter="submit" />
|
<input v-model="password" :type="showPw ? 'text' : 'password'" autocomplete="current-password" @keyup.enter="submit" />
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="error" role="alert" v-if="error">{{ error }}</div>
|
<div class="error" role="alert" v-if="error">{{ error }}</div>
|
||||||
<button class="primary" style="width:100%;margin-top:16px" :disabled="loading || !email || !password" @click="submit">
|
<button class="primary" style="width:100%;margin-top:16px" :disabled="loading || !username || !password" @click="submit">
|
||||||
<span v-if="loading" class="spinner"></span>
|
<span v-if="loading" class="spinner"></span>
|
||||||
<span v-else>{{ i18n.t('login') }}</span>
|
<span v-else>{{ i18n.t('login') }}</span>
|
||||||
</button>
|
</button>
|
||||||
@@ -29,7 +29,7 @@ import { i18n } from '../i18n'
|
|||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const email = ref('')
|
const username = ref('')
|
||||||
const password = ref('')
|
const password = ref('')
|
||||||
const showPw = ref(false)
|
const showPw = ref(false)
|
||||||
const error = ref('')
|
const error = ref('')
|
||||||
@@ -39,8 +39,13 @@ async function submit() {
|
|||||||
error.value = ''
|
error.value = ''
|
||||||
loading.value = true
|
loading.value = true
|
||||||
try {
|
try {
|
||||||
await auth.login(email.value, password.value)
|
await auth.login(username.value.trim(), password.value)
|
||||||
router.push(route.query.redirect || '/')
|
// First-time admin setup is mandatory before using the app.
|
||||||
|
if (auth.mustSetup) {
|
||||||
|
router.push({ path: '/setup' })
|
||||||
|
} else {
|
||||||
|
router.push(route.query.redirect || '/')
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error.value = i18n.t('loginError')
|
error.value = i18n.t('loginError')
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
65
frontend/src/views/Setup.vue
Normal file
65
frontend/src/views/Setup.vue
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<template>
|
||||||
|
<div class="setup-wrap">
|
||||||
|
<div class="card setup-card">
|
||||||
|
<h1>🔐 {{ i18n.t('setupTitle') }}</h1>
|
||||||
|
<p class="muted">{{ i18n.t('setupSubtitle') }} <strong>{{ auth.user?.name || auth.user?.username }}</strong></p>
|
||||||
|
|
||||||
|
<label>{{ i18n.t('email') }}</label>
|
||||||
|
<input v-model="email" type="email" autocomplete="email" @keyup.enter="submit" />
|
||||||
|
|
||||||
|
<label>{{ i18n.t('newPassword') }}</label>
|
||||||
|
<input v-model="password" type="password" autocomplete="new-password" @keyup.enter="submit" />
|
||||||
|
|
||||||
|
<label>{{ i18n.t('confirmPassword') }}</label>
|
||||||
|
<input v-model="confirm" type="password" autocomplete="new-password" @keyup.enter="submit" />
|
||||||
|
|
||||||
|
<div class="error" role="alert" v-if="error">{{ error }}</div>
|
||||||
|
|
||||||
|
<button class="primary" style="width:100%;margin-top:16px" :disabled="busy || !email || !password || password !== confirm" @click="submit">
|
||||||
|
<span v-if="busy" class="spinner"></span>
|
||||||
|
<span v-else>{{ i18n.t('save') }}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { ref } from 'vue'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
import { auth } from '../store/auth'
|
||||||
|
import { i18n } from '../i18n'
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
|
const email = ref('')
|
||||||
|
const password = ref('')
|
||||||
|
const confirm = ref('')
|
||||||
|
const error = ref('')
|
||||||
|
const busy = ref(false)
|
||||||
|
|
||||||
|
async function submit() {
|
||||||
|
error.value = ''
|
||||||
|
if (password.value.length < 4) {
|
||||||
|
error.value = i18n.t('passwordTooShort')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (password.value !== confirm.value) {
|
||||||
|
error.value = i18n.t('passwordMismatch')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
busy.value = true
|
||||||
|
try {
|
||||||
|
await auth.finishSetup(email.value.trim(), password.value)
|
||||||
|
router.push('/')
|
||||||
|
} catch (e) {
|
||||||
|
error.value = e.message
|
||||||
|
} finally {
|
||||||
|
busy.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.setup-wrap { display: flex; justify-content: center; padding-top: 8vh; }
|
||||||
|
.setup-card { width: 380px; }
|
||||||
|
h1 { margin-top: 0; }
|
||||||
|
</style>
|
||||||
Reference in New Issue
Block a user