feat(auth): авторизация Pleily — JWT + device_id + auth_identities
Backend: - POST /auth/device — анонимный вход по device_id (UUID) - GET /auth/me — текущий юзер - DELETE /me — удаление аккаунта (Apple требует) БД: - users.source TEXT DEFAULT 'telegram' - users.device_id UUID UNIQUE - users.apple_id TEXT UNIQUE - app_user_id_seq (START -1 INCREMENT -1) - auth_identities (provider, provider_uid, user_id) Auth: - JWT через PyJWT, APP_JWT_SECRET из .env - Authorization: Bearer <token> в require_auth - Rate limit /auth/device (5/60) Отрицательные telegram_id для Pleily-юзеров.
This commit is contained in:
+89
@@ -1246,6 +1246,95 @@ async def init_db():
|
||||
""")
|
||||
logger.info("Table word_usage initialized")
|
||||
|
||||
# ============ 🔧 НОВОЕ (07.10.2026): pleily_auth —
|
||||
# поддержка авторизации мобильного приложения Pleily.
|
||||
# - source: откуда пришёл юзер ('telegram' | 'pleily_device' | 'pleily_apple')
|
||||
# - device_id: UUID устройства (анонимный вход)
|
||||
# - apple_id: Apple ID (потом, для привязки прогресса)
|
||||
# - app_user_id_seq: sequence для отрицательных user_id (Pleily-юзеры)
|
||||
# - auth_identities: связка provider+provider_uid → user_id
|
||||
# ============================================================
|
||||
|
||||
# source
|
||||
col_exists = await conn.fetchval("""
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'users' AND column_name = 'source'
|
||||
""")
|
||||
if not col_exists:
|
||||
await conn.execute("""
|
||||
ALTER TABLE users ADD COLUMN source TEXT DEFAULT 'telegram'
|
||||
""")
|
||||
await conn.execute("""
|
||||
UPDATE users SET source = 'telegram' WHERE source IS NULL
|
||||
""")
|
||||
logger.info("Added source column to users (Pleily)")
|
||||
|
||||
# device_id
|
||||
col_exists = await conn.fetchval("""
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'users' AND column_name = 'device_id'
|
||||
""")
|
||||
if not col_exists:
|
||||
await conn.execute("""
|
||||
ALTER TABLE users ADD COLUMN device_id UUID
|
||||
""")
|
||||
await conn.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_device_id
|
||||
ON users(device_id) WHERE device_id IS NOT NULL
|
||||
""")
|
||||
logger.info("Added device_id column to users (Pleily)")
|
||||
|
||||
# apple_id
|
||||
col_exists = await conn.fetchval("""
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'users' AND column_name = 'apple_id'
|
||||
""")
|
||||
if not col_exists:
|
||||
await conn.execute("""
|
||||
ALTER TABLE users ADD COLUMN apple_id TEXT
|
||||
""")
|
||||
await conn.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_apple_id
|
||||
ON users(apple_id) WHERE apple_id IS NOT NULL
|
||||
""")
|
||||
logger.info("Added apple_id column to users (Pleily)")
|
||||
|
||||
# app_user_id_seq — sequence для отрицательных user_id
|
||||
await conn.execute("""
|
||||
CREATE SEQUENCE IF NOT EXISTS app_user_id_seq
|
||||
START -1
|
||||
INCREMENT -1
|
||||
MINVALUE -9223372036854775808
|
||||
MAXVALUE -1
|
||||
""")
|
||||
logger.info("Table app_user_id_seq initialized (Pleily)")
|
||||
|
||||
# auth_identities — связка provider+provider_uid → user_id
|
||||
await conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS auth_identities (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(telegram_id) ON DELETE CASCADE,
|
||||
provider TEXT NOT NULL,
|
||||
provider_uid TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
UNIQUE(provider, provider_uid)
|
||||
)
|
||||
""")
|
||||
await conn.execute("""
|
||||
CREATE INDEX IF NOT EXISTS idx_auth_identities_user
|
||||
ON auth_identities(user_id)
|
||||
""")
|
||||
|
||||
# Backfill: для существующих telegram-юзеров создаём identity
|
||||
await conn.execute("""
|
||||
INSERT INTO auth_identities (user_id, provider, provider_uid)
|
||||
SELECT telegram_id, 'telegram', telegram_id::text
|
||||
FROM users
|
||||
WHERE telegram_id > 0
|
||||
ON CONFLICT DO NOTHING
|
||||
""")
|
||||
logger.info("Table auth_identities initialized (Pleily)")
|
||||
|
||||
await conn.execute("CREATE INDEX IF NOT EXISTS idx_sentences_level_number ON sentences(level, sentence_number)")
|
||||
# Оставляем score-индексы (не дубли)
|
||||
await conn.execute("CREATE INDEX IF NOT EXISTS idx_user_progress_score ON user_progress(score DESC)")
|
||||
|
||||
Reference in New Issue
Block a user