在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

这很奇怪,因为我可以对服务器进行其他查询而没有C​​ORS问题。我认为这与我在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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...