mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-22 23:31:12 +02:00
- Updated all hooks to import and use AUTH_TOKEN_KEY constant from lib/constants - Updated all components to use AUTH_TOKEN_KEY instead of hardcoded string - Updated all atoms to use AUTH_TOKEN_KEY - Updated all app pages to use AUTH_TOKEN_KEY - Made CORS origin configurable via CORS_ORIGINS environment variable - Added CORS_ORIGINS to .env.example with documentation This improves maintainability by centralizing the auth token key string, preventing typos and making it easier to change the key name if needed.
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
"use client";
|
|
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
|
import { useEffect } from "react";
|
|
import { baseApiService } from "@/lib/apis/base-api.service";
|
|
import { AUTH_TOKEN_KEY } from "@/lib/constants";
|
|
|
|
interface TokenHandlerProps {
|
|
redirectPath?: string; // Path to redirect after storing token
|
|
tokenParamName?: string; // Name of the URL parameter containing the token
|
|
storageKey?: string; // Key to use when storing in localStorage
|
|
}
|
|
|
|
/**
|
|
* Client component that extracts a token from URL parameters and stores it in localStorage
|
|
*
|
|
* @param redirectPath - Path to redirect after storing token (default: '/')
|
|
* @param tokenParamName - Name of the URL parameter containing the token (default: 'token')
|
|
* @param storageKey - Key to use when storing in localStorage (default: 'auth_token')
|
|
*/
|
|
const TokenHandler = ({
|
|
redirectPath = "/",
|
|
tokenParamName = "token",
|
|
storageKey = AUTH_TOKEN_KEY,
|
|
}: TokenHandlerProps) => {
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
|
|
useEffect(() => {
|
|
// Only run on client-side
|
|
if (typeof window === "undefined") return;
|
|
|
|
// Get token from URL parameters
|
|
const token = searchParams.get(tokenParamName);
|
|
|
|
if (token) {
|
|
try {
|
|
// Store token in localStorage
|
|
localStorage.setItem(storageKey, token);
|
|
|
|
// Update the baseApiService singleton with the new token
|
|
// This ensures subsequent API calls use the correct token
|
|
baseApiService.setBearerToken(token);
|
|
|
|
// Redirect to specified path
|
|
router.push(redirectPath);
|
|
} catch (error) {
|
|
console.error("Error storing token in localStorage:", error);
|
|
}
|
|
}
|
|
}, [searchParams, tokenParamName, storageKey, redirectPath, router]);
|
|
|
|
return (
|
|
<div className="flex items-center justify-center min-h-[200px]">
|
|
<p className="text-gray-500">Processing authentication...</p>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default TokenHandler;
|