43 lines
1.7 KiB
Python
43 lines
1.7 KiB
Python
from fastapi import APIRouter, Depends
|
|
from motor.motor_asyncio import AsyncIOMotorDatabase
|
|
|
|
from app.core.security import create_access_token
|
|
from app.db.mongo import get_database
|
|
from app.schemas.auth import (
|
|
AuthBootstrapResponse,
|
|
AuthLoginBootstrapRequest,
|
|
AuthLoginRequest,
|
|
AuthSignupRequest,
|
|
AuthTokenResponse,
|
|
)
|
|
from app.services.auth import create_account, get_account_by_identifier, verify_account_login
|
|
|
|
router = APIRouter(prefix="/auth", tags=["auth"])
|
|
|
|
|
|
@router.post("/signup", response_model=AuthTokenResponse)
|
|
async def signup(payload: AuthSignupRequest, database: AsyncIOMotorDatabase = Depends(get_database)) -> AuthTokenResponse:
|
|
owner_id = await create_account(database, payload)
|
|
access_token = create_access_token(owner_id)
|
|
return AuthTokenResponse(accessToken=access_token, ownerId=owner_id)
|
|
|
|
|
|
@router.post("/bootstrap", response_model=AuthBootstrapResponse)
|
|
async def bootstrap(
|
|
payload: AuthLoginBootstrapRequest,
|
|
database: AsyncIOMotorDatabase = Depends(get_database),
|
|
) -> AuthBootstrapResponse:
|
|
account = await get_account_by_identifier(database, payload.identifierType.value, payload.identifier)
|
|
return AuthBootstrapResponse(
|
|
identifierType=payload.identifierType,
|
|
identifier=account["identifier"],
|
|
saltAuth=account["saltAuth"],
|
|
saltEnc=account["saltEnc"],
|
|
)
|
|
|
|
|
|
@router.post("/login", response_model=AuthTokenResponse)
|
|
async def login(payload: AuthLoginRequest, database: AsyncIOMotorDatabase = Depends(get_database)) -> AuthTokenResponse:
|
|
owner_id = await verify_account_login(database, payload)
|
|
access_token = create_access_token(owner_id)
|
|
return AuthTokenResponse(accessToken=access_token, ownerId=owner_id)
|