0

I am writing a small api for creating different objects. And I wanted to carry out a routine check that can be found on every website with registration. But I came across such an unpleasant use of the if operator.

class AuthenticateController(RegisterCRUD, Controller):
    prefix = "/api/auth"
    tags = ["auth"]
    refresh_token_ttl = 30*60
    access_token_ttl = 15*60

    @post("/register")
    async def register(user: UserIn):
        if not (user.name and user.secondname and user.lastname and user.phone and user.login):
            raise HTTPException(status_code=400, detail="Please fill in all required fields.")
        if not RegisterCRUD.phone_validator(user.phone):
            raise HTTPException(status_code=400, detail="Please enter the correct phone number.")
        if RegisterCRUD.check_phone(user.phone):
            raise HTTPException(status_code=400, detail="This phone number is already registered.")
        if RegisterCRUD.check_login(user.login):
            raise HTTPException(status_code=400, detail="This login is already occupied.")
        if RegisterCRUD.register(user.name, user.secondname, user.lastname, user.phone,
                                user.login, HashedData.hash_password(user.password)):
            return JSONResponse(status_code=201, content={"message": "The user has been successfully created."})
        raise HTTPException(status_code=501, detail="Failed to create a user.")

But in general, my login and phone db models have the value unique=True. So trying to create the same values causes an error. But I haven't figured out how to link the return of the desired message to it.

I would like to clarify whether it is correct to carry out verification in this way? Or there is a more different way.

In general, I am waiting for criticism and advice! Thank you all in advance!

Serega
  • 171
  • 8
  • 1
    If I understand correctly, you'd like to remove the `RegisterCRUD.check_phone` and `RegisterCRUD.check_login` `if` statements, and instead rely on the database uniqueness constraint? What database and ORM are you using? – M.O. Apr 23 '23 at 21:42
  • 1
    If you're using SQLAlchemy, trying to insert a duplicate value in a unique column will raise an `IntegrityError`, and you have to parse the error message to find out which column was the duplicate one. [Here](https://stackoverflow.com/a/74316313/11612918) is an example (I've not tried it myself, YMMV). – M.O. Apr 23 '23 at 21:44
  • 1
    I would also recommend removing your first `if` statement and making those fields required in the `UserIn` model. Then FastAPI will automatically return a 422 error if either of those columns are missing. – M.O. Apr 23 '23 at 21:46
  • 2
    The same is also the case for the second `if`-statement; this should be handled with a validator in your Pydantic model. For the two other cases, that should be the responsibility of the `register` method, which could throw exceptions if either username or phone is already registered. – MatsLindh Apr 24 '23 at 07:46

0 Answers0