我正在尝试用passport.js设置graphql,在服务器端似乎一切正常,但在客户端,当我检查哪个用户当前登录(req.user)时,我没有定义,而在服务器端,我得到了当前用户
我在localhost:4000上运行我的服务器,在localhost:3000上运行客户机。
我的一些配置如下:
我已经尝试在客户端和服务器端更改凭据
服务器配置
app.use(
session({
resave: false,
saveUninitialized: false,
secret,
store: new MongoStore({
url: MONGO_URI,
autoReconnect: true
}),
cookie: {
maxAge: 1000 * 60 * 60 * 2
}
})
);
const server = new ApolloServer({
typeDefs,
resolvers,
// required for passport req.user access
playground: { settings: { 'request.credentials': 'include' } },
// so we have access to app req,res through graphql
context: ({ req, res }) => ({
req,
res
})
});
server.applyMiddleware({ app });客户端配置
const cache = new InMemoryCache();
const link = new HttpLink({
uri: 'http://localhost:4000/graphql',
credentials: 'same-origin',
});
const client = new ApolloClient({
cache,
link,
});我希望能够在客户端访问获取当前登录的用户(react)
发布于 2019-03-29 08:50:23
为了防止任何人遇到同样的问题,我们需要解决几个问题才能使其正常工作:
在客户端中:
const link = createHttpLink({
uri: 'http://localhost:4000/graphql',
credentials: 'include',
});在服务器中:
// pass types and resolvers
const server = new ApolloServer({
typeDefs,
resolvers,
// required for passport req.user access
playground: { settings: { 'request.credentials': 'include' } },
// so we have access to app req,res through graphql
context: ({ req, res }) => ({
req,
res
})
});
server.applyMiddleware({
app,
cors: { origin: 'http://localhost:3000', credentials: true }
});它对我很有效:)
https://stackoverflow.com/questions/55349719
复制相似问题