bsition/api/routes/auth.py

27 lines
711 B
Python

from fastapi import APIRouter, Header
from fastapi.responses import JSONResponse
from pydantic import BaseModel, EmailStr
from api.jwt import validate_token, write_token
from backend.postgres import get_user_by_username
auth_routes = APIRouter()
class User(BaseModel):
username: str
@auth_routes.post("/login")
def login(user: User):
if get_user_by_username(user.username) is not None:
return write_token(user.dict())
else:
return JSONResponse(content={"message": "User not found"}, status_code=404)
# @auth_routes.post("/token")
# def verify_token(Authorization: str = Header(None)):
# token = Authorization.split(" ")[1]
# return validate_token(token, output=True)