Start foundations

This commit is contained in:
François Voron
2019-10-05 17:39:37 +02:00
commit 4e0b0f6f7d
10 changed files with 541 additions and 0 deletions

View File

30
fastapi_users/models.py Normal file
View 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
View 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
View 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)