2025-07-27 10:05:37 -07:00
|
|
|
"use client";
|
2025-04-07 23:47:06 -07:00
|
|
|
|
2025-07-27 10:41:15 -07:00
|
|
|
import { useEffect } from "react";
|
2026-01-25 16:15:25 +05:30
|
|
|
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
|
2026-04-07 04:45:48 -07:00
|
|
|
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
|
2026-06-23 12:57:01 +05:30
|
|
|
import { getAndClearRedirectPath } from "@/lib/auth-utils";
|
|
|
|
|
import { buildBackendUrl } from "@/lib/env-config";
|
2026-01-09 15:00:15 +02:00
|
|
|
import { trackLoginSuccess } from "@/lib/posthog/events";
|
2025-04-07 23:47:06 -07:00
|
|
|
|
|
|
|
|
interface TokenHandlerProps {
|
2025-12-02 01:24:09 -08:00
|
|
|
redirectPath?: string; // Default path to redirect after storing token (if no saved path)
|
2026-06-23 12:57:01 +05:30
|
|
|
tokenParamName?: string; // Deprecated: tokens are no longer read from URLs
|
2025-04-07 23:47:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-23 12:57:01 +05:30
|
|
|
* Client component that finalizes a cookie session after OAuth/local login.
|
|
|
|
|
* After confirming the session, it redirects the user back to the page they were on before
|
2025-12-02 01:24:09 -08:00
|
|
|
* being redirected to login (if available), or to the default redirectPath.
|
2025-07-27 10:05:37 -07:00
|
|
|
*
|
2025-12-02 01:24:09 -08:00
|
|
|
* @param redirectPath - Default path to redirect after storing token (default: '/dashboard')
|
2025-04-07 23:47:06 -07:00
|
|
|
* @param tokenParamName - Name of the URL parameter containing the token (default: 'token')
|
|
|
|
|
*/
|
|
|
|
|
const TokenHandler = ({
|
2025-12-02 01:24:09 -08:00
|
|
|
redirectPath = "/dashboard",
|
2026-06-23 12:57:01 +05:30
|
|
|
tokenParamName: _tokenParamName = "token",
|
2025-04-07 23:47:06 -07:00
|
|
|
}: TokenHandlerProps) => {
|
2026-01-25 16:15:25 +05:30
|
|
|
// Always show loading for this component - spinner animation won't reset
|
2026-01-27 15:28:30 +05:30
|
|
|
useGlobalLoadingEffect(true);
|
2026-01-25 16:15:25 +05:30
|
|
|
|
2025-07-27 10:05:37 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (typeof window === "undefined") return;
|
|
|
|
|
|
2026-04-07 04:45:48 -07:00
|
|
|
const run = async () => {
|
2026-06-23 12:57:01 +05:30
|
|
|
try {
|
|
|
|
|
const sessionResponse = await fetch(buildBackendUrl("/auth/session"), {
|
|
|
|
|
credentials: "include",
|
|
|
|
|
});
|
|
|
|
|
if (!sessionResponse.ok) {
|
|
|
|
|
window.location.href = "/login";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-05 17:55:21 +02:00
|
|
|
|
2026-06-23 12:57:01 +05:30
|
|
|
const alreadyTracked = sessionStorage.getItem("login_success_tracked");
|
|
|
|
|
if (!alreadyTracked) {
|
|
|
|
|
trackLoginSuccess("google");
|
|
|
|
|
}
|
|
|
|
|
sessionStorage.removeItem("login_success_tracked");
|
2026-01-07 17:22:57 +02:00
|
|
|
|
2026-06-23 12:57:01 +05:30
|
|
|
// Auto-set active search space in desktop if not already set
|
|
|
|
|
if (window.electronAPI?.getActiveSearchSpace) {
|
|
|
|
|
try {
|
|
|
|
|
const stored = await window.electronAPI.getActiveSearchSpace();
|
|
|
|
|
if (!stored) {
|
|
|
|
|
const spaces = await searchSpacesApiService.getSearchSpaces();
|
|
|
|
|
if (spaces?.length) {
|
|
|
|
|
await window.electronAPI.setActiveSearchSpace?.(String(spaces[0].id));
|
2026-04-07 04:45:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-23 12:57:01 +05:30
|
|
|
} catch {
|
|
|
|
|
// non-critical
|
2026-04-07 04:45:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-23 12:57:01 +05:30
|
|
|
|
|
|
|
|
const savedRedirectPath = getAndClearRedirectPath();
|
|
|
|
|
const finalRedirectPath = savedRedirectPath || redirectPath;
|
|
|
|
|
window.location.href = finalRedirectPath;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error finalizing session:", error);
|
|
|
|
|
window.location.href = redirectPath;
|
2026-01-07 18:13:31 +02:00
|
|
|
}
|
2026-04-07 04:45:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
run();
|
2026-06-23 12:57:01 +05:30
|
|
|
}, [redirectPath]);
|
2025-07-27 10:05:37 -07:00
|
|
|
|
2026-01-25 16:15:25 +05:30
|
|
|
// Return null - the global provider handles the loading UI
|
|
|
|
|
return null;
|
2025-04-07 23:47:06 -07:00
|
|
|
};
|
|
|
|
|
|
2025-07-27 10:05:37 -07:00
|
|
|
export default TokenHandler;
|