HttpClient include credentials ( cookie )
S0PEX opened this issue · 2 comments
S0PEX commented
Hey,
I am currently migrating my project from the apollo client and was running in an error where I can't set my authentication cookie.
With Apollo I was able to set
const client = new ApolloClient({
uri: 'https://myapi.local/graphql',
cache: new InMemoryCache(),
// Enable sending cookies over cross-origin requests
credentials: 'include'
});
it seems that this option is missing or how would I accomplish this.
Edit: It seems that this behavior is caused because my client cookie isn't available at the server ( obviously ).
Would it be better to send it to the server or somehow turn off ssr for the queries.
Edit2: I am currently trying to pass the headers from ctx.req
to the client without success.
export function getStore(snapshot = null, ctx = null): ModelCreationType<RootStoreType> {
if (isServer || !store) {
store = RootStore.create(undefined, {
gqlHttpClient: createHttpClient('http://localhost:5000/graphql', {
credentials: 'include',
headers: ctx?.req ? { cookie: ctx.req.headers.cookie } : undefined
}),
ssr: true,
})
}
if (snapshot) {
applySnapshot(store, snapshot)
}
return store
}
export default class MyApp extends App<any, any> {
store: ModelCreationType<RootStoreType>
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static async getInitialProps({ Component, ctx, router }) {
const store = getStore()
const pageProps =
(Component.getInitialProps &&
(await Component.getInitialProps({ ...ctx, store }))) ||
{}
let storeSnapshot
if (isServer) {
// eslint-disable-next-line no-console
console.log(ctx)
const tree = <MyApp {...{ Component, router, pageProps, store }} />
await getDataFromTree(tree, store)
storeSnapshot = getSnapshot<RootStoreType>(store, ctx)
}
return { pageProps, storeSnapshot }
}
Regards Artur