feat: implement SQLite database management and Supabase integration for user authentication and points synchronization
This commit is contained in:
@@ -366,6 +366,18 @@ class DBManager:
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_telegram_bind_tokens_expires ON telegram_bind_tokens(expires_at)"
|
||||
)
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS web_telegram_bind_tokens (
|
||||
token TEXT PRIMARY KEY,
|
||||
supabase_user_id TEXT NOT NULL,
|
||||
supabase_email TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP NOT NULL
|
||||
)
|
||||
""")
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_web_telegram_bind_tokens_expires ON web_telegram_bind_tokens(expires_at)"
|
||||
)
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS airport_obs_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -1452,6 +1464,69 @@ class DBManager:
|
||||
conn.commit()
|
||||
return int(row["telegram_id"])
|
||||
|
||||
def create_web_bind_token(
|
||||
self,
|
||||
supabase_user_id: str,
|
||||
supabase_email: str = "",
|
||||
ttl_minutes: int = 10,
|
||||
) -> str:
|
||||
normalized_uid = str(supabase_user_id or "").strip().lower()
|
||||
if not normalized_uid:
|
||||
raise ValueError("supabase_user_id is required")
|
||||
token = secrets.token_urlsafe(16)
|
||||
now = datetime.now()
|
||||
expires_at = now + timedelta(minutes=max(1, int(ttl_minutes)))
|
||||
with self._get_connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
DELETE FROM web_telegram_bind_tokens
|
||||
WHERE supabase_user_id = ? OR expires_at < ?
|
||||
""",
|
||||
(normalized_uid, now.isoformat()),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO web_telegram_bind_tokens (
|
||||
token, supabase_user_id, supabase_email, expires_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(token, normalized_uid, str(supabase_email or "").strip(), expires_at.isoformat()),
|
||||
)
|
||||
conn.commit()
|
||||
return token
|
||||
|
||||
def consume_web_bind_token(self, token: str) -> Optional[Dict[str, str]]:
|
||||
token = str(token or "").strip()
|
||||
if not token:
|
||||
return None
|
||||
now = datetime.now()
|
||||
with self._get_connection() as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT supabase_user_id, supabase_email, expires_at
|
||||
FROM web_telegram_bind_tokens
|
||||
WHERE token = ?
|
||||
LIMIT 1
|
||||
""",
|
||||
(token,),
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
try:
|
||||
expires_at = datetime.fromisoformat(row["expires_at"])
|
||||
except Exception:
|
||||
expires_at = now
|
||||
conn.execute("DELETE FROM web_telegram_bind_tokens WHERE token = ?", (token,))
|
||||
conn.commit()
|
||||
if now > expires_at:
|
||||
return None
|
||||
return {
|
||||
"supabase_user_id": str(row["supabase_user_id"] or "").strip().lower(),
|
||||
"supabase_email": str(row["supabase_email"] or "").strip(),
|
||||
}
|
||||
|
||||
def add_message_activity(
|
||||
self,
|
||||
telegram_id: int,
|
||||
|
||||
Reference in New Issue
Block a user