【发布时间】:2021-08-25 08:14:04
【问题描述】:
所以我有一个在 localhost:5000 运行的 Express / TypeGraphql 后端和在 localhost:3000 运行的 Next / React 应用程序。我决定将 apollo-server 用于 graphql API,将 apollo-client 用于前端。成功登录后,我将 httpOnly cookie 存储在网络浏览器中。
我想要做的是,在每个请求中获取 cookie,并在 oauth2 令牌有效时授权用户。即使请求是有效的,并且我得到了查询,cookie 也显示为 null。另外,我在控制台中没有错误。
我在这里保存 cookie =>
server.ts
app.get('/auth/google/callback', function (req, res, next) {
passport.authenticate('google', { session: false }, (err, user, info) => {
if (err) return next(err);
if (!user) return res.redirect('/');
res.cookie('login_cookie', JSON.stringify(info), {
secure: false,
httpOnly: true,
expires: dayjs().add(30, 'days').toDate(),
sameSite: false,
});
return res.redirect('http://localhost:3000/home');
})(req, res, next);
});
在这里,我在请求后将 cookie 记录到控制台 =>
解析器.ts
@Query(() => [User])
async users(@Ctx() ctx: Context) {
console.log('cookies ', ctx.req.cookies);
return await ctx.prisma.user.findMany();
}
最后,这是我的 apollo 客户端配置 =>
apollo-client.ts
const client = new ApolloClient({
cache: new InMemoryCache(),
credentials: "include",
uri: "http://localhost:5000/graphql",
});
每次请求后,我的控制台都会显示 cookies: [Object: null prototype] {},即使我在浏览器的 cookie 存储中看到它们。
request.ts
export async function getServerSideProps() {
const { data } = await client.query({
query: gql`
query allUsers {
users {
id
username
}
}
`,
});
return {
props: {
users: data.users,
},
};
}
【问题讨论】:
-
你是否在后端发出 GraphQL 请求,例如到服务器端呈现某些视图?我认为您必须将 cookie 转发到您的 GraphQL 后端。也许github.com/valeriangalliat/fetch-cookie 能帮上忙?
-
我编辑了这个问题。我这样提出请求,它也应该在我向后端发出的每个请求中自动发送 cookie。我不认为我需要一个包来做到这一点。
标签: javascript cookies graphql apollo-client