Node.js nuxtjs apollo客户端未设置授权标头
我正在尝试使用nuxtjs创建登录功能,并在后端使用。我想将令牌从前端(nuxtjs/apollo客户端)传递到后端(nodejs/apollo服务器)。Node.js nuxtjs apollo客户端未设置授权标头,node.js,authorization,nuxt.js,apollo,apollo-client,Node.js,Authorization,Nuxt.js,Apollo,Apollo Client,我正在尝试使用nuxtjs创建登录功能,并在后端使用。我想将令牌从前端(nuxtjs/apollo客户端)传递到后端(nodejs/apollo服务器)。 登录功能(前端) nuxtjs.config(前端) 浏览器开发工具中的Cookie 索引文件(后端) const-app=express() 常数corsConfig={ 来源:'http://127.0.0.1:3000', 方法:“获取、头部、放置、修补、发布、删除、选项”, 凭据:正确 } 应用程序使用(cors(corsConf
登录功能(前端) nuxtjs.config(前端) 浏览器开发工具中的Cookie
索引文件(后端)
const-app=express()
常数corsConfig={
来源:'http://127.0.0.1:3000',
方法:“获取、头部、放置、修补、发布、删除、选项”,
凭据:正确
}
应用程序使用(cors(corsConfig))
应用程序使用(摩根(‘开发’))
const getMe=async req=>{
const token=req.headers.authorization/{
如果(请求){
const me=等待getMe(请求)
返回{
模型,
我
秘密:process.env.secret,
装载机:{
用户:新数据加载器(键=>
装载机.用户.批量用户(键,型号),
),
},
}
}
},
})
server.applyMiddleware({
应用程序,
路径:'/graphql',
科尔斯:错
})
const httpServer=http.createServer(应用程序)
installSubscriptionHandlers(httpServer)
const port=process.env.port | 8000
sync({force:true})。然后(async()=>{
createUsers(新日期())
httpServer.listen({port},()=>{
console.log(`Apollo Server onhttp://localhost:${port}/graphql`)
})
})
令牌保存在名为“阿波罗令牌”的cookie中。但是,未设置格式为“承载令牌”的创作标头。根据apollo客户端文档,这应该自动设置(https://github.com/nuxt-community/apollo-module#authenticationtype-字符串(可选默认承载)。
我错过了什么?我将非常感谢任何形式的帮助!
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('/feed')
} catch (err) {
// Error message
}
}
apollo: {
clientConfigs: {
default: {
httpEndpoint: 'http://localhost:8000/graphql',
wsEndpoint: 'ws://localhost:8000/graphql',
authenticationType: 'Bearer',
httpLinkOptions: {
credentials: 'include'
},
}
}
const app = express()
const corsConfig = {
origin: 'http://127.0.0.1:3000',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true
}
app.use(cors(corsConfig))
app.use(morgan('dev'))
const getMe = async req => {
const token = req.headers.authorization // <========
console.log(token) // returns 'Bearer undefined'
if (token) {
try {
return await jwt.verify(token, process.env.SECRET)
} catch (e) {
// Error message
}
}
}
const server = new ApolloServer({
introspection: true,
playground: true,
typeDefs: schema,
resolvers,
context: async ({ req }) => {
if (req) {
const me = await getMe(req)
return {
models,
me,
secret: process.env.SECRET,
loaders: {
user: new DataLoader(keys =>
loaders.user.batchUsers(keys, models),
),
},
}
}
},
})
server.applyMiddleware({
app,
path: '/graphql',
cors: false
})
const httpServer = http.createServer(app)
server.installSubscriptionHandlers(httpServer)
const port = process.env.PORT || 8000
sequelize.sync({ force: true }).then(async () => {
createUsers(new Date())
httpServer.listen({ port }, () => {
console.log(`Apollo Server on http://localhost:${port}/graphql`)
})
})