Skip to content

Instantly share code, notes, and snippets.

@GeekyGeeky
Created October 8, 2022 01:31
Show Gist options
  • Save GeekyGeeky/8dad6e66ed9fe5100444c67feaf715ed to your computer and use it in GitHub Desktop.
Save GeekyGeeky/8dad6e66ed9fe5100444c67feaf715ed to your computer and use it in GitHub Desktop.
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