
- Created User model and schemas - Implemented secure password hashing with bcrypt - Added JWT token-based authentication - Created user registration and login endpoints - Added authentication to todo routes - Updated todos to be associated with users - Created migration script for the user table - Updated documentation with auth information
60 lines
1.3 KiB
Python
60 lines
1.3 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from pydantic import BaseModel, EmailStr, Field, field_validator
|
|
|
|
|
|
class UserBase(BaseModel):
|
|
"""Base schema for User objects"""
|
|
|
|
email: EmailStr
|
|
username: str = Field(..., min_length=3, max_length=50)
|
|
is_active: bool = True
|
|
is_superuser: bool = False
|
|
|
|
|
|
class UserCreate(UserBase):
|
|
"""Schema for creating a new user"""
|
|
|
|
password: str = Field(..., min_length=8, max_length=100)
|
|
password_confirm: str
|
|
|
|
@field_validator("password_confirm")
|
|
def passwords_match(cls, v, values):
|
|
if "password" in values.data and v != values.data["password"]:
|
|
raise ValueError("Passwords do not match")
|
|
return v
|
|
|
|
|
|
class UserUpdate(BaseModel):
|
|
"""Schema for updating a user"""
|
|
|
|
email: Optional[EmailStr] = None
|
|
username: Optional[str] = None
|
|
password: Optional[str] = None
|
|
is_active: Optional[bool] = None
|
|
is_superuser: Optional[bool] = None
|
|
|
|
|
|
class UserInDBBase(UserBase):
|
|
"""Base schema for User objects from the database"""
|
|
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class User(UserInDBBase):
|
|
"""Schema for User objects returned from the API"""
|
|
|
|
pass
|
|
|
|
|
|
class UserInDB(UserInDBBase):
|
|
"""Schema for User objects with hashed_password"""
|
|
|
|
hashed_password: str
|