インターセプタ追加

This commit is contained in:
rca 2024-07-19 02:01:43 +09:00
parent 983e5976f5
commit 39a62ab55a

View File

@ -1,6 +1,7 @@
import { boot } from 'quasar/wrappers'; import { boot } from 'quasar/wrappers';
import axios, { AxiosInstance } from 'axios'; import axios, { AxiosInstance } from 'axios';
import { API_BASE_URL } from 'src/const'; import { API_BASE_URL } from 'src/const';
import useStore from 'src/store';
declare module '@vue/runtime-core' { declare module '@vue/runtime-core' {
interface ComponentCustomProperties { interface ComponentCustomProperties {
@ -15,8 +16,20 @@ declare module '@vue/runtime-core' {
// good idea to move this instance creation inside of the // good idea to move this instance creation inside of the
// "export default () => {}" function below (which runs individually // "export default () => {}" function below (which runs individually
// for each client) // for each client)
const store = useStore();
const api = axios.create({ baseURL: API_BASE_URL }) const api = axios.create({ baseURL: API_BASE_URL })
// トークン付与
api.interceptors.request.use((config) => {
const token = store.state.account.token;
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
export default boot(({ app }) => { export default boot(({ app }) => {
// for use inside Vue files (Options API) through this.$axios and this.$api // for use inside Vue files (Options API) through this.$axios and this.$api