Oauth2
FastAPI Users provides an optional OAuth2 authentication support. Read the official documentation on this for more details.
Installation¶
In case you are confused on how to install dependencies:
1 | |
Configuration¶
Instantiate an OAuth2 client¶
You first need to get an HTTPX OAuth client instance. Read the documentation for more information.
1 2 3 | |
Setup the database adapter¶
You'll need to define the Tortoise ORM model for storing OAuth accounts. We provide a base one for this:
1 2 3 4 5 6 7 8 9 10 | |
The base class TortoiseBaseUserOAuthAccountModelUUID already defines a foreign key to your user model like this:
You only need to change this if your user model is not called User or if it is not present in a tortoise orm app/namespace called models.
1 2 3 4 5 6 | |
User or if it is not present in a tortoise orm app/namespace called models.
However, you must keep the fields name as user and the related_name as oauth_accounts.
Besides, when instantiating the database adapter, we need pass this Tortoise ORM model as second argument.
Primary key is defined as UUID
By default, we use UUID as a primary key ID for your user. If you want to use another type, like an auto-incremented integer, you can use TortoiseBaseUserOAuthAccountModel as base class and define your own id field.
1 2 | |