mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-11-01 01:48:46 +08:00
Start foundations
This commit is contained in:
0
fastapi_users/__init__.py
Normal file
0
fastapi_users/__init__.py
Normal file
30
fastapi_users/models.py
Normal file
30
fastapi_users/models.py
Normal file
@ -0,0 +1,30 @@
|
||||
import uuid
|
||||
from pydantic import BaseModel
|
||||
from pydantic.types import EmailStr
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
id: str = uuid.uuid4
|
||||
email: Optional[EmailStr] = None
|
||||
is_active: Optional[bool] = True
|
||||
is_superuser: Optional[bool] = False
|
||||
first_name: Optional[str] = None
|
||||
last_name: Optional[str] = None
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
email: EmailStr
|
||||
password: str
|
||||
|
||||
|
||||
class UserUpdate(UserBase):
|
||||
pass
|
||||
|
||||
|
||||
class UserDB(UserBase):
|
||||
hashed_password: str
|
||||
|
||||
|
||||
class User(UserBase):
|
||||
pass
|
||||
11
fastapi_users/password.py
Normal file
11
fastapi_users/password.py
Normal file
@ -0,0 +1,11 @@
|
||||
from passlib.context import CryptContext
|
||||
|
||||
pwd_context = CryptContext(schemes=['bcrypt'], deprecated='auto')
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str):
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password: str):
|
||||
return pwd_context.hash(password)
|
||||
12
fastapi_users/router.py
Normal file
12
fastapi_users/router.py
Normal file
@ -0,0 +1,12 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .models import UserCreate, UserDB
|
||||
from .password import get_password_hash
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post('/register')
|
||||
async def register(user: UserCreate):
|
||||
hashed_password = get_password_hash(user.password)
|
||||
return UserDB(**user.dict(), hashed_password=hashed_password)
|
||||
Reference in New Issue
Block a user