Make requests with session data #4102
-
Hey! I'm not sure how to do this: I have a custom import { useSession } from "next-auth/react";
import { useState } from "react";
export default () => {
const { data } = useSession();
const [list, setList] = useState();
const updateList = async () => {
const res = await fetch("URL", {
headers: { Authorization: `Bearer ${data.accessToken}` }
});
setList(await res.json());
}
useEffect(() => updateList(), []);
return <ul>{list.forEach(l => <li>{l}</li>)}</ul>;
} Sometimes, How would I do this? Also, Is it possible to make this request when the user signs in and store it alongside the other session info? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, the error you're getting indicates that you need to handle your side effect differently. You cannot return
You can try to use the JWT & session callbacks. |
Beta Was this translation helpful? Give feedback.
Hi, the error you're getting indicates that you need to handle your side effect differently. You cannot return
<Loading/>
early and calluseEffect
after; that would be a violation of The Rule of HooksBesides, you probably need to include
data
in the hook dependency array. Otherwise, the callback would only run once.You can try to use the JWT & session callbacks.