build seeder

This commit is contained in:
2024-05-10 06:31:31 +00:00
parent bc8815f40e
commit 392dee8640
5 changed files with 46 additions and 34 deletions

View File

@ -3,6 +3,7 @@ from fastapi import APIRouter, Depends, HTTPException, Response
from fastapi.encoders import jsonable_encoder
from sqlalchemy.orm import Session
from fuware.core.security.hasher import get_hasher
from fuware.db.db_setup import generate_session
from fuware.schemas import ReturnValue, PrivateUser, UserRequest
from fuware.schemas.user.user import UserCreate
@ -10,8 +11,8 @@ from fuware.services import UserService
public_router = APIRouter(tags=["Users: Authentication"])
user_service = UserService()
hasher = get_hasher()
@public_router.put('/register')
def register_user(user: UserCreate, db: Session = Depends(generate_session)) -> ReturnValue[Any]:
@ -22,14 +23,15 @@ def register_user(user: UserCreate, db: Session = Depends(generate_session)) ->
return ReturnValue(status=200, data=jsonable_encoder(user_return))
# @public_router.post('/login', response_model=ReturnValue[PrivateUser])
# def user_login(user: UserRequest, response: Response) -> ReturnValue[Any]:
# db_user = UserService.get_by_username(user.username)
# if not db_user:
# raise HTTPException(status_code=401, detail="Your username or password input is wrong!")
# if not verify_password(user.password, db_user.password):
# raise HTTPException(status_code=401, detail="Your username or password input is wrong!")
# if db_user.is_lock is True:
# raise HTTPException(status_code=401, detail="Your Account is banned")
@public_router.post('/login', response_model=ReturnValue[Any])
def user_login(user: UserRequest, response: Response) -> ReturnValue[Any]:
db_user = user_service.get_by_username(username=user.username)
if not db_user:
raise HTTPException(status_code=401, detail="Your username or password input is wrong!")
if not hasher.verify(password=user.password, hashed=db_user.password):
raise HTTPException(status_code=401, detail="Your username or password input is wrong!")
if db_user.is_lock is True:
raise HTTPException(status_code=401, detail="Your Account is banned")
# cookieEncode = encryptString(user.username + ',' + user.password)
# response.set_cookie(key=COOKIE_KEY, value=cookieEncode.decode('utf-8'))
# return ReturnValue(status=200, data=jsonable_encoder(db_user))
return ReturnValue(status=200, data=jsonable_encoder(db_user))