Created
October 8, 2022 01:31
-
-
Save geekygeeky/8dad6e66ed9fe5100444c67feaf715ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineNuxtPlugin } from '#app' | |
import axios from 'axios' | |
import { useAuthStore } from '~/stores/auth' | |
export default defineNuxtPlugin(({ $pinia }) => { | |
return { | |
provide: { | |
axios: ({ use_auth }: Record<string, any> = {}) => { | |
let headers = { 'Content-Type': 'application/json' }; | |
if (use_auth) { | |
const authStore = useAuthStore($pinia); | |
headers['Authorization'] = `Bearer ${authStore.userToken}`; | |
} | |
return axios.create({ | |
baseURL: '', | |
headers: { ...headers } | |
}) | |
} | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment