1

Why don't I have cookies installed in my browser when I try to return a message in this code?

@post("/registration_user")#, response_class=JSONResponse)
    async def sign_in_user(self, item: schemas.RegistrWithoutLogin):
        print("-------------------------------------------------------------------")
        self.registration_user(models.User, models.Credentials, item.middlename, item.name, item.lastname, item.phone, item.login, item.password)
        jwt_user = self.authenticate_user(item.login, item.password)
        print(jwt_user)
        print("-------------------------------------------------------------------")
        response = Response(status_code=200)
        response.set_cookie(key="Authorization", value=_T.encode_jwt(jwt_user), max_age=600)
        #role = [{"role": "user"}]
        return {"role": "user", "cookie_set": True} #JSONResponse(role, headers=response.headers)

In the code in the comments there are also options that I tried for my purpose, but they also did not install cookies in the browser.

I run everything if anything in another file Here is his code

app = FastAPI()
origins = [
    "http://localhost.GideOne.com",
    "https://localhost.GideOne.com",
    "http://localhost",
    "http://localhost:8000",

]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

def main():
    app.include_router(AuthenticateController.create_router())
    app.include_router(Company.create_router())
    app.include_router(InviteEmployee.create_router())
    app.include_router(Criteries.create_router())
    app.include_router(OwnersDashboard.create_router())
    app.include_router(WorkersDashboard.create_router())
    app.include_router(ManagersDashboard.create_router())
    uvicorn.run(app, host="0.0.0.0", port=8000,)

if __name__ == "__main__":
    main()
Chris
  • 18,724
  • 6
  • 46
  • 80
Serega
  • 171
  • 8
  • 1
    You're not returning the `response` object you created, so how would FastAPI know to use that Response? How are you verifying that a cookie hasn't been set? – MatsLindh Mar 22 '23 at 12:06
  • Yes, I know that if I return a response, it will set cookies for me in the browser. But I also need to return the message and therefore an error occurs. I check in Swagger_Ui – Serega Mar 22 '23 at 12:11
  • 1
    If you want to return JSON, create a JSONResponse, set the cookie on that response and return that? – MatsLindh Mar 22 '23 at 12:18
  • Thank you very much! I realized the mistake! She was just inattentive. I used the usual Response and I needed JsonResponse – Serega Mar 22 '23 at 12:25
  • Please have a look at [this answer](https://stackoverflow.com/a/74060795/17865804) and [this answer](https://stackoverflow.com/a/73137093/17865804), as well as [here](https://stackoverflow.com/a/73963905/17865804) and [here](https://stackoverflow.com/a/73599289/17865804) – Chris Apr 22 '23 at 07:08

1 Answers1

0

A common mistake from inattention. It was necessary to use JSON Response instead of Response.

@post("/registration_user")
async def sign_in_user(self, item: schemas.RegistrWithoutLogin):
    print("-------------------------------------------------------------------")
    self.registration_user(models.User, models.Credentials, item.middlename, item.name, item.lastname, item.phone, item.login, item.password)
    jwt_user = self.authenticate_user(item.login, item.password)
    print(jwt_user)
    print("-------------------------------------------------------------------")
    role = [{"role": "user"}]
    response = JSONResponse(content=role)
    response.set_cookie(key="Authorization", value=_T.encode_jwt(jwt_user), max_age=600)
    return response
Serega
  • 171
  • 8