Skip to content

Proper way of implementing (reusing) the user and session_auth app in a FastAPI project? #112

Answered by dantownsend
heliumbrain asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for the kind words.

With the session auth, Piccolo API has a function called session_login, which returns a Starlette HTTPEndpoint.

As FastAPI is built on top of Starlette, you can just mount this login endpoint within your FastAPI app like this:

app = FastAPI()

app.mount(
    path="/login/",
    app=session_login(
        auth_table=BaseUser,  # Or some subclass of BaseUser
        session_table=SessionsBase,   # Or some subclass of BaseUser
        redirect_to='/',
    ),
)

For your use case, it sounds like you want to create a session for the user straight after they create an account. You could do something like this:

@app.post("/auth/register")
async def create_user(user: Bas…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@heliumbrain
Comment options

@heliumbrain
Comment options

Answer selected by heliumbrain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants