Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

axios - GitHub API get request is automatically sending x-auth-token

I have a react app with custom user-auth built using json web token. When the user is authenticated during login, it stores the jwt in localstorage. Elsewhere in the application I make a completely unrelated axios call to GitHub to retrieve my project's issues from the API. This data should be visible via the public routes, not requiring a user to be signed in to use.

If no user is currently logged in (i.e., localstorage is empty, without token) the GitHub axios call works as expected. However, if there is a token being stored in local-storage the browser returns the following CORS error:

"Request header field x-auth-token is not allowed by Access-Control-Allow-Headers in preflight response."

Here is what my axios call looks like:

import axios from "axios";

export default {
  getIssues: function () {
    return new Promise((resolve, reject) => {
      axios
        .get("https://api.github.com/repos/lucsedirae/open-house-crm/issues")
        .then((res) => {
          const issues = res.data;
          const results = issues.map((issue) => {
            return {
              title: issue.title,
              ghUser: issue.user.login,
              state: issue.state,
              createdAt: issue.created_at,
              updatedAt: issue.updated_at,
              description: issue.body,
              number: issue.number,
              comments: issue.comments,
            };
          });
          resolve(results);
        })
        .catch((err) => reject(err));
    });
  },
};

Is there a way to get the GitHub axios call to ignore the json web token? I've tried researching different headers or options using the cors package on my server.js but to no avail.

question from:https://stackoverflow.com/questions/65838258/github-api-get-request-is-automatically-sending-x-auth-token

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...