Proper way of implementing (reusing) the user and session_auth app in a FastAPI project? #112
-
Hi there! First of all I just need to say, GREAT job with this project. I love how well thought out it is and the documentation is on point. I'm quite new to both FastAPI and Piccolo, so please bear with me in my adventure of incompetence :) I'm currently setting up a FastAPI project that will act as the backend API for a web-app that lives completely separate. I've read up a bit on the User implementation in Piccolo and the session_auth app in piccolo-api. So far I've managed to create a endpoint that allows end-users to register for a new account: BaseUserPydantic = create_pydantic_model(table=BaseUser, model_name="BaseUserPydantic")
@app.post("/auth/register")
async def create_user(user: BaseUserPydantic):
try:
user = BaseUser(**user.__dict__)
await user.save().run()
except Exception:
return Response(status_code=500) This works, but I'm sure it can be improved... Now, where I feel a bit lost is how to "hook in" the session-auth and making it work together.
I have a lot of tiny questions, but mainly I just need some guidance in getting my auth-flow to work. I've tried digging through the fastapi-admin repo to see how it was solved there, but I'm afraid I can't really grasp it. Appologize for the vauge questions. I wish I knew enough to pinpoint my questions better :) Appreciate any help, and please let me know if you need me to expand on some parts of my setup. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Thanks for the kind words. With the session auth, Piccolo API has a function called session_login, which returns a Starlette 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: BaseUserPydantic):
try:
user = BaseUser(**user.__dict__)
await user.save().run()
session = await SessionsBase.create_session(user_id=user.id)
response = Response(status_code=200)
response.set_cookie(
key='id',
value=session.token,
httponly=True,
secure=True, # if in production, otherwise False
samesite="lax",
)
return response
except Exception:
return Response(status_code=500) You then wrap any endpoints you want to protect using the SessionAuthMiddleware. In terms of adding new columns to Does that help? |
Beta Was this translation helpful? Give feedback.
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:
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: