2020-09-11 00:00:48 +02:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response
|
|
|
|
from sqlalchemy.orm import Session
|
2020-06-05 01:09:55 +02:00
|
|
|
|
|
|
|
from app.schemas import *
|
|
|
|
from app.twilio import send_otp
|
2020-09-11 00:00:48 +02:00
|
|
|
from database.crud import get_db, insert_data, verify_otp
|
2020-03-23 21:17:16 +01:00
|
|
|
|
2020-09-03 18:31:52 +02:00
|
|
|
router = APIRouter()
|
2020-03-23 21:17:16 +01:00
|
|
|
|
2020-09-03 18:31:52 +02:00
|
|
|
|
|
|
|
@router.post("/register")
|
2020-09-11 00:00:48 +02:00
|
|
|
async def create_user(request: UserCreate, db: Session = Depends(get_db)):
|
|
|
|
insert_data(model="Users", data=request, db=db)
|
2020-06-05 01:09:55 +02:00
|
|
|
send_otp(receiver=request.mobile)
|
|
|
|
return {"message": "User created, pending OTP verification"}
|
2020-04-19 21:30:41 +02:00
|
|
|
|
|
|
|
|
2020-06-05 01:09:55 +02:00
|
|
|
# FIXME Use OAuth2 for verification
|
2020-09-03 18:31:52 +02:00
|
|
|
@router.post("/login")
|
2020-09-11 00:00:48 +02:00
|
|
|
async def log_in(request: UserLogin, response: Response, db: Session = Depends(get_db)):
|
2020-06-05 01:09:55 +02:00
|
|
|
return {"message": "Logged in successfully"}
|
|
|
|
# response.status_code = status.HTTP_400_BAD_REQUEST
|
|
|
|
# return {"message": "The email/password combination is not correct"}
|
2020-05-28 00:53:23 +02:00
|
|
|
|
|
|
|
|
2020-09-03 18:31:52 +02:00
|
|
|
@router.post("/otpVerification")
|
2020-09-11 00:00:48 +02:00
|
|
|
async def validate_otp(
|
|
|
|
request: OTPVerify, response: Response, db: Session = Depends(get_db)
|
|
|
|
):
|
|
|
|
if verify_otp(data=request, db=db):
|
2020-06-05 01:09:55 +02:00
|
|
|
return {"message": "The OTP has been verified successfully"}
|
2020-07-24 19:27:15 +02:00
|
|
|
raise HTTPException(status_code=400, detail="The OTP is not correct")
|