mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
FIX with-apollo-auth example token access (#4771)
in some cases access to the token doesn't work in client side. this access to the token once in getInitialProps method and pass it down using props
This commit is contained in:
parent
932e72b965
commit
ae7e53239a
|
@ -22,8 +22,9 @@ export default App => {
|
||||||
|
|
||||||
static async getInitialProps(ctx) {
|
static async getInitialProps(ctx) {
|
||||||
const { Component, router, ctx: { req, res } } = ctx
|
const { Component, router, ctx: { req, res } } = ctx
|
||||||
|
const token = parseCookies(req).token
|
||||||
const apollo = initApollo({}, {
|
const apollo = initApollo({}, {
|
||||||
getToken: () => parseCookies(req).token
|
getToken: () => token
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.ctx.apolloClient = apollo
|
ctx.ctx.apolloClient = apollo
|
||||||
|
@ -69,7 +70,8 @@ export default App => {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...appProps,
|
...appProps,
|
||||||
apolloState
|
apolloState,
|
||||||
|
token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +80,7 @@ export default App => {
|
||||||
// `getDataFromTree` renders the component first, the client is passed off as a property.
|
// `getDataFromTree` renders the component first, the client is passed off as a property.
|
||||||
// After that rendering is done using Next's normal rendering pipeline
|
// After that rendering is done using Next's normal rendering pipeline
|
||||||
this.apolloClient = initApollo(props.apolloState, {
|
this.apolloClient = initApollo(props.apolloState, {
|
||||||
getToken: () => parseCookies().token
|
getToken: () => props.token
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue