This commit is contained in:
DESKTOP-RTLN3BA\$punk 2025-12-19 13:30:50 -08:00
parent 10a2180873
commit 8d66d32c07
2 changed files with 19 additions and 38 deletions

View file

@ -9,20 +9,24 @@ export function GoogleLoginButton() {
const t = useTranslations("auth");
const handleGoogleLogin = () => {
// IMPORTANT:
// FastAPI Users OAuth stores the "state" in a cookie.
// Doing a cross-origin fetch() (www.surfsense.com -> backend.ssbacktemp.xyz)
// will NOT persist Set-Cookie unless you use credentials + non-wildcard CORS.
// The simplest/most reliable approach is a top-level navigation to the backend
// authorize endpoint so the cookie is set as first-party.
const backendUrl = process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL;
if (!backendUrl) {
console.error("Missing NEXT_PUBLIC_FASTAPI_BACKEND_URL");
return;
}
// This endpoint performs a 302 to Google (more reliable than fetching JSON).
window.location.href = `${backendUrl}/auth/google/start`;
// Redirect to Google OAuth authorization URL
fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/auth/google/authorize`)
.then((response) => {
if (!response.ok) {
throw new Error("Failed to get authorization URL");
}
return response.json();
})
.then((data) => {
if (data.authorization_url) {
window.location.href = data.authorization_url;
} else {
console.error("No authorization URL received");
}
})
.catch((error) => {
console.error("Error during Google login:", error);
});
};
return (
<div className="relative w-full overflow-hidden">