一个关于将JWT刷新标记设置为cookie时什么是对什么是错的简短问题。
如果我的API将响应报头中的refresh token发送到ApolloServer,那么Set-Cookie报头将自动通过我的Apollo-server传递给te客户端(apollo- client )。或者,我应该只期望从我的应用程序接口中获得一个主体数据集,包括token + refreshToke,我将通过apollo-server中间件提取它,并将其作为cookie添加到客户机响应中。
有什么想法或建议吗?
发布于 2019-11-17 01:21:21
Cookie必须由服务器设置。这将使用express response对象来完成。确保可以从您的gql上下文访问它。
res.cookie('X-Refresh-Token', refreshToken, {
maxAge: 900000, // 15 minutes in milliseconds
httpOnly: true, // ensure the cookie will not be exposed
secure: true, // set this in production to ensure HTTPS
sameOrigin: 'none' // set this in production if the server is separate domain
})然后,您的客户端必须启用凭据。对于apollo-boost,这将是:
new ApolloClient({
uri: 'http://localhost:8080',
credentials: 'include', // 'same-origin' if your server is same domain
})要确保在GraphQL游乐场中传递cookies:
new ApolloServer({
context: ({ req, res }) => ({ req, res }),
playground: {
settings: {
'request.credentials': 'include',
},
},
})如果你还没有用过它,cookie parser middleware可能会派上用场。
https://stackoverflow.com/questions/58890996
复制相似问题