Add forgetting password api
parent
6cf370cde6
commit
622748cb37
|
@ -54,7 +54,9 @@ async def select_account_by(
|
||||||
|
|
||||||
|
|
||||||
async def update_account(
|
async def update_account(
|
||||||
account: Literal["username", "email", "password"], id: str, value: str
|
account: Literal["username", "email", "password"],
|
||||||
|
id: str,
|
||||||
|
value: str,
|
||||||
):
|
):
|
||||||
session = async_session()
|
session = async_session()
|
||||||
match account:
|
match account:
|
||||||
|
@ -74,6 +76,17 @@ async def update_account(
|
||||||
await session.close()
|
await session.close()
|
||||||
|
|
||||||
|
|
||||||
|
async def update_password(email: str, new_password: str):
|
||||||
|
session = async_session()
|
||||||
|
await session.execute(
|
||||||
|
update(UserAccount)
|
||||||
|
.where(UserAccount.email == email)
|
||||||
|
.values(password=new_password)
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
await session.close()
|
||||||
|
|
||||||
|
|
||||||
async def select_profile(id: str) -> UserProfile:
|
async def select_profile(id: str) -> UserProfile:
|
||||||
session = async_session()
|
session = async_session()
|
||||||
res: ScalarResult[UserProfile] = await session.scalars(
|
res: ScalarResult[UserProfile] = await session.scalars(
|
||||||
|
|
|
@ -5,9 +5,9 @@ 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, update_password
|
||||||
from ..utils.email_code import verify_code
|
from ..utils.email_code import verify_code
|
||||||
from ..utils.password import verify_password
|
from ..utils.password import verify_password, get_hashed_password
|
||||||
from ..utils.token_handler import (
|
from ..utils.token_handler import (
|
||||||
create_signin_token,
|
create_signin_token,
|
||||||
oauth2_scheme,
|
oauth2_scheme,
|
||||||
|
@ -27,6 +27,12 @@ class TokenPayload(BaseModel):
|
||||||
device_id: str
|
device_id: str
|
||||||
|
|
||||||
|
|
||||||
|
class ForgetPasswordBody(BaseModel):
|
||||||
|
email: str
|
||||||
|
password: str
|
||||||
|
code: str
|
||||||
|
|
||||||
|
|
||||||
@router.post("/username", response_model=UserAccountResponse)
|
@router.post("/username", response_model=UserAccountResponse)
|
||||||
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
|
||||||
|
@ -98,3 +104,18 @@ async def signin_by_token(token: str = Depends(oauth2_scheme)):
|
||||||
"code": 9998,
|
"code": 9998,
|
||||||
"msg": "Token Is Not Right",
|
"msg": "Token Is Not Right",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/forget_password", response_model=UserAccountResponse)
|
||||||
|
async def forget_password(body: ForgetPasswordBody):
|
||||||
|
is_correct = verify_code(body.email, body.code)
|
||||||
|
if not is_correct:
|
||||||
|
return {"code": 10201, "msg": "The Verification Code Is Not Correct"}
|
||||||
|
|
||||||
|
hashed_password = get_hashed_password(body.password)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await update_password(body.email, hashed_password)
|
||||||
|
return {"code": 10200, "msg": "Update Password Successfully"}
|
||||||
|
except Exception:
|
||||||
|
return {"code": 9999, "msg": "Update Password Fail, Please Try Again Later"}
|
||||||
|
|
Loading…
Reference in New Issue