fastapi-jwt-auth icon indicating copy to clipboard operation
fastapi-jwt-auth copied to clipboard

Cookie not being sent to frontend

Open apaul45 opened this issue 3 years ago • 0 comments

When testing my register and login functions from my front end application, I found that the CSRF cookie was not appearing. I was able to confirm that my backend api never sent it once trying one of my jwt_required functions.

When I test this with the Swagger UI though, I'm finding that it does send a cookie containing the JWT, but that this resides in the http://127.0.0.1:8000/ url.

I'm not sure why the cookie isn't being sent to the front end, as I passed in a response object to set_access_cookies.

@router.post("/login")
async def login_user(user:LoggedInUser, response: Response, auth: AuthJWT = Depends()):
    existing_user = await users_coll.find_one({"username": user.username})
    print(existing_user)
    if not existing_user:
        raise HTTPException(status_code=400, detail="Incorrect username")
    elif not pwd_context.verify(user.password, existing_user["passwordHash"]):
        raise HTTPException(status_code=400, detail="Incorrect password")
    else:
        #Create, store, and return a JWT in a cookie
        token = auth.create_access_token(subject=user.username)
        auth.set_access_cookies(token, response=response)
        return {"msg": "User successfully logged in"}

Screen Shot 2022-06-05 at 4 03 22 PM

Screen Shot 2022-06-05 at 4 04 18 PM

apaul45 avatar Jun 05 '22 20:06 apaul45