A
A
Alexey Yakovlev2022-02-05 19:16:17
HTTP Cookies
Alexey Yakovlev, 2022-02-05 19:16:17

How to refer to Cookies in getServerSideProps?

export async function getServerSideProps({ query }: NextPageContext) {
    const response = await fetch(`http://localhost:5000/api/users/${query.name}`, {
        method: "GET",
        headers: {
            Authorization: `Bearer ${Cookies.get("token")}`
        }
    });
    const user = await response.json();
    return { props: { user } };
}


Cookies.get("token") = undefined, although everything is in the cookie

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Karo, 2022-02-05
@aleshaykovlev

It appears that Cookies is a lib to receive cookies in the browser via the browser api.
And this code is executed on the server - cookie data must be obtained from the user's request.
Data about it should be in context.
Here is a relevant question from SO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question