diff --git a/app/routes.py b/app/routes.py index 07f58c8..09a28e5 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,7 +3,7 @@ from sqlalchemy.orm import Session from app.external_services import resend_otp, send_otp from app.schemas import * -from database.crud import add_user, get_db, verify_otp +from database.crud import * router = APIRouter() @@ -29,7 +29,8 @@ def validate_otp(data: OTPVerify, db: Session = Depends(get_db)): return response -@router.post("/resendOTP", response_model=OTPresendResponse) -def deliver_otp(data: OTPresend, db: Session = Depends(get_db)): +@router.post("/resendOTP", response_model=OTPResendResponse) +def deliver_otp(data: OTPResend, db: Session = Depends(get_db)): + update_otp(data=data, db=db) response = resend_otp(data=data, db=db) return response diff --git a/app/schemas.py b/app/schemas.py index 545ba1f..24a76ed 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -106,14 +106,16 @@ class OTPVerifyResponse(OTPBase): orm_mode = True -class OTPresend(BaseModel): +class OTPResend(BaseModel): email: EmailStr + otp: int = randbits(20) + otp_valid_time: datetime = datetime.now() + timedelta(minutes=10) class Config: orm_mode = True -class OTPresendResponse(UserCreateResponse): +class OTPResendResponse(UserCreateResponse): pass class Config: diff --git a/database/crud.py b/database/crud.py index f3db7a6..1eb554c 100644 --- a/database/crud.py +++ b/database/crud.py @@ -47,12 +47,19 @@ def fetch_user_by_email(data, db): return db.query(Users).filter(Users.email == data.email).first() -def add_user(data, db): +def create_user(data, db): data.password = pwd_context.hash(data.password) user = insert_data(model="Users", data=data, db=db) return user +def update_otp(data: OTPResend, db): + db.query(Users).filter(Users.email == data.email).update( + {Users.otp: data.otp, Users.otp_valid_time: data.otp_valid_time} + ) + db.commit() + + def activate_account(data: OTPVerify, db): db.query(Users).filter(Users.access_key == data.access_key).update( {Users.status: 1} diff --git a/shell.nix b/shell.nix index 51b7b0a..a197bba 100644 --- a/shell.nix +++ b/shell.nix @@ -13,7 +13,6 @@ mkShell { python38Packages.alembic python38Packages.pytest python38Packages.twilio - python38Packages.cryptography python38Packages.passlib python38Packages.bcrypt sqlite