|
|
@ -6,6 +6,7 @@ from pydantic import BaseModel
|
|
|
|
from jose import ExpiredSignatureError, JWTError
|
|
|
|
from jose import ExpiredSignatureError, JWTError
|
|
|
|
|
|
|
|
|
|
|
|
from ..crud.user_crud import select_account_by
|
|
|
|
from ..crud.user_crud import select_account_by
|
|
|
|
|
|
|
|
from ..utils.email_code import verify_code
|
|
|
|
from ..utils.password import verify_password
|
|
|
|
from ..utils.password import verify_password
|
|
|
|
from ..utils.token_handler import (
|
|
|
|
from ..utils.token_handler import (
|
|
|
|
create_signin_token,
|
|
|
|
create_signin_token,
|
|
|
@ -30,6 +31,7 @@ class TokenPayload(BaseModel):
|
|
|
|
async def signin_by_username(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
|
|
async def signin_by_username(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
|
|
username = form_data.username
|
|
|
|
username = form_data.username
|
|
|
|
password = form_data.password
|
|
|
|
password = form_data.password
|
|
|
|
|
|
|
|
|
|
|
|
is_existence, user = await select_account_by("username", username)
|
|
|
|
is_existence, user = await select_account_by("username", username)
|
|
|
|
|
|
|
|
|
|
|
|
if not is_existence:
|
|
|
|
if not is_existence:
|
|
|
@ -43,6 +45,24 @@ async def signin_by_username(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
|
|
return {"code": 10201, "msg": "Username or Password Is Incorrect"}
|
|
|
|
return {"code": 10201, "msg": "Username or Password Is Incorrect"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/email", response_model=UserAccountResponse)
|
|
|
|
|
|
|
|
async def signin_by_email(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
|
|
|
|
|
|
email = form_data.username
|
|
|
|
|
|
|
|
code = form_data.password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
is_existence, user = await select_account_by("email", email)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not is_existence:
|
|
|
|
|
|
|
|
return {"code": 10202, "msg": "The Email Has Not Been Registered"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
is_correct = verify_code(email, code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if is_correct:
|
|
|
|
|
|
|
|
return {"code": 10200, "msg": "Sign in Successfully", "data": user.to_dict()}
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return {"code": 10201, "msg": "The Verification Code Is Not Correct"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/token", response_model=TokenCreationResponse)
|
|
|
|
@router.post("/token", response_model=TokenCreationResponse)
|
|
|
|
async def create_token(token_payload: TokenPayload):
|
|
|
|
async def create_token(token_payload: TokenPayload):
|
|
|
|
token = create_signin_token(**token_payload.model_dump())
|
|
|
|
token = create_signin_token(**token_payload.model_dump())
|
|
|
|