({
redirectPath = "/dashboard",
tokenParamName = "token",
storageKey = "surfsense_bearer_token",
}: TokenHandlerProps)
| 21 | * @param storageKey - Key to use when storing in localStorage (default: 'surfsense_bearer_token') |
| 22 | */ |
| 23 | const TokenHandler = ({ |
| 24 | redirectPath = "/dashboard", |
| 25 | tokenParamName = "token", |
| 26 | storageKey = "surfsense_bearer_token", |
| 27 | }: TokenHandlerProps) => { |
| 28 | const searchParams = useSearchParams(); |
| 29 | |
| 30 | useEffect(() => { |
| 31 | // Only run on client-side |
| 32 | if (typeof window === "undefined") return; |
| 33 | |
| 34 | // Get token from URL parameters |
| 35 | const token = searchParams.get(tokenParamName); |
| 36 | |
| 37 | if (token) { |
| 38 | try { |
| 39 | // Track login success for OAuth flows (e.g., Google) |
| 40 | // Local login already tracks success before redirecting here |
| 41 | const alreadyTracked = sessionStorage.getItem("login_success_tracked"); |
| 42 | if (!alreadyTracked) { |
| 43 | // This is an OAuth flow (Google login) - track success |
| 44 | trackLoginSuccess("google"); |
| 45 | } |
| 46 | // Clear the flag for future logins |
| 47 | sessionStorage.removeItem("login_success_tracked"); |
| 48 | |
| 49 | // Store token in localStorage using both methods for compatibility |
| 50 | localStorage.setItem(storageKey, token); |
| 51 | setBearerToken(token); |
| 52 | |
| 53 | // Check if there's a saved redirect path from before the auth flow |
| 54 | const savedRedirectPath = getAndClearRedirectPath(); |
| 55 | |
| 56 | // Use the saved path if available, otherwise use the default redirectPath |
| 57 | const finalRedirectPath = savedRedirectPath || redirectPath; |
| 58 | |
| 59 | // Redirect to the appropriate path |
| 60 | window.location.href = finalRedirectPath; |
| 61 | } catch (error) { |
| 62 | console.error("Error storing token in localStorage:", error); |
| 63 | // Even if there's an error, try to redirect to the default path |
| 64 | window.location.href = redirectPath; |
| 65 | } |
| 66 | } |
| 67 | }, [searchParams, tokenParamName, storageKey, redirectPath]); |
| 68 | |
| 69 | return ( |
| 70 | <div className="flex items-center justify-center min-h-[200px]"> |
| 71 | <p className="text-gray-500">Processing authentication...</p> |
| 72 | </div> |
| 73 | ); |
| 74 | }; |
| 75 | |
| 76 | export default TokenHandler; |
nothing calls this directly
no test coverage detected