SWS-5007 / My-Experience

13 stars 0 forks source link

How to send the JWT Token in Axios Request Header? #2

Open SWS-5007 opened 1 year ago

SWS-5007 commented 1 year ago

Hi, Everyone. I am going to send the JWT Token in axios request header to laravel api.

Here is my codebase:

    const access_token = localStorage.getItem("accessToken");
    await axios
      .get("http://localhost:8000/api/getTokenStatus", {
        headers: {
          Authorization: access_token,
        },
      })
      .then((res) => {
        console.log(res.data);
      })
      .catch((error) => {
        console.error(error);
      });

Here, I am getting the Authorization Token not found Error Msg from Laravel API JWT Middleware. This means, I didn't send the token correctly so that Laravel JWT Middleware can check the token. How should I send the token correctly? Please let me know.

SWS-5007 commented 1 year ago

Hello, I found the solution for this issue. Actually, Laravel JWT Middleware create any JWT Token as Bearer Method. So we need to send the token in axios request by adding the Bearer.

Here is my correct codebase.

    await axios
      .get("http://localhost:8000/api/getTokenStatus", {
        headers: {
          Authorization: `Bearer ${access_token}`,
        },
      })
      .then((res) => {
        console.log(res.data);
      })
      .catch((error) => {
        console.error(error);
      });

I hope my experience will help you. Thanks.