Skip to content

Commit

Permalink
feat: add auth endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rorre committed Jun 7, 2024
1 parent c13fc58 commit b21b33e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cefies/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fastapi import FastAPI

import cefies.internal.firestore # noqa: F401 -- Intended to initialize Firebase
from cefies.routes import index_router
from cefies.routes import index_router, auth_router

app = FastAPI()
app.include_router(index_router)
app.include_router(auth_router)
17 changes: 17 additions & 0 deletions cefies/models/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Annotated
from pydantic import BaseModel, EmailStr, StringConstraints


class Token(BaseModel):
token: str


class LoginData(BaseModel):
email: EmailStr
password: str


class RegisterData(BaseModel):
email: EmailStr
password: Annotated[str, StringConstraints(min_length=8)]
name: Annotated[str, StringConstraints(min_length=1)]
6 changes: 6 additions & 0 deletions cefies/models/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pydantic import BaseModel


class MessageResponse(BaseModel):
error: bool
message: str
6 changes: 5 additions & 1 deletion cefies/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from cefies.routes.index import router as index_router
from cefies.routes.auth import router as auth_router


__all__ = ["index_router"]
__all__ = [
"index_router",
"auth_router",
]
48 changes: 48 additions & 0 deletions cefies/routes/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from typing import Optional
from fastapi import APIRouter, HTTPException, UploadFile, status

from cefies.models.auth import LoginData, RegisterData, Token
from cefies.models.db.user import User
from cefies.models.response import MessageResponse
from cefies.security import authenticate_user, create_access_token, get_password_hash


router = APIRouter(prefix="/auth")


@router.post("/login")
async def login(data: LoginData):
user = authenticate_user(data.email, data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)

token = create_access_token(user.key)
return Token(token=token)


@router.post("/register")
def register(data: RegisterData):
existing_user = User.collection.filter(email=data.email).get()
print(existing_user)
if existing_user:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Email has been used",
)

new_user = User()
new_user.email = data.email
new_user.name = data.name
new_user.password = get_password_hash(data.password)
# TODO: Upload file to object and set avatar
new_user.avatar = ""
new_user.save()

return MessageResponse(
error=False,
message="Successfully registered",
)

0 comments on commit b21b33e

Please sign in to comment.