[Vuejs]-Token is missing in my vue app

0👍

Updated The problem was in req. it’s res to receive the token instead of req

signup() {
      this.$http
        .post("/signup", { username: this.username, password: this.password })
        .then(res => this.signupSuccessful(res))
        .catch(() => this.signupFailed());
    },
    signupSuccessful(res) {
      if (!res.data.auth_token) {
        this.signupFailed();
        return;
      }
      this.error = false;
      localStorage.token = res.data.auth_token;
      this.$store.dispatch("login");
      this.$router.replace(this.$route.query.redirect || "/rooms");
    },
    .
    .
    .
    .

0👍

Try to set the token on every request with axios.interceptors

Put this on the main.js file so everywhere you import axios will have the config

axios.interceptors.request.use(config => {
 const token= localStorage.getItem('my-token-key') // or where you have the token

 config.headers.common['Authorization'] = 'Bearer ' + token

 // you must return the config or it will not work
 return config
})

I think that the problem is that axios.create instance is executed (created) just 1 time (then reference it), and not every time you import it, so if there was no token when the instance was created it will not work

Leave a comment