Next.js Discord

Discord Forum

Why the session.assessToken value is not getting ?

Unanswered
Flemish Giant posted this in #help-forum
Open in Discord
Flemish GiantOP
The value of session get log into the console then why the value not caught in the useEffect
here is the code
const pathName = useSearchParams();
const [shareCards, setShareCards] = useState<cardInterface[]>([]);
const [error, setError] = useState("");
const Cardtoken = pathName.get("Cardtoken");
const { data: session } = useSession();
console.log("🚀 ~ ShareContent ~ useSession:", session);

useEffect(() => {
// Only proceed if Cardtoken and token are available
if (Cardtoken && session?.accessToken) {
const fetchSharedCard = async () => {
try {
console.log("Before the API call");
const res = await axios.get(http://localhost:3001/content/share, {
headers: {
"Content-Type": "application/json",
Authorization: Bearer ${session?.accessToken},
},
params: {
Cardtoken,
},
});
setShareCards([res.data.shareCardData]);
} catch (error) {
console.error("Error fetching shared card:", error);
setError("Error in getting the card");
}
};

fetchSharedCard();
} else {
if (!Cardtoken) setError("Invalid URL - Missing token");
if (!session?.accessToken) setError("Token is - Missing token");
}
}, [Cardtoken, session]);

if (error) {
return <h1>{error}</h1>;
}

0 Replies