dorinclisu / fastapi-auth0

FastAPI authentication and authorization using auth0.com
MIT License
230 stars 39 forks source link

How to add an action after the Authorization flow ? #35

Closed pybanaszak closed 1 year ago

pybanaszak commented 1 year ago

Let's say I want to create a user in my database if it does no exist right after the Authorization Flow on all the auth required endpoints. How should I proceed ?

dorinclisu commented 1 year ago

Well, the way I proceed in all my use cases is with Auth0User.id or Auth0User.email as the unique key for my own database table/collection.

Practically, your custom user function would be something like:

def get_db_user(auth0user: Auth0User = Security(auth.get_user)) -> User: 
    # implement or call the relevant db functions: fetch, automatically insert, etc.

This decouples the application database from the auth0 database which is valid across multiple apps as SSO.