'How do I include my credentials in getServerSideProps in Nextjs? [duplicate]
In the async function getServerSideProps I need to make a fetch from the back-end where I include my credentials to instantiate a session. I have the following code:
export async function getServerSideProps({ query: { id } }) {
// Instantiate
await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/workflow/${id}`, {
method: 'GET',
credentials: 'include',
})
.then((response) => console.log('Instantiate...', response))
.catch((error) => console.error('Instantiation error', error));
}
The URL and all seem to work, but the console shows a 500 error because the credentials don't seem to be included in the fetch. When pasting the exact call in browser console/Postman, the response is a 200 code, so back-end seems to work correctly.
Solution 1:[1]
I moved the fetch from getServerSideProps to componentDidMount, which fixed the problem. Only thing to take in mind is to initiate the state with empty values, since the order of calls is:
- constructor
- render
- componentDidMount
- render (if state update)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alex Janse |