refactor: Phase 2 - rename routes to RESTful style

This commit is contained in:
qzl
2026-02-26 13:41:32 +08:00
parent 4b707c7da1
commit 3cab7b03f7
6 changed files with 143 additions and 107 deletions
+23 -25
View File
@@ -9,16 +9,15 @@ from supabase import AuthError, create_client
from core.config.settings import SupabaseSettings, config
from core.logging import get_logger
from v1.auth.schemas import (
AuthResendCodeResponse,
AuthSignupStartResponse,
AuthTokenResponse,
AuthUser,
AuthUserByEmailResponse,
LoginRequest,
RefreshRequest,
SignupResendRequest,
SignupStartRequest,
SignupVerifyRequest,
SessionCreateRequest,
SessionRefreshRequest,
SessionResponse,
UserByEmailResponse,
VerificationCreateRequest,
VerificationCreateResponse,
VerificationResendRequest,
VerificationVerifyRequest,
)
from v1.auth.service import AuthServiceGateway
@@ -34,9 +33,9 @@ class SupabaseAuthGateway(AuthServiceGateway):
self._client = create_client(settings.url, settings.anon_key)
self._admin_client = create_client(settings.url, settings.service_role_key)
async def signup_start(
self, request: SignupStartRequest
) -> AuthSignupStartResponse:
async def create_verification(
self, request: VerificationCreateRequest
) -> VerificationCreateResponse:
payload: dict[str, Any] = {
"email": request.email,
"password": request.password,
@@ -47,14 +46,16 @@ class SupabaseAuthGateway(AuthServiceGateway):
try:
sign_up = cast(Any, self._client.auth.sign_up)
await asyncio.to_thread(sign_up, payload)
return AuthSignupStartResponse(email=request.email)
return VerificationCreateResponse(email=request.email)
except AuthError as exc:
logger.warning("Signup failed", error_type=type(exc).__name__)
raise HTTPException(
status_code=422, detail="Invalid signup request"
) from exc
async def signup_verify(self, request: SignupVerifyRequest) -> AuthTokenResponse:
async def verify_verification(
self, request: VerificationVerifyRequest
) -> SessionResponse:
payload: dict[str, Any] = {
"type": "signup",
"email": request.email,
@@ -70,18 +71,15 @@ class SupabaseAuthGateway(AuthServiceGateway):
status_code=401, detail="Invalid verification code"
) from exc
async def signup_resend(
self, request: SignupResendRequest
) -> AuthResendCodeResponse:
async def resend_verification(self, request: VerificationResendRequest) -> None:
payload: dict[str, Any] = {"type": "signup", "email": request.email}
try:
resend = cast(Any, self._client.auth.resend)
await asyncio.to_thread(resend, payload)
except AuthError as exc:
logger.warning("Signup resend failed", error_type=type(exc).__name__)
return AuthResendCodeResponse()
async def login(self, request: LoginRequest) -> AuthTokenResponse:
async def create_session(self, request: SessionCreateRequest) -> SessionResponse:
payload: dict[str, Any] = {"email": request.email, "password": request.password}
try:
sign_in = cast(Any, self._client.auth.sign_in_with_password)
@@ -91,7 +89,7 @@ class SupabaseAuthGateway(AuthServiceGateway):
logger.warning("Login failed", error_type=type(exc).__name__)
raise HTTPException(status_code=401, detail="Invalid credentials") from exc
async def refresh(self, request: RefreshRequest) -> AuthTokenResponse:
async def refresh_session(self, request: SessionRefreshRequest) -> SessionResponse:
try:
response = await asyncio.to_thread(
self._client.auth.refresh_session,
@@ -104,7 +102,7 @@ class SupabaseAuthGateway(AuthServiceGateway):
status_code=401, detail="Invalid refresh token"
) from exc
async def logout(self, refresh_token: str | None) -> None:
async def delete_session(self, refresh_token: str | None) -> None:
if not refresh_token:
raise HTTPException(status_code=401, detail="Missing refresh token")
try:
@@ -127,7 +125,7 @@ class SupabaseAuthGateway(AuthServiceGateway):
status_code=401, detail="Invalid refresh token"
) from exc
async def get_user_by_email(self, email: str) -> AuthUserByEmailResponse:
async def get_user_by_email(self, email: str) -> UserByEmailResponse:
users = await asyncio.to_thread(_list_auth_users, self._admin_client)
normalized_email = email.lower()
user = next(
@@ -141,7 +139,7 @@ class SupabaseAuthGateway(AuthServiceGateway):
if user is None:
raise HTTPException(status_code=404, detail="User not found")
return AuthUserByEmailResponse(
return UserByEmailResponse(
id=str(getattr(user, "id", "")),
email=str(getattr(user, "email", "")),
created_at=str(getattr(user, "created_at", "")),
@@ -153,7 +151,7 @@ class SupabaseAuthGateway(AuthServiceGateway):
)
def _map_auth_response(response: object, failure_message: str) -> AuthTokenResponse:
def _map_auth_response(response: object, failure_message: str) -> SessionResponse:
session = getattr(response, "session", None)
user = getattr(response, "user", None)
if session is None or user is None:
@@ -164,7 +162,7 @@ def _map_auth_response(response: object, failure_message: str) -> AuthTokenRespo
raise HTTPException(status_code=401, detail=failure_message)
auth_user = AuthUser(id=str(user.id), email=str(email))
return AuthTokenResponse(
return SessionResponse(
access_token=str(session.access_token),
refresh_token=str(session.refresh_token),
expires_in=int(session.expires_in or 0),