Graphql Nuxt/Apollo-设置授权标头
我正在尝试使用nuxtjs创建登录功能,并在后端使用。我想将令牌从前端(nuxtjs/apollo客户端)传递到后端(nodejs/apollo服务器)。Graphql Nuxt/Apollo-设置授权标头,graphql,apollo,apollo-client,nuxtjs,Graphql,Apollo,Apollo Client,Nuxtjs,我正在尝试使用nuxtjs创建登录功能,并在后端使用。我想将令牌从前端(nuxtjs/apollo客户端)传递到后端(nodejs/apollo服务器)。 登录功能 async signin () { const email = this.email const password = this.password try { const res = await this.$apollo.mutate({ mutation: signIn, variab
登录功能
async signin () {
const email = this.email
const password = this.password
try {
const res = await this.$apollo.mutate({
mutation: signIn,
variables: {
email,
password
}
}).then(({ data }) => data && data.signIn)
const token = res.token
await this.$apolloHelpers.onLogin(token)
this.$router.push('/')
} catch (err) {
// Error message
}
}
nuxtjs.config
import Cookies from 'js-cookie'
const token = Cookies.get('apollo-token')
apollo: {
clientConfigs: {
httpEndpoint: 'http://localhost:8000/graphql',
httpLinkOptions: {
credentials: 'include',
headers: {
'authorization': token ? token : '' // passing the token on this line fails it returns ''
}
}
}
},
浏览器开发工具中的Cookie我无法将令牌传递到授权标头。我还尝试使用localStorage(`authorization':(process.client)?localStorage.getItem('token'`):''''')来实现这一点,但由于服务器端呈现,这似乎不可能实现。
令牌保存在名为“阿波罗令牌”的cookie中。但是,未设置格式为“承载令牌”的创作标头。根据apollo客户端文档,这应该自动设置()。
我将非常感谢任何形式的帮助 我遇到了同样的问题, 这很奇怪,过了一段时间它就开始自动工作了 我的配置是
httpEndpoint: process.env.GRAPHQL_URL,
httpLinkOptions: {
credentials: 'same-origin'
},
watchLoading: '~/plugins/apollo-watch-loading-handler.js',
errorHandler: '~/plugins/apollo-error-handler.js',
wsEndpoint: null, // process.env.STRAPI_GRAPHQL_WS_URL,
tokenName: 'session-token',
authenticationType: 'Bearer',
// Enable Automatic Query persisting with Apollo Engine
persisting: false,
// Use websockets for everything (no HTTP)
// You need to pass a `wsEndpoint` for this to work
websocketsOnly: false
此外,还应将配置包装为“默认”
如果这不起作用,试试看
// in default config object
getAuth: () => token ? `Bearer ${token}`: '',
请告诉我它是否有效我遇到了同样的问题, 这很奇怪,过了一段时间它就开始自动工作了 我的配置是
httpEndpoint: process.env.GRAPHQL_URL,
httpLinkOptions: {
credentials: 'same-origin'
},
watchLoading: '~/plugins/apollo-watch-loading-handler.js',
errorHandler: '~/plugins/apollo-error-handler.js',
wsEndpoint: null, // process.env.STRAPI_GRAPHQL_WS_URL,
tokenName: 'session-token',
authenticationType: 'Bearer',
// Enable Automatic Query persisting with Apollo Engine
persisting: false,
// Use websockets for everything (no HTTP)
// You need to pass a `wsEndpoint` for this to work
websocketsOnly: false
此外,还应将配置包装为“默认”
如果这不起作用,试试看
// in default config object
getAuth: () => token ? `Bearer ${token}`: '',
请让我知道它是否有效