I’m facing issue in storing cookies in front side. I’m currently using FastAPI with fastapi-session
and when I try to connect it to the front side (html,css,js), it doesn’t work.
When I use FastAPI with session to communicate using postman, it works fine without any issues but when I try using front side it doesn’t work, the issue is in JavaScript side but I’m not sure how to make FastAPI be integrated with front side that will work with fastapi-session.
I used set cookies in JavaScript but it didn’t work
Thank you
Part of python code
# fastapi
app = FastAPI(
title="API",
description=description,
version="1.0.0", # API version
openapi_tags=tags_metadata,
)
origins = ["*"]
app.add_middleware(CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"], )
@app.get("/", tags=["Root"], response_model=Result,
responses={
**responses,
200: {
"description": "Successfully created new session",
"content": {
"application/json": {
"example": {'status_code': '0', 'status_message': 'Success',
'data': "hi admin. How can i help you"}
}
},
},
})
async def root(response: Response, auth: str = Header(None)):
if auth:
authenticate = hmac.compare_digest(auth, "abc")
if authenticate:
session = uuid1()
SessionCreation(session)
data = SessionData(authorized=authenticate, session_id=session)
await backend.create(session, data)
cookie.attach_to_response(response, session)
return {"status_code": "0", "status_message": "Success", "data": response}
else:
return JSONResponse(status_code=401, content="Unauthorized!")
else:
return JSONResponse(status_code=404, content={"message": "Missing Header"})
Part of JavaScript code
# js code
fetch(url, {
method: "GET",
mode: "cors",
headers: new Headers({
"auth": "abc",
"Content-Type": "application/json; charset=UTF-8",
}),
})
.then(() => {
alert(document.cookie)
});