Skip to content

Instantly share code, notes, and snippets.

@davestacey
Created October 23, 2018 14:47
Show Gist options
  • Save davestacey/623608bbd5ee34e1f9683253c1e819be to your computer and use it in GitHub Desktop.
Save davestacey/623608bbd5ee34e1f9683253c1e819be to your computer and use it in GitHub Desktop.
with-apollo-auth

From examples/with-apollo-auth

  • initApollo
//examples/with-apollo-auth/lib/withApollo.js
static async getInitialProps (ctx) {
const { Component, router, ctx: { req, res } } = ctx
const token = parseCookies(req).token
const apollo = initApollo({}, {
getToken: () => token
})
ctx.ctx.apolloClient = apollo
function create (initialState, { getToken }) {
const httpLink = createHttpLink({
uri: 'https://api.graph.cool/simple/v1/cj5geu3slxl7t0127y8sity9r',
credentials: 'same-origin'
})
const authLink = setContext((_, { headers }) => {
const token = getToken()
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment