add registration page with nickname/phone fields and fix lateinit crashes
This commit is contained in:
@@ -165,7 +165,8 @@ def create_app(
|
||||
user = video_repository.create_user(
|
||||
username=payload.username,
|
||||
password=payload.password,
|
||||
nickname=payload.username,
|
||||
nickname=payload.nickname or payload.username,
|
||||
phone=payload.phone,
|
||||
)
|
||||
if user is None:
|
||||
raise HTTPException(status_code=409, detail="Username is already taken.")
|
||||
|
||||
@@ -138,12 +138,15 @@ class AuthRequest(BaseModel):
|
||||
|
||||
username: str = Field(min_length=3, max_length=50, pattern=r"^[A-Za-z0-9_.-]+$")
|
||||
password: str = Field(min_length=8, max_length=128)
|
||||
nickname: Optional[str] = Field(default=None, max_length=80)
|
||||
phone: Optional[str] = Field(default=None, max_length=20)
|
||||
|
||||
|
||||
class UserPublic(BaseModel):
|
||||
id: str
|
||||
username: str
|
||||
nickname: str
|
||||
phone: Optional[str] = None
|
||||
created_at: datetime
|
||||
|
||||
|
||||
|
||||
@@ -75,6 +75,7 @@ class VideoRepository:
|
||||
id TEXT PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
||||
nickname TEXT NOT NULL,
|
||||
phone TEXT,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
@@ -147,6 +148,14 @@ class VideoRepository:
|
||||
}
|
||||
if "user_id" not in attempt_columns:
|
||||
connection.execute("ALTER TABLE attempts ADD COLUMN user_id TEXT NOT NULL DEFAULT ''")
|
||||
|
||||
with self._connect() as connection:
|
||||
user_columns = {
|
||||
row["name"]
|
||||
for row in connection.execute("PRAGMA table_info(users)")
|
||||
}
|
||||
if "phone" not in user_columns:
|
||||
connection.execute("ALTER TABLE users ADD COLUMN phone TEXT")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS idx_attempts_user ON attempts(user_id)")
|
||||
|
||||
with self._connect() as connection:
|
||||
@@ -750,20 +759,28 @@ class VideoRepository:
|
||||
),
|
||||
)
|
||||
|
||||
def create_user(self, *, username: str, password: str, nickname: Optional[str]) -> Optional[Dict[str, Any]]:
|
||||
def create_user(
|
||||
self,
|
||||
*,
|
||||
username: str,
|
||||
password: str,
|
||||
nickname: Optional[str] = None,
|
||||
phone: Optional[str] = None,
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
user_id = uuid.uuid4().hex
|
||||
now = utc_now()
|
||||
try:
|
||||
with self._connect() as connection:
|
||||
row = connection.execute(
|
||||
"""
|
||||
INSERT INTO users (id, username, nickname, password_hash, created_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
INSERT INTO users (id, username, nickname, phone, password_hash, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
user_id,
|
||||
username.strip().lower(),
|
||||
(nickname or username).strip()[:80],
|
||||
(phone or "").strip()[:20] or None,
|
||||
hash_password(password),
|
||||
now,
|
||||
),
|
||||
@@ -783,7 +800,7 @@ class VideoRepository:
|
||||
def get_user_by_id(self, user_id: str) -> Optional[Dict[str, Any]]:
|
||||
with self._connect() as connection:
|
||||
row = connection.execute(
|
||||
"SELECT id, username, nickname, created_at FROM users WHERE id = ?",
|
||||
"SELECT id, username, nickname, phone, created_at FROM users WHERE id = ?",
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
Reference in New Issue
Block a user