[Python] How can I redirect a user from login page to another page and save their cookies?

Discussão em 'Python' iniciado por Stack, Janeiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have a code for post request:

    @router.post("/login")
    async def login(response: Response, credentials: UserLoginSchema = Form()):
    if credentials.email == ADMIN_EMAIL and credentials.password == "123":
    token = auth.create_access_token(uid=credentials.email)
    response.set_cookie(config.JWT_ACCESS_COOKIE_NAME, token, path="/", samesite="lax", secure=True, httponly=True)
    return RedirectResponse(url="/", status_code=status.HTTP_302_FOUND)
    raise HTTPException(401, detail={"message": "Invalid credentials"})


    And I have a main page route:

    @app.get("/", response_class=HTMLResponse, dependencies=[Depends(auth.access_token_required)])
    async def get_index(request: Request):
    return templates.TemplateResponse(name="index.html", request=request)


    When you sign in it doesn't work as expected.

    If I do it without redirect, just manually going to "/" after singing in, it works out. But as soon as I return RedirectResponse(url="/", status_code=status.HTTP_302_FOUND) it just forgets that I want to set a cookie and goes to "/" without them.

    What's wrong?

    I use FastAPI and AuthX.

    Continue reading...

Compartilhe esta Página