mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-14 22:52:15 +02:00
check search space access before redirect
This commit is contained in:
parent
2f8919baef
commit
348898b08b
1 changed files with 48 additions and 26 deletions
|
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
import { useRouter, useSearchParams } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
import { membersApiService } from "@/lib/apis/members-api.service";
|
||||||
import { getAndClearRedirectPath, setBearerToken } from "@/lib/auth-utils";
|
import { getAndClearRedirectPath, setBearerToken } from "@/lib/auth-utils";
|
||||||
import { trackLoginSuccess } from "@/lib/posthog/events";
|
import { trackLoginSuccess } from "@/lib/posthog/events";
|
||||||
|
import { queryClient } from "@/lib/query-client/client";
|
||||||
|
|
||||||
interface TokenHandlerProps {
|
interface TokenHandlerProps {
|
||||||
redirectPath?: string; // Default path to redirect after storing token (if no saved path)
|
redirectPath?: string; // Default path to redirect after storing token (if no saved path)
|
||||||
|
|
@ -36,34 +38,54 @@ const TokenHandler = ({
|
||||||
const token = searchParams.get(tokenParamName);
|
const token = searchParams.get(tokenParamName);
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
try {
|
const handleAuth = async () => {
|
||||||
// Track login success for OAuth flows (e.g., Google)
|
try {
|
||||||
// Local login already tracks success before redirecting here
|
// Track login success for OAuth flows (e.g., Google)
|
||||||
const alreadyTracked = sessionStorage.getItem("login_success_tracked");
|
// Local login already tracks success before redirecting here
|
||||||
if (!alreadyTracked) {
|
const alreadyTracked = sessionStorage.getItem("login_success_tracked");
|
||||||
// This is an OAuth flow (Google login) - track success
|
if (!alreadyTracked) {
|
||||||
trackLoginSuccess("google");
|
// This is an OAuth flow (Google login) - track success
|
||||||
|
trackLoginSuccess("google");
|
||||||
|
}
|
||||||
|
// Clear the flag for future logins
|
||||||
|
sessionStorage.removeItem("login_success_tracked");
|
||||||
|
|
||||||
|
// Store token in localStorage using both methods for compatibility
|
||||||
|
localStorage.setItem(storageKey, token);
|
||||||
|
setBearerToken(token);
|
||||||
|
|
||||||
|
// Clear any cached data from previous sessions
|
||||||
|
queryClient.clear();
|
||||||
|
|
||||||
|
// Check if there's a saved redirect path from before the auth flow
|
||||||
|
const savedRedirectPath = getAndClearRedirectPath();
|
||||||
|
|
||||||
|
// Check if saved path contains a search space ID and verify access
|
||||||
|
const searchSpaceMatch = savedRedirectPath?.match(/^\/dashboard\/(\d+)/);
|
||||||
|
if (searchSpaceMatch && savedRedirectPath) {
|
||||||
|
const searchSpaceId = Number(searchSpaceMatch[1]);
|
||||||
|
try {
|
||||||
|
await membersApiService.getMyAccess({ search_space_id: searchSpaceId });
|
||||||
|
router.push(savedRedirectPath);
|
||||||
|
return;
|
||||||
|
} catch {
|
||||||
|
// User doesn't have access, fall through to default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the saved path if available, otherwise use the default redirectPath
|
||||||
|
const finalRedirectPath = savedRedirectPath || redirectPath;
|
||||||
|
|
||||||
|
// Redirect to the appropriate path
|
||||||
|
router.push(finalRedirectPath);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error storing token in localStorage:", error);
|
||||||
|
// Even if there's an error, try to redirect to the default path
|
||||||
|
router.push(redirectPath);
|
||||||
}
|
}
|
||||||
// Clear the flag for future logins
|
};
|
||||||
sessionStorage.removeItem("login_success_tracked");
|
|
||||||
|
|
||||||
// Store token in localStorage using both methods for compatibility
|
handleAuth();
|
||||||
localStorage.setItem(storageKey, token);
|
|
||||||
setBearerToken(token);
|
|
||||||
|
|
||||||
// Check if there's a saved redirect path from before the auth flow
|
|
||||||
const savedRedirectPath = getAndClearRedirectPath();
|
|
||||||
|
|
||||||
// Use the saved path if available, otherwise use the default redirectPath
|
|
||||||
const finalRedirectPath = savedRedirectPath || redirectPath;
|
|
||||||
|
|
||||||
// Redirect to the appropriate path
|
|
||||||
router.push(finalRedirectPath);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error storing token in localStorage:", error);
|
|
||||||
// Even if there's an error, try to redirect to the default path
|
|
||||||
router.push(redirectPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [searchParams, tokenParamName, storageKey, redirectPath, router]);
|
}, [searchParams, tokenParamName, storageKey, redirectPath, router]);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue