Skip to main content

FastAPI Users DB Deta

https://img.shields.io/pypi/v/fastapi_users_db_deta.svg https://img.shields.io/travis/ak4zh/fastapi_users_db_deta.svg Documentation Status

Sub-package for Deta Base support in FastAPI Users.

Installation

To install FastAPI Users DB Deta in a project:

pip install -U fastapi_users_db_deta

Usage

Project structure:

.
├── users
│   ├── __init__.py
│   ├── models.py
│   ├── manager.py
└── main.py

users/models.py:

from fastapi_users import models
from fastapi_users.models import BaseOAuthAccountMixin


class User(models.BaseUser, BaseOAuthAccountMixin):
    pass


class UserCreate(models.BaseUserCreate):
    pass


class UserUpdate(models.BaseUserUpdate):
    pass


class UserDB(User, models.BaseUserDB):
    pass

users/manager.py:

from typing import Optional

import deta
from fastapi import Depends, Request
from fastapi_users import BaseUserManager, FastAPIUsers
from fastapi_users.authentication import JWTAuthentication, CookieAuthentication
from fastapi_users_db_deta import DetaBaseUserDatabase

from users.models import UserDB, UserCreate, UserUpdate, User

SECRET = 'secret'
DETA_PROJECT_KEY = 'deta_project_key'
DATABASE = deta.Deta(DETA_PROJECT_KEY)
UserBase = DATABASE.Base('users')
OAuthBase = DATABASE.Base('oauth_accounts')


class UserManager(BaseUserManager[UserCreate, UserDB]):
    user_db_model = UserDB
    reset_password_token_secret = SECRET
    verification_token_secret = SECRET


async def get_user_db():
    yield DetaBaseUserDatabase(UserDB, UserBase().db, OAuthBase().db)


async def get_user_manager(user_db=Depends(get_user_db)):
    yield UserManager(user_db)


jwt_authentication = JWTAuthentication(
    secret=SECRET, lifetime_seconds=5, tokenUrl="auth/jwt/login"
)
cookie_authentication = CookieAuthentication(
    secret=SECRET, lifetime_seconds=5, cookie_name='userAuthToken'
)

fastapi_users_app = FastAPIUsers(
    get_user_manager,
    [jwt_authentication, cookie_authentication],
    User,
    UserCreate,
    UserUpdate,
    UserDB,
)

current_active_user = fastapi_users_app.current_user(active=True)

main.py:

from fastapi import FastAPI

from users.manager import cookie_authentication, jwt_authentication, fastapi_users_app

app = FastAPI()

# include cookie auth router
app.include_router(
    fastapi_users_app.get_auth_router(
        cookie_authentication,
    ), prefix="/auth/cookie", tags=["auth"]
)

# include jwt auth router
app.include_router(
    fastapi_users_app.get_auth_router(
        jwt_authentication,
    ), prefix="/auth/jwt", tags=["auth"]
)

app.include_router(
    fastapi_users_app.get_register_router(), prefix="/auth", tags=["auth"]
)

app.include_router(
    fastapi_users_app.get_reset_password_router(),
    prefix="/auth",
    tags=["auth"],
)

app.include_router(
    fastapi_users_app.get_verify_router(),
    prefix="/auth",
    tags=["auth"],
)

app.include_router(
    fastapi_users_app.get_users_router(), prefix="/users", tags=["users"]
)

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.1.0 (2021-11-17)

  • First release on PyPI.

Release files for fastapi-users-db-deta 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-users-db-deta 0.2.3
File Size Uploaded
fastapi_users_db_deta-0.2.3.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-users-db-deta 0.2.3
File Interpreter ABI Platform
fastapi_users_db_deta-0.2.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 17.0 kB

Release files / fastapi_users_db_deta-0.2.3.tar.gz

Download URL fastapi_users_db_deta-0.2.3.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bd0f9e93fa328e1cf30d983dcecc95a04a77ea42f9eb796791e14ea7473ba7dd
BLAKE2b-256 checksum
How to use checksums
1163a2b055af5cb73b1c3eff63e79197a4b7958e97246043596a013def3b5c25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.8.2 requests/2.26.0 setuptools/57.0.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / fastapi_users_db_deta-0.2.3-py2.py3-none-any.whl

Download URL fastapi_users_db_deta-0.2.3-py2.py3-none-any.whl
Size 5.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0d93e5ea9b6f1ffc90fde2784b9bb311250311c15fcde14ff968949028441f50
BLAKE2b-256 checksum
How to use checksums
280e5eed54c4e032626eb71ff6d9a1e109eb65556bc119909f7f87b8940b8c81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.8.2 requests/2.26.0 setuptools/57.0.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page