我正在使用MERN堆栈构建FullStack应用程序。我可以使用带有快速会话的cookie来验证用户身份。我对react的所有请求已经在标头上发送了cookie,到目前为止,一切都可以正常进行。现在我想基于会话数据基于响应来呈现组件,但是我的cookie是HttpOnly,我想保持这种方式。因此,如果我无法使用JS访问会话cookie,应该如何获得授权和登录状态?我现在应该如何处理?我搜索了很多东西,却找不到一个很好的答案。
节点:
app.use(
session({
store: new MongoStore({ mongooseConnection: mongoose.connection }),
name: SESSION_NAME,
secret: SESSION_SECRET,
resave: true,
rolling: true,
saveUninitialized: false,
cookie: {
maxAge: parseInt(SESSION_MAX_AGE, 10),
sameSite: true,
httpOnly: true,
secure: !NODE_ENV.trim() === 'development'
}
})
);
const server = new ApolloServer({...});
server.applyMiddleware({
app,
cors: {
credentials: true,
origin: 'http://localhost:3000'
}
});
客户端:(使用阿波罗)
import ApolloClient from 'apollo-boost';
const client = new ApolloClient({
uri: 'http://localhost:8080/graphql',
credentials: 'include'
});
export default client;