0

I am developing with Nuxtjs and using nuxtauth.

I am trying to give the content " JWT " to the Authorization of http request.

However, even if I change the string to be given by coding, "Bearer " is sent and I get a 401 error.

failed call mypages API

The formed text (including the JWT) can be seen in the console.

expected header

The process is as follows.

<template>
  <div>
    <button @click="getUserInfo">get user data</button>
    <div>
      {{ responseData }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pretoken: this.$auth.strategy.token.get(),
      responseData: {}
    };
  },
  methods: {
    async getUserInfo() {
      const url = "/server/v1/mypages/";
      const pretoken = this.pretoken;
      const fixedtoken = pretoken.replace("Bearer", "JWT");
      console.log(fixedtoken);
      this.$axios.setHeader("Authorization", fixedtoken);
      this.responseData = await this.$axios.get(url);
    }
  }
};
</script>

How can I achieve this?

kissu
  • 40,416
  • 14
  • 65
  • 133
dore
  • 5
  • 2

1 Answers1

0

try the codes below and it should work

const config = { headers: { 'Authorization': fixedtoken } };
this.responseData = await this.$axios.get(url, config);
fevid
  • 723
  • 6
  • 18
  • Thanks! I tried that method and it did not work. The Authorization of the request was still Bearer < token >... I checked the config in the console, but it shows headers: {Authorization: "JWT "} and I don't know why :_( – dore Jul 17 '21 at 08:00
  • @dore, try adding this option to your `auth/nuxt` configuration. `auth: { token: { global: false, } }`, using `$axios` sends your token automatically and might be overwriting yours. I hope it works for you. – fevid Jul 17 '21 at 08:38
  • 1
    This is an absolute solution! However, to be precise, it worked correctly in my environment when I set Nuxtconfig as follows `auth:{ strategies:{ local:{ token: { global: false, }, ...... } } }` Thanks to you, I can move ahead with development! Thank you so much! – dore Jul 17 '21 at 17:05