SurfSense/surfsense_web/components/TokenHandler.tsx

88 lines
3 KiB
TypeScript
Raw Normal View History

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";
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
2026-04-07 20:28:07 +02:00
import { getAndClearRedirectPath, setBearerToken, setRefreshToken } from "@/lib/auth-utils";
2026-01-09 15:00:15 +02:00
import { trackLoginSuccess } from "@/lib/posthog/events";
2025-04-07 23:47:06 -07:00
interface TokenHandlerProps {
redirectPath?: string; // Default path to redirect after storing token (if no saved path)
2025-07-27 10:05:37 -07:00
tokenParamName?: string; // Name of the URL parameter containing the token
storageKey?: string; // Key to use when storing in localStorage (kept for backwards compatibility)
2025-04-07 23:47:06 -07:00
}
/**
* Client component that extracts a token from URL parameters and stores it in localStorage
* After storing the token, it redirects the user back to the page they were on before
* being redirected to login (if available), or to the default redirectPath.
2025-07-27 10:05:37 -07: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')
* @param storageKey - Key to use when storing in localStorage (default: 'surfsense_bearer_token')
2025-04-07 23:47:06 -07:00
*/
const TokenHandler = ({
redirectPath = "/dashboard",
2025-07-27 10:05:37 -07:00
tokenParamName = "token",
storageKey = "surfsense_bearer_token",
2025-04-07 23:47:06 -07:00
}: TokenHandlerProps) => {
// Always show loading for this component - spinner animation won't reset
2026-01-27 15:28:30 +05:30
useGlobalLoadingEffect(true);
2025-07-27 10:05:37 -07:00
useEffect(() => {
if (typeof window === "undefined") return;
const run = async () => {
const params = new URLSearchParams(window.location.search);
const token = params.get(tokenParamName);
const refreshToken = params.get("refresh_token");
2025-07-27 10:05:37 -07:00
if (token) {
try {
const alreadyTracked = sessionStorage.getItem("login_success_tracked");
if (!alreadyTracked) {
trackLoginSuccess("google");
}
sessionStorage.removeItem("login_success_tracked");
localStorage.setItem(storageKey, token);
setBearerToken(token);
if (refreshToken) {
setRefreshToken(refreshToken);
}
// 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));
}
}
} catch {
// non-critical
}
}
const savedRedirectPath = getAndClearRedirectPath();
const finalRedirectPath = savedRedirectPath || redirectPath;
window.location.href = finalRedirectPath;
} catch (error) {
console.error("Error storing token in localStorage:", error);
window.location.href = redirectPath;
}
}
};
run();
}, [tokenParamName, storageKey, redirectPath]);
2025-07-27 10:05:37 -07:00
// 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;