Handle Registration in FastAPI and Tortoise ORM
By: Bartosz Konikiewicz
Tags: Registration, Python, Fastapi, Tortoise Orm, Authentication

Config

You need two things here:

Application settings Database settings

Let’s start with the former, as database settings will depend on it.

Application Settings

Settings are placed in /app/config/settings.py file. One of FastAPI dependencies is Pydantic — a tool for settings management and data validation. Eventually we will use both of these features. First, you need to create a model that inherits from BaseSettings class. This is the right choice, because I opted for saving some sensitive data in environment variables. As Pydantic devs state it:

If you create a model that inherits from BaseSettings , the model initialiser will attempt to determine the values of any fields not passed as keyword arguments by reading from the environment. (Default values will still be used if the matching environment variable is not set.)

Knowing that, your settings class should look like this:

Let’s break it down a bit:

DATABASE_URL — an environment variable you’ve defined when setting dbmate up. It needs to belong to Settings class, because you will reference it when setting Tortoise ORM up.

— an environment variable you’ve defined when setting dbmate up. It needs to belong to class, because you will reference it when setting Tortoise ORM up. SECRET_KEY — an environment variable ( that you need to define now ) used when generating a registration confirmation token. For purposes of this tutorial, it may be any random string, like sdasfgrt23 . For real apps, use a string generated by OpenSSL.

— an environment variable ( ) used when generating a registration confirmation token. For purposes of this tutorial, it may be any random string, like . For real apps, use a string generated by OpenSSL. REGISTRATION_TOKEN_LIFETIME — lifetime of a token sent in an email an user gets after registering an account (in seconds).

— lifetime of a token sent in an email an user gets after registering an account (in seconds). TOKEN_ALGORITHM — an algorithm you’ll provide when generating a registration token.

— an algorithm you’ll provide when generating a registration token. APP_NAME — FastAPI generates a Swagger documentation for you. This is where the application name gets displayed.

— FastAPI generates a Swagger documentation for you. This is where the application name gets displayed. SMTP_SERVER — in this tutorial, you will use a debugging server built into Python. It’s going to be localhost:25 . Define it now.

— in this tutorial, you will use a debugging server built into Python. It’s going to be . MAIL_SENDER — the sender of the registration confirmation email.

— the sender of the registration confirmation email. API_PREFIX — a string to prepend every endpoint with. You may as well leave it empty.

— a string to prepend every endpoint with. You may as well leave it empty. HOST — server host.

— server host. PORT — server port

— server port BASE_URL — base URL that you’ll use to generate a confirmation link.

— base URL that you’ll use to generate a confirmation link. MODELS — here you declare your models. You specify module paths, which means that models.users stands for a module users in models package.

— here you declare your models. You specify module paths, which means that stands for a module in package. You also provide a Config class, where you specify, that variable names are case sensitive. It doesn’t matter for Windows, but it’s important if you use Linux.

There is no need to create Settings object every time you need to reference a variable. We can define a factory method that will return a singleton, like so:

from functools import lru_cache

@lru_cache()

def get_settings():

return Settings()

So far, so good. Let’s move to database configuration.

Database Settings

You define database config in /app/config/db.py . Again — this is just a convention. If you need to provide custom settings, you can look them up in Tortoise ORM documentation. In this tutorial, config is simple. We just need to define two things:

Connection string

ORM models declared in Settings class

Simple enough:

One thing to note: you need to strip database connection string of optional arguments: settings.DATABASE_URL.split('?')[0] . This means that the connection string won’t include sslmode=disable part.

Now you can actually use the config.