Replies: 2 comments 2 replies
-
@jinsley8 did you ever fix this? I have the same issue. I logged the Basically, the cookies and the headers are not passed at all. |
Beta Was this translation helpful? Give feedback.
2 replies
-
I figured it out, I added It now passes the cookie with the request from the server to API route. // apiClient.ts
const baseConfig: AxiosRequestConfig = {
baseURL: API_DOMAIN,
withCredentials: true,
headers: {
'Content-Type': 'application/json',
},
};
const apiClient = axios.create(baseConfig); function getAuthSession(
...args:
| [GetServerSidePropsContext['req'], GetServerSidePropsContext['res']]
| [NextApiRequest, NextApiResponse]
| []
) {
return getServerSession(...args, authOptions);
};
// page.ts
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const queryClient = new QueryClient();
const session = await getAuthSession(ctx.req, ctx.res);
if (session) {
if (ctx.req.headers.cookie) {
// pass cookie
apiClient.defaults.headers.Cookie = ctx.req.headers.cookie;
}
await Promise.all([
queryClient.prefetchQuery(queries.tasks.all(0, { field: 'createdAt', sort: 'desc' })),
queryClient.prefetchQuery(queries.notifications.all(session.user.id)),
]);
}
const extraProps = {
session,
dehydratedState: dehydrate(queryClient),
};
// this is just a custom role checker
return await checkUserSessionProtected(ctx, extraProps, session);
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am prefetching data with Next.js pages router but the query functions need access to the session. Should I be passing the session to the queries or can the queryClient get access somehow?
Each query function calls an endpoint which results in a 401 error if there is no session so the prefetch results in an error then the query on the client has access to the session and finally loads the data.
Beta Was this translation helpful? Give feedback.
All reactions