Kekse werden nicht an Backend (Fastapi) vom Frontend (React) [Duplicat] übergeben.JavaScript

Javascript-Forum
Anonymous
 Kekse werden nicht an Backend (Fastapi) vom Frontend (React) [Duplicat] übergeben.

Post by Anonymous »

Ich habe 2 Cookies aus Fastapi wie: < /p>

Code: Select all

    RESPONSE.set_cookie(
key="access_token",
value=ACCESS_TOKEN_FINAL,
httponly=True,
secure=True,
max_age=3600,
expires=time.time() + 3600,
samesite="none"
)

< /code>
beide, die ich in meinem Browser sehen kann. Wenn ich jedoch versuche, eine Sitzungsprüfung aufzurufen; < /p>
@AUTH_ROUTER.get("/checkSession")
async def checkSession(request: Request):
AdvancedLogging(f"checkSession called")
AdvancedLogging(f"Request.cookies all: {request.cookies.get('*')}")
< /code>
Ich kann sehen, dass keine Cookies gesendet werden. CORS ist korrekt eingerichtet, und ich rufe die API mit: < /p>
aufexport async function isAuthenticated() {
try {
const response = await fetch("http://127.0.0.1:8000/auth/checkSession", {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
});

const data = await response.json();

if (response.ok) {
console.log("User is authenticated:", data);
return true; // User is authenticated
} else {
console.error("User is not authenticated");

// If the backend returns a "redirect" field, use it to redirect the user
if (data.redirect) {
window.location.href = data.redirect; // Redirect to the dynamically provided login URL
}
return false; // User is not authenticated
}
} catch (error) {
console.error("Error during authentication check:", error);
return false; // Return false on error
}
}

< /code>
Ich habe das Gefühl, verrückt zu werden. Hat jemand irgendwelche Vorschläge? >## CORS Middleware
app.add_middleware(
CORSMiddleware,
allow_origins=[IsScoped("FRONTEND_URL")],
# allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post