Files
life-echo/api/app/features/auth/repo.py
Sully 105b50a277 merge dark mode and google OAuth (#35)
* feat(api): implement Google OAuth login and user management

- Added Google OpenID Connect login functionality, allowing users to authenticate using their Google accounts.
- Created new endpoints for Google login, including user registration and linking existing accounts.
- Introduced Google token verification logic and error handling for authentication failures.
- Updated environment configuration to include Google OAuth client IDs and verification settings.
- Enhanced user model to support OpenID and linked Google accounts.

This feature improves user experience by enabling seamless sign-in with Google, while maintaining security and integrity of user data.

* fix(auth): wire staging Google token verifier

* chore(deps): update expo to version 55.0.6 and adjust @expo/env dependency in pnpm-lock.yaml

* chore(deps): update Babel dependencies to version 7.29.7 in package-lock.json

* feat(auth): enhance phone login for China users

- Updated phone login functionality to support only mainland China (+86) mobile numbers.
- Added user prompts and descriptions for phone login, including confirmation and cancellation options.
- Adjusted translations for both English and Chinese to reflect the new phone login requirements.
- Updated Google OAuth client IDs in configuration files for production and staging environments.

* chore(deps): add peer flag to use-sync-external-store in package-lock.json

* chore(deps): add @emnapi/core and @emnapi/runtime to package-lock.json

* fix(app-expo): align Android native dependencies

* fix(app-expo): normalize lockfile for npm 10

* fix(config): update environment variable handling to use static access

- Introduced a static mapping for public environment variables to ensure proper access during the release bundle.
- Updated the `requirePublicEnv` and `optionalPublicEnv` functions to reference the new `PUBLIC_ENV` object instead of directly accessing `process.env`.
- Added comments to clarify the necessity of static access for certain environment variables.

* feat(app-expo): dark mode, FAQ i18n, eval ASR, and theme cleanup (#34)

* feat(app-expo): dark mode, FAQ i18n, version CI, and theme cleanup

Implement light/dark scene colors across chat, reading, and headers; remove
default/brand theme picker and ThemeVariablesProvider. Localize FAQ in-app,
fix dark-mode text visibility, and remove the unused /api/faqs endpoint.
Align About/version with Expo config and inject APP_VERSION in CI builds.

Also includes phone E164 auth/SMS updates, eval ASR page, and related API work.

* revert: remove phone E.164 changes from dark-mode branch

These auth/SMS internationalization updates were accidentally bundled into
the dark-mode commit; restore 11-digit CN phone flow and drop related API,
migration, and Expo UI work from this branch.

* fix: address PR review issues for dark mode and eval ASR

Use light foreground colors for sepia reading in dark mode, fix chat send
button contrast, stream-limit eval ASR uploads, restore LiveTester phone
validation, and remove unused AudioSegmenter code.

* fix(app-expo): improve chat send button contrast in light and dark mode

Add dedicated send button colors (accent fill in dark, primary fill in
light), use RNText to avoid NativeWind overrides, and restore dark labels
in light mode for readable composer actions.

---------

Co-authored-by: Kevin <kevin@brighteng.org>

---------

Co-authored-by: penghanyuan <penghanyuan@gmail.com>
Co-authored-by: Kevin <kevin@brighteng.org>
2026-06-09 11:14:36 +08:00

178 lines
5.2 KiB
Python

from datetime import datetime
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db import utc_now
from app.features.auth.models import RefreshToken, SmsVerificationCode
from app.features.user.models import User
async def get_user_by_phone(phone: str, db: AsyncSession) -> User | None:
stmt = select(User).where(User.phone == phone)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def get_user_by_id(user_id: str, db: AsyncSession) -> User | None:
return await db.get(User, user_id)
async def get_user_by_email(email: str, db: AsyncSession) -> User | None:
stmt = select(User).where(User.email == email)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def get_user_by_openid(openid: str, db: AsyncSession) -> User | None:
stmt = select(User).where(User.openid == openid)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def get_refresh_token_by_token(
token_str: str, db: AsyncSession
) -> RefreshToken | None:
stmt = select(RefreshToken).where(RefreshToken.token == token_str)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def get_refresh_token_by_id(
token_id: str, db: AsyncSession
) -> RefreshToken | None:
return await db.get(RefreshToken, token_id)
async def link_refresh_rotation(
old_token_id: str,
new_token_id: str,
rotated_at: datetime,
db: AsyncSession,
) -> None:
"""Record lineage on the consumed refresh token row."""
stmt = (
update(RefreshToken)
.where(RefreshToken.id == old_token_id)
.values(replaced_by_token_id=new_token_id, rotated_at=rotated_at)
)
await db.execute(stmt)
async def get_active_tokens_for_user(
user_id: str, db: AsyncSession
) -> list[RefreshToken]:
stmt = select(RefreshToken).where(
RefreshToken.user_id == user_id,
RefreshToken.is_revoked == False, # noqa: E712
)
result = await db.execute(stmt)
return list(result.scalars().all())
async def create_user(user: User, db: AsyncSession) -> None:
db.add(user)
async def create_refresh_token(token: RefreshToken, db: AsyncSession) -> None:
db.add(token)
async def try_consume_refresh_token(
token_str: str, db: AsyncSession
) -> RefreshToken | None:
"""Atomically revoke a valid refresh token; returns row or None."""
now = utc_now()
stmt = (
update(RefreshToken)
.where(
RefreshToken.token == token_str,
RefreshToken.is_revoked.is_(False),
RefreshToken.expires_at > now,
)
.values(is_revoked=True)
.returning(RefreshToken)
)
result = await db.execute(stmt)
return result.scalar_one_or_none()
# ── SMS verification code ─────────────────────────────────────
async def create_verification_code(
record: SmsVerificationCode, db: AsyncSession
) -> None:
db.add(record)
async def get_recent_code_for_rate_limit(
phone: str, db: AsyncSession
) -> SmsVerificationCode | None:
"""Latest verification code record for the phone (for rate limit check)."""
stmt = (
select(SmsVerificationCode)
.where(
SmsVerificationCode.phone == phone,
SmsVerificationCode.is_expired.is_(False),
)
.order_by(SmsVerificationCode.created_at.desc())
.limit(1)
)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def get_latest_unused_code(
phone: str, purpose: str, db: AsyncSession
) -> SmsVerificationCode | None:
"""Latest unused, non-expired verification code for phone+purpose."""
stmt = (
select(SmsVerificationCode)
.where(
SmsVerificationCode.phone == phone,
SmsVerificationCode.purpose == purpose,
SmsVerificationCode.is_used.is_(False),
SmsVerificationCode.is_expired.is_(False),
)
.order_by(SmsVerificationCode.created_at.desc())
.limit(1)
)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def mark_verification_code_expired(code_id: str, db: AsyncSession) -> None:
"""Mark a verification code expired (e.g. after SMS provider failure)."""
stmt = (
update(SmsVerificationCode)
.where(SmsVerificationCode.id == code_id)
.values(is_expired=True)
)
await db.execute(stmt)
async def try_consume_verification_code(
phone: str,
code: str,
purpose: str,
db: AsyncSession,
) -> SmsVerificationCode | None:
"""Atomically mark matching unused code as used; returns row or None."""
now = utc_now()
stmt = (
update(SmsVerificationCode)
.where(
SmsVerificationCode.phone == phone,
SmsVerificationCode.purpose == purpose,
SmsVerificationCode.code == code,
SmsVerificationCode.is_used.is_(False),
SmsVerificationCode.is_expired.is_(False),
SmsVerificationCode.expires_at > now,
)
.values(is_used=True, verified_at=now)
.returning(SmsVerificationCode)
)
result = await db.execute(stmt)
return result.scalar_one_or_none()