如何解决在Graphql-yoga服务器上设置Cookie会引发CORS错误,但不是
编辑:绝对不是CORS。就像只是无视我将令牌写入cookie的尝试...我也很难让它抛出一个我认为有用的错误...我会不断地向墙上扔飞镖,直到有道理为止。
我有一个带阿波罗客户端前端的graphql-yoga服务器。我在服务器上使用cookie解析器在浏览器中存储Microsoft Graph身份验证令牌。我收到一个错误,显示为CORS,但我不知道为什么。
Access to fetch at 'https://api.mydomain.store/' from origin 'https://mydomain.store' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs,set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Error: Network error: Failed to fetch
这很奇怪,因为我可以对服务器进行其他查询而没有CORS问题。我认为这与我在Cookie上设置选项的方式有关,但我认为我做得正确:
//saveCookies
function saveValuesToCookie(token: any,res: Response) {
// Parse the identity token
const user: any = jwt.decode(token.token.id_token);
const isDev = process.env.NODE_ENV === 'development';
console.log('isDev: ',isDev);
const cookieOpts: CookieOptions = {
domain: isDev ? 'localhost' : 'mydomain.store',maxAge: 1000 * 60 * 60 * 24 * 365,httpOnly: true,sameSite: isDev ? 'lax' : true,secure: isDev ? false : true,};
// Save the access token in a cookie
res.cookie('graph_access_token',token.token.access_token,cookieOpts);
//save the users email to a cookie
res.cookie('graph_user_email',user.preferred_username,cookieOpts);
// Save the refresh token in a cookie
res.cookie('graph_refresh_token',token.token.refresh_token,cookieOpts);
res.cookie('graph_user_id',user.oid,cookieOpts);
// Save the token expiration tiem in a cookie
res.cookie(
'graph_token_expires',token.token.expires_at.getTime(),cookieOpts
);
}
根据我到目前为止所看到的资源,这对于当前的浏览器规则来说似乎是正确的。
所以我看一下该突变的位置:
//apollo react authorize mutation
const AUTHORIZE_MUTATION = gql`
mutation($code: String!) {
authorize(code: $code) {
id
email
displayName
studentFaculty
}
}
`;
function AuthorizePage() {
const router = useRouter();
const { code } = router.query; //grab the code from the query params
const [authorize,{ loading }] = useMutation(AUTHORIZE_MUTATION,{
variables: { code },// refetchQueries: [{ query: ME_QUERY }],onError: (error) => {
console.error(error); //**this is where the CORS error originates**
return Router.push('/');
},update: (_,{ data }) => {
console.log(data); //** never gets this far **
},});
useEffect(() => {
if (router.query.code) {
authorize();
}
},[router.query]);
if (loading) return <Loading />;
return <Loading />;
}
我很茫然。谁能发现什么地方出问题或将我指向其他地方?对我而言,最难的部分是代码可以在端口3000上的localhost服务客户端和端口4000上的服务器上完美运行。 我认为此错误不是真正的CORS问题,因为我可以进行未经身份验证的查询没有问题。
感谢您的时间!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。