ZBLOG

vue jwt No authorization token was found

This error message indicates that the client is trying to access a protected resource without providing a valid authorization token.

To fix this issue, you need to ensure that your Vue application includes the authorization token in each request made to the server. This can be achieved by either storing the token in a cookie or local storage and sending it with each request using an HTTP interceptor.

Here’s an example of how to implement an HTTP interceptor in Vue.js:

import axios from 'axios';

// Create an Axios instance with default configuration
const axiosInstance = axios.create({
  baseURL: 'https://api.example.com',
});

// Add an interceptor that adds the authorization token to each request
axiosInstance.interceptors.request.use((config) => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

export default axiosInstance;

In this example, we create an Axios instance with a default base URL and add an interceptor function that checks for the presence of an authorization token in local storage. If a token is found, it is added to the headers of each outgoing request as a bearer token.

With this interceptor in place, you should no longer see the “No authorization token was found” error message when accessing protected resources.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=6059

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?