geo/backend/app/schemas/auth.py

86 lines
2.0 KiB
Python

import uuid
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, EmailStr, Field
class UserRegister(BaseModel):
email: EmailStr
password: str = Field(min_length=8)
name: str = Field(min_length=1, max_length=100)
class UserLogin(BaseModel):
email: EmailStr
password: str
class ForgotPasswordRequest(BaseModel):
email: EmailStr
class ResetPasswordRequest(BaseModel):
token: str
new_password: str = Field(..., min_length=8)
class VerifyEmailRequest(BaseModel):
email: EmailStr
code: str = Field(..., min_length=6, max_length=6)
class ChangePasswordRequest(BaseModel):
old_password: str
new_password: str = Field(..., min_length=8)
class UpdateProfileRequest(BaseModel):
name: Optional[str] = None
avatar_url: Optional[str] = None
class UserResponse(BaseModel):
id: uuid.UUID | str
email: str
name: str | None = None
is_active: bool = True
email_verified: bool = False
avatar_url: str | None = None
created_at: datetime | None = None
model_config = {"from_attributes": True}
@classmethod
def from_user(cls, user) -> "UserResponse":
avatar = user.avatar_url
# 防止 mock 对象或非字符串值
if avatar is not None and not isinstance(avatar, str):
avatar = None
return cls(
id=str(user.id) if not isinstance(user.id, str) else user.id,
email=user.email,
name=user.name,
is_active=user.is_active,
email_verified=user.email_verified,
avatar_url=avatar,
created_at=user.createdAt if hasattr(user, "createdAt") else None,
)
class TokenResponse(BaseModel):
access_token: str
token_type: str
refresh_token: str
user: UserResponse
class RefreshTokenRequest(BaseModel):
refresh_token: str
class AccessTokenResponse(BaseModel):
access_token: str
token_type: str
refresh_token: str